2009年6月1日 星期一

Lab: Static Method II

Define a Complex class with a static method for computing complex addition. Use (2+3i)*(4+5i) in your test.



public class ADT {
private int real, image ;
public ADT(){
}
public ADT(int real , int image) {
this.real = real;
this.image = image;
}
public ADT mult(ADT ADT2){
ADT ans = new ADT(0,0) ;
ans.real = this.real * ADT2.real + this.image * ADT2.image;
ans.image = this.image * ADT2.real + this.real * ADT2.image;
return ans;
}
public String toString(){
return real + "+" + image + "i";
}
}

-----

public class StaticMethodII {
public static void main(String[] args) {
ADT f1, f2;
f1 = new ADT(2,3);
f2 = new ADT(4,5);
System.out.println("f1 = " + f1.toString());
System.out.println("f2 = " + f2.toString());
System.out.println("f1 x f2 = " + f1.mult(f2).toString());
}



沒有留言:

張貼留言