2008年5月26日 星期一

Lab ADT, accessor, mutator

public class Complex
{
private int real;
private int imaginary;

public void setComplex(int real,int imaginary)
{
this.real=real;
this.imaginary=imaginary;
}

public String getComplex()
{
return (real+"+"+imaginary+"i");
}

public Complex addComplex(Complex newComplex)
{
Complex result=new Complex();
result.real=real+newComplex.real;
result.imaginary=imaginary+newComplex.imaginary;
return result;
}

}


public class LabComplexClass
{
public static void main(String[] args)
{
Complex data1= new Complex(),
data2= new Complex();

data1.setComplex(2,3);
data2.setComplex(4,5);

System.out.println(data1.addComplex(data2).getComplex());

//Mutator
data1.setComplex(5, 6);
System.out.println(data1.addComplex(data2).getComplex());
}

}












沒有留言: