2005/12/26

Lab 12-19-2005 Class Parameter

package welcome;

public class Complex
{
double real=0,image=0;
public Complex(double real,double image)
{ setComplex(real,image); }
public void setComplex(double real,double image)
{
this.real = real;
this.image = image;
}

public void add(Complex R)
{
this.real = this.real + R.real;
this.image = this.image + R.image;
}
}


package welcome;
public class Complex_Demo
{
public static void main(String args[])
{
Complex R1 = new Complex(2,3);
Complex R2 = new Complex(4,5);
System.out.println("R1: " + R1.real + " + " + R1.image + "*i");
System.out.println("R2: " + R2.real + " + " + R2.image + "*i");
R1.add(R2);System.out.println("Therefore, R1+R2 = " + R1.real + " + " + R1.image + "*i");
}
}

0 Comments:

張貼留言

<< Home