Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Can someone shed some light on this program

 
Hemal Mehta
Ranch Hand
Posts: 101
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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);
}
}
 
Brett Knapik
Ranch Hand
Posts: 255
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i don't really think this is a program. its a package but then again it has a main method
------------------
I wish there was a button on my monitor to turn up the intellegince.
Theres a button called 'brightness' but it doesn't work
 
Govinda
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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);
}
}

 
Prasad Ballari
Ranch Hand
Posts: 149
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hemal,
The program is just one way of creating objects.
Explaination goes like this
package Proj1;
public class complex{
private double real, imaginary;
private complex c1_1, c2_2;
//Constructor
private complex(double r, double i){
real=r;
imaginary=i;
System.out.println("in constr real=" + real + " img=" + imaginary );
}
//Constructor
protected complex(complex c1, complex c2){
/*Here real, imaginary are not set for the object hence when you create object with this constructor,you can't get values real and imaginary variables.Still if you want the data then u should refer through c1_1 and c2_2 object reference. */
c1_1=c1;
c2_2=c2;
}
//method
protected complex add1(complex c1, complex c2){
return new complex(c1,c2);
}
//method
public complex add(complex c){
System.out.println("c2 values real=" + c.real+" img=" + c.imaginary);
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);
}
}

Note: The Hemal's original program does't contain any result for output hence the program after running becomes mum.

HTH
Prasad

------------------

[This message has been edited by Prasad Ballari (edited November 26, 2000).]
[This message has been edited by Prasad Ballari (edited November 26, 2000).]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic