728x90
class Complex{
public double real;
public double image;
Complex(double r, double i){
real = r;
image = i;
}
public Complex addComplex(Complex c) {
Complex c0 = new Complex(0,0);
c0.real = real + c.real;
c0.image = image + c.image;
return c0;
}
public Complex subComplex(Complex c) {
Complex c0 = new Complex(0,0);
c0.real = real - c.real;
c0.image = image - c.image;
return c0;
}
public Complex mulComplex(Complex c) {
Complex c0 = new Complex(0,0);
c0.real = real * c.real - image * c.image;
c0.image = real * c.image + image * c.real;
return c0;
}
public Complex divComple(Complex c) {
Complex c0 = new Complex(0,0);
c0.real = (real * c.real + image * c.image) /
(c.real * c.real + c.image * c.image);
c0.image = (image * c.real - real * c.image) /
(c.real * c.real + c.image * c.image);
return c0;
}
public String toString() {
String form = "(" + real + ", " + image + "i)";
return form;
}
}
public class chp5_7 {
public static void main(String[] args) {
Complex c1 = new Complex(1,2);
Complex c2 = new Complex(3,4);
Complex c3 = new Complex(5,6);
Complex c4 = new Complex(7,8);
c1 = c1.addComplex(c2);
c2 = c2.subComplex(c3);
c3 = c3.mulComplex(c4);
c4 = c4.divComple(c1);
System.out.print("c1 = " + c1 + ", c2 = " + c2 +
", c3 = " + c3 + ", c4 = " + c4);
}
}
반응형
'전.java' 카테고리의 다른 글
Count 클래스 (0) | 2021.02.08 |
---|---|
스택 클래스 (Stack) (0) | 2021.02.08 |
분수 클래스 (Fraction) (0) | 2021.02.08 |
거스름돈을 동전의 개수가 최소가 되도록 계산하는 자바 프로그램 (0) | 2021.02.07 |
숫자 찍기 (0) | 2021.02.07 |