TH BAI THUC HANH 2
/*COMPLEX
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package baithuchanh2;
/**
*
* @author VY
*/
import javax.swing.JOptionPane;
public class Complex {
private double a;
private double b;
public void setA(double _a){
this.a=_a;
}
public double getA(){
return this.a;
}
public void setB(double _b){
this.b=_b;
}
public double getB(){
return this.b;
}
public Complex(){
this.a=0;
this.b=0;
}
public Complex(double _a,double _b){
this.a=_a;
this.b=_b;
}
public void nhapSoPhuc(){
boolean check;
do
{
String strA=JOptionPane.showInputDialog("Nhap phan thuc a:");
try
{
this.a=Double.parseDouble(strA);
check=true;
}
catch(Exception e)
{
check=false;
}
} while(check==false);
do
{
String strB=JOptionPane.showInputDialog("Nhap phan thuc b:");
try
{
this.b=Double.parseDouble(strB);
check=true;
}
catch(Exception e)
{
check=false;
}
} while(check==false);
}
public Complex congHaiSoPhuc(Complex cmp1,Complex cmp2)
{
Complex cmp=new Complex();
cmp.a=cmp1.a+cmp2.a;
cmp.b=cmp1.b+cmp2.b;
return cmp;
}
public Complex truHaiSoPhuc(Complex cmp1,Complex cmp2)
{
Complex cmp=new Complex();
cmp.a=cmp1.a-cmp2.a;
cmp.b=cmp1.b=cmp2.b;
return cmp;
}
public Complex nhanHaiSoPhuc(Complex cmp1,Complex cmp2)
{
Complex cmp=new Complex();
cmp.a=cmp1.a*cmp2.a-cmp1.b*cmp2.b;
cmp.b=cmp1.a*cmp2.b+cmp1.b*cmp2.a;
return cmp;
}
public Complex chiaHaiSoPhuc(Complex cmp1,Complex cmp2)
{
Complex cmp=new Complex();
cmp.a=(cmp1.a*cmp2.a+cmp1.b*cmp2.b)/(Math.pow(cmp2.a, 2)+Math.pow(cmp2.b, 2));
cmp.b=(cmp1.b*cmp2.a-cmp1.a*cmp2.b)/(Math.pow(cmp2.a, 2)+Math.pow(cmp2.b, 2));
return cmp;
}
public void inSoPhuc(Complex cmp)
{
JOptionPane.showMessageDialog(null, "So phuc:"+a+"i+"+b);
}
}
/*MAIN
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package baithuchanh2;
/**
*
* @author VY
*/
public class Main {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
// test Complex
Complex complex=new Complex();
complex.nhapSoPhuc();
complex.inSoPhuc(complex);
//
Complex cmp1=new Complex(1,2);
//Cong
Complex cmp2=new Complex();
cmp2=cmp2.congHaiSoPhuc(complex, cmp1);
}
}
Bạn đang đọc truyện trên: Truyen247.Pro