You are on page 1of 4

H v tn: Vit Thanh MSV: 1121050299 Lp: mng my tnh k56

Bi thc hnh s 2
bi: xy dng lp s phc c cc phng thc get/set, khi to hm
khng tham s, c tham s, phng thc nhp, in, cng, tr, nhn, chia s phc. Bi lm cc cu thng bo ra mn hnh em c s dng ting vit nn font ch trong Eclipse l UTF 8.

Class Complex
package complex; import javax.swing.JOptionPane; public class Complex { private double thuc; private double ao; //Khi to hm khng c tham s gn phn o = thc=0 public Complex() { thuc =0; ao = 0; } //Khi to 2 tham s thc v o public Complex(double thuc, double ao) { super(); this.thuc=thuc; this.ao=ao; } // phng thc nhp s phc public void nhapSoPhuc(Complex cmp) { String strThuc, strAo; strThuc = JOptionPane.showInputDialog(null,"Nhp vo phn thc: ","S Phc: ",JOptionPane.INFORMATION_MESSAGE); cmp.thuc=Double.parseDouble(strThuc); strAo = JOptionPane.showInputDialog(null,"Nhp vo phn o: ", "S Phc: ",JOptionPane.INFORMATION_MESSAGE); cmp.ao=Double.parseDouble(strAo);

} // phng thc in s phc public void inSoPhuc(Complex cmp) { JOptionPane.showMessageDialog(null, "S phc: "+cmp.thuc+" + "+cmp.ao+"i","Kt qu",JOptionPane.INFORMATION_MESSAGE); } // Cng 2 s phc (a + b.i) + (c + d.i) = (a + c) + (b + d).i public Complex congHaisoPhuc(Complex cmp1, Complex cmp2) { Complex cmp3 = new Complex(); cmp3.thuc = cmp1.thuc + cmp2.thuc; cmp3.ao = cmp1.ao + cmp2.ao; return cmp3; } // Tr 2 s phc (a + b.i) - (c + d.i) = (a - c) + (b - d).i public Complex truHaiSoPhuc(Complex cmp1, Complex cmp2) { Complex cmp3 = new Complex(); cmp3.thuc = cmp1.thuc - cmp2.thuc; cmp3.ao = cmp1.ao - cmp2.ao; return cmp3; } // Nhn 2 s phc (a + b.i)(c + d.i) = (a.c - b.d) + (b.c + a.d).i public Complex nhanHaiSoPhuc(Complex cmp1, Complex cmp2) { Complex cmp3 = new Complex(); cmp3.thuc = (cmp1.thuc*cmp2.thuc - cmp1.ao*cmp2.ao); cmp3.ao = (cmp1.ao*cmp2.thuc + cmp1.thuc*cmp2.ao); return cmp3; } // Chia 2 s phc (a + b.i)/(c + d.i) = (a.c + b.d)/(c.c + d.d) + ((b.c a.d).i)/(c.c + d.d) public Complex chiaHaiSoPhuc(Complex cmp1, Complex cmp2) { Complex cmp3 = new Complex(); if (cmp2.ao!=0 && cmp2.thuc!=0) { cmp3.thuc = (cmp1.thuc*cmp2.thuc + cmp1.ao*cmp2.ao)/(cmp2.thuc*cmp2.thuc+cmp2.ao*cmp2.ao); cmp3.ao = (cmp1.ao*cmp2.thuc cmp2.ao*cmp1.thuc)/(cmp2.thuc*cmp2.thuc+cmp2.ao*cmp2.ao); } else { System.out.println("Php chia s phc ny khng thc hin c! Mi xem li!"); cmp3.ao=0; cmp3.thuc=0; } return cmp3; } public double getThuc() { return thuc;

} public void setThuc(double thuc) { this.thuc = thuc; } public double getAo() { return ao; } public void setAo(double ao) { this.ao = ao; }

Test
package complex; import javax.swing.JOptionPane; public class TestComplex { public static void main(String[] args) { JOptionPane.showMessageDialog(null, "Gi tr mc nh ca s phc","Thng Bo",JOptionPane.INFORMATION_MESSAGE); Complex sp1 = new Complex(); sp1.inSoPhuc(sp1); JOptionPane.showMessageDialog(null, "Gi tr tham s t truyn vo ca s phc","Thng Bo",JOptionPane.INFORMATION_MESSAGE); Complex sp2 = new Complex(1.2, 2.3); sp2.inSoPhuc(sp2); JOptionPane.showMessageDialog(null, "Test phng thc nhp s phc","Thng Bo",JOptionPane.INFORMATION_MESSAGE); sp1.nhapSoPhuc(sp1); sp1.inSoPhuc(sp1); JOptionPane.showMessageDialog(null, "Test phng thc cng 2 s phc\n(1 + 2i) + (3 +4i)","Thng Bo",JOptionPane.INFORMATION_MESSAGE); Complex sp3 = new Complex(1.0, 2.0); Complex sp4 = new Complex(3.0, 4.0); sp1 = sp1.congHaisoPhuc(sp3, sp4); sp1.inSoPhuc(sp1); JOptionPane.showMessageDialog(null, "Test phng thc tr 2 s phc\nNhp bt k t bn phm","Thng Bo",JOptionPane.INFORMATION_MESSAGE); sp3.nhapSoPhuc(sp3); JOptionPane.showMessageDialog(null, "Tip tc nhp s phc th 2","Thng Bo",JOptionPane.INFORMATION_MESSAGE); sp4.nhapSoPhuc(sp4); sp1 = sp1.truHaiSoPhuc(sp3, sp4); sp1.inSoPhuc(sp1);

JOptionPane.showMessageDialog(null, "Test phng thc nhn 2 s phc\n(1 + 2i)(3 +4i)","Thng Bo",JOptionPane.INFORMATION_MESSAGE); sp1 = sp1.nhanHaiSoPhuc(sp3, sp4); sp1.inSoPhuc(sp1); JOptionPane.showMessageDialog(null, "Test phng thc chia 2 s phc\n(1 + 2i) \\ (3 +4i)","Thng Bo",JOptionPane.INFORMATION_MESSAGE); sp1 = sp1.chiaHaiSoPhuc(sp3, sp4); sp1.inSoPhuc(sp1); } }

You might also like