Define a Complex class with a static method for computing complex addition. Use (2+3i)+(4+5i) in your test.
-------------
public class Complex {
int real;
int image;
public Complex()
{}
public Complex( int real , int image)
{
this.real = real;
this.image = image;
}
public static Complex add(Complex a, Complex b) {
Complex ans = new Complex();
ans.real = a.real + b.real;
ans.image = a.image + b.image;
return ans;
}
public String toString() {
return real + "+" + image + "i";
}
}
---------------
public class LabStaticMethod {
public static void main (String args[]) {
Complex f1, f2;
f1 = new Complex(2, 3);
f2 = new Complex(4, 5);
System.out.println("f1 = " + f1.toString());
System.out.println("f2 = " + f2.toString());
System.out.println("f1 + f2 = " + Complex.add(f1, f2).toString());
}
}
沒有留言:
張貼留言