I think your code is about complex numbers whose general syntax
is --------x+iy-----------
where x is its real part & y is its imaginary part.
and you want to perform some mathematical operation on them like
addition , Subtraction , multiplication , division , etc.
I make some modification to your code (commented some thing from that & adding some thing from my side)as i written addition& subtraction operation , you can write other operations also.
code
------------------------------------
<pre>
/**
* Title: Govinda
* Description:
* Copyright: Copyright (c)
* Company:
* @author
* @version 1.0
*/
package Proj1;
public class complex
{
private double real, imaginary;
private complex c1_1, c2_2;
public complex(double r, double i)
{
real=r;
imaginary=i;
}
/*public complex(complex c1, complex c2)
{
c1_1=c1;
c2_2=c2;
}*/ // i don't understand the purpose of this constructor.
public complex add(complex c)
{
return new complex(c.real+this.real,c.imaginary+this.imaginary);
}
public complex subtract (complex c)
{
return new complex(this.real-c.real ,this.imaginary-c.imaginary);
}
public String toString() {
return "\nThe Real part of Complex Number is "+ real +"\n"+
"The Imaginary part of Complex Number is "+ imaginary ; }
public static void main(String[] args)
{
complex c1=new complex(1.0,2);
System.out.println("Complex number c1 is"+c1);
complex c2=new complex(3,4);
System.out.println("Complex number c2 is"+c2);
complex c3=c1.add(c2);
System.out.println("The addition of c1 & c2 produced "+c3);
complex c8=new complex(-1,-2);
System.out.println("Complex number c8 is"+c8);
complex c9=new complex(1,2);
System.out.println("Complex number c9 is"+c9);
System.out.println("The subtraction of c8 & c9 produced "+c8.subtract(c9));
/*complex c4=new complex(c1,c2);
complex c5=new complex(c8,c9);
by this code if you want to add two complex numbers and return new one then write an another method to do so */
}
}
</pre>
when trying to run this code place in its appropriate package ,
give proper command line with package name and see the result.
one more thing your classpath should pointing to that package Proj1.
ooooop's sheeeeeeeeeeeee silence
Originally posted by Hemal Mehta:
Can anyone tell what this program accomplishes, i am somewhat confused!
**
* Title: Hemal Mehta
* Description:
* Copyright: Copyright (c)
* Company:
* @author
* @version 1.0
*/
package Proj1;
public class complex
{
private double real, imaginary;
private complex c1_1, c2_2;
private complex(double r, double i)
{
real=r;
imaginary=i;
}
protected complex(complex c1, complex c2)
{
c1_1=c1;
c2_2=c2;
}
protected complex add1(complex c1, complex c2)
{
return new complex(c1,c2);
}
public complex add(complex c)
{
return new complex(c.real+c.imaginary,c.real+c.imaginary);
}
public static void main(String[] args)
{
complex c1=new complex(1.0,2);
complex c2=new complex(3,4);
complex c3=c1.add(c2);
complex c10=c2.add1(c1,c2);
complex c8=new complex(-1,-2);
complex c9=new complex(1,2);
complex c4=new complex(c1,c2);
complex c5=new complex(c8,c9);
}
}