Complex
package Lab7;public class Complex
{
private double real,imaginary ;
public Complex(double realN, double imaginaryN )
{
this.real=realN;
this.imaginary =imaginaryN;
}
public Complex()
{
real = 0 ;
imaginary = 0;
}
public Complex(double newValue )
{
this.real= newValue;
this.imaginary =0;
}
public static Complex add(Complex C1,Complex C2)
{
Complex C3 = new Complex(0,0);
C3.real = C1.real + C2.real ;
C3.imaginary = C1.imaginary+C2.imaginary;
return C3;
}
public static String toString(Complex C3)
{
return Integer.toString((int)C3.real)+"+"+Integer.toString((int)C3.imaginary )+"i";
}
public void writeOutput()
{
System.out.println(real+"+" + imaginary + "i");
}
}
-------
package Lab7;
public class ComplexDemo {
public static void main(String[] args) {
Complex C1 = new Complex(2,3);
C1.writeOutput();
Complex C2 = new Complex(4,5);
C2.writeOutput();
Complex C3 = Complex.add(C1,C2);
System.out.println("The value is : "+C3.toString(C3));
}
}
1 Comments:
Wrong:
method should be part of the class.
instance variables should be private.
張貼留言
<< Home