Monday, December 25, 2006

Lab Static Method, Part II

















public class Complex
{
private double r,i;
public Complex(double newr,double newi)
{
r=newr;
i=newi;
}
public static Complex add(Complex c1,Complex c2)
{
Complex c3=new Complex(0,0);
c3.i=c1.i+c2.i;
c3.r=c1.r+c2.r;
return c3;
}
public Complex add(Complex c1)
{
Complex c3=new Complex(0,0);
c3.i=c1.i+i;
c3.r=c1.r+r;
return (c3);
}

public void output(){
System.out.println(this.r+"+" + this.i + "i");
}
}

/////

public class demo
{
public static void main(String[] args)
{
Complex c1 = new Complex(2,3);
Complex c2 = new Complex(4,5);
Complex c3 =Complex.add(c1,c2);
System.out.print("(2+3i)+(4+5i)=");
c3.output();
}}

No comments: