Can someone help me out my complex class won't complie?
This thing has been buging me for days I can't get it to work :mad:
Who made up imaginary numbers anyway :(
Code:
public class Complex
{
//Data
double real, imaginary;
Can you post the error you get when you try to compile?
11-15-2005, 01:02 AM
Mcody2
I think it has something to do with the equals and the reciprocal class
11-15-2005, 01:09 AM
evlich
Can you post the exact output from the compiler?
11-17-2005, 01:07 PM
aniseed
It looks to me like you tried to modify the code I posted on the other thread and created some inconsistencies in the process. In your code, you are returning a Complex object from the method abs() and in another place it is expecting a double
Code:
double sqAbs = this.abs();
Anyway, I fail to understand why you should return another Complex object instead of a double. The formula is as simple as sqrt(real^2 + imaginary^2) and it is always real. Of course, if you want to represent everything as Complex, you should take care of this point.
Your member variables "real" and "imaginary" do not have a getReal() and getImaginary() method. It seems like you didn't include it at all. Similarly for the set methods. You need to have them to provide the get methods atleast. Alternatively, you could make the variables public and refer to them directly. But it would be quite a violation of OO principles.
11-17-2005, 01:34 PM
aniseed
Here's my sample code again, with some additions. See if this helps.
Code:
/* Note: Class not defined public to stuff all the code in one source file. */
/* Complex class */
class Complex {
private double a; // Real part of the complex number
private double b; // Imaginary part of the complex number
/* Constructors */
public Complex() {
// Default no-arg constructor.
// Real and imaginary parts are 0.0d by default.
}
public Complex(double real, double imaginary) {
// Set the real and imaginary parts
this.a = real;
this.b = imaginary;
}
public Complex(Complex complex) {
// Create a copy of the Complex object passed as argument.
this.a = complex.getReal();
this.b = complex.getImaginary();
}
/* Overloaded toString() method from java.lang.Object
to print the Complex number representation. */
public String toString() {
return this.a +(this.b>0?"+":"") +this.b +"j";
}
/* Overloaded equals() method from java.lang.Object
to test the equality of two Complex objects */
public boolean equals(Complex complex) {
return (this.a == complex.getReal()) && (this.b == complex.getImaginary());
}
/* Getter methods */
public double getReal() { return this.a; }
public double getImaginary() { return this.b; }
/* Returns the absolute value */
public double abs() {
// This method will work with JDK 1.5
return Math.hypot(this.a, this.b);
/*
For older versions of JDK, use the following line instead:
Math.sqrt(Math.pow(this.a, 2) +Math.pow(this.b, 2));
*/
}
/* Square of absolute value */
public double sqAbs() {
return Math.pow(this.a, 2) +Math.pow(this.b, 2);
}
/* Complex Conjugate of the complex number */
public Complex conjugate() {
// Returns a-b*j
return new Complex(this.a, -this.b);
}
/* Reciprocal of the Complex number */
public Complex reciprocal() {
// Returns (a-b*j)/(a^2 +b^2)
Complex reciprocal = this.conjugate();
double sqAbs = this.sqAbs();
reciprocal.setReal(reciprocal.getReal()/sqAbs);
reciprocal.setImaginary(reciprocal.getImaginary()/sqAbs);
return reciprocal;
}
// Additional methods.
/* Adding another Complex number to the present Complex number
Note: The method returns void since the state of the original object
will change, for which the method was called. */
public void add(Complex complex) {
this.a += complex.getReal();
this.b += complex.getImaginary();
}
/* Multiply a Complex number to the present number */
public void multiply(Complex complex) {
double real = complex.getReal();
double imag = complex.getImaginary();
this.a = a*real - b*imag;
this.b = b*real + a*imag;
}
/* Divide */
public void divide(Complex complex) {
// Division is the same as multiplying with the reciprocal.
this.multiply(complex.reciprocal());
}
/* TODO: Add other methods for complex number operations here. */
}
/* Class to test the Complex number operations. */
public class TestComplex {
public static void main(String[] argv) {
Complex complex = new Complex(1, 2);
System.out.println("Complex number = " +complex);
System.out.println("Absolute value = " +complex.abs());
System.out.println("Reciprocal = " +complex.reciprocal());
complex.add(new Complex(2, 5));
System.out.println("Adding a complex number = " +complex);
complex.subtract(new Complex(0, 3));
System.out.println("Subtracting a complex number = " +complex);
complex.multiply(new Complex(7, 2));
System.out.println("Multiplying a complex number = " +complex);
complex.divide(new Complex(4, 9));
System.out.println("Dividing a complex number = " +complex);
public class ComplexTest
{
public static void main (String[] args)
{
double localN = 0;
Console c = new Console ();
Complex x = new Complex (5, -5);
// x.get(c);
c.println (x.toString ());
Complex y = new Complex (10, 0);
Complex adds = new Complex ();
Complex subtracts = new Complex ();
Complex multiplys = new Complex ();
Complex divides = new Complex ();
Complex power = new Complex ();
adds = x.add (y);
subtracts = x.subtract (y);
multiplys = x.multiply (y);
divides = x.divide (y);
c.println ("addition");
c.println (adds.toString ());
c.println ("subtraction");
c.println (subtracts.toString ());
c.println ("multiplication");
c.println (multiplys.toString ());
c.println ("division");
c.println (divides.toString ());
c.println ("sqrt (a^2 +b^2) is:" + x.abs ());
c.print ("enter a value of n to calculate power");
localN = c.readDouble ();
power = (x.pow (localN));
c.println ("the power is " + power.toString ());
c.println ("in relation to second complex, the first is");
c.println ("greater?" + x.greaterThan (y));
c.println ("less?" + x.lessThan (y));
c.println ("equal to?" + x.equalTo (y));
}
}
/*
class Complex
This class stores a complex number and allows user to get, add, subtract, multiply, divide, find absolute value,
return value of power
Fields:
a - simple part of complex number, a double
b - imaginary part of complex number, a double
Methods:
constructor
get -gets field values of the complex number from the user
toString -prints the complex number in correct form with regards to +/- zeroes and ones
(eg) if a=5 and b=-1, it prints:5 - i
add -adds Complex with a given Complex
subtract -subtracts Complex with a given Complex
multiply -multiplies Complex with a given Complex
divide -divides Complex with a given Complex
abs -returns value of square root of (a^2+b^2)
pow -returns value of (a+bi)^n, given n where (a+bi)^n=r^ncos(nt)+r^n*sin(nt)i
-where r=abs() t=atan(b/a)
*/
class Complex
{
protected double a;
protected double b;
/**********************************************************************************
Constructor for class Complex
Parameters: two parts of complex number
Return Value:none
**************************************************************************************/
public Complex (double aLocal, double bLocal)
{
a = aLocal;
b = bLocal;
}
/**********************************************************************************
Overloaded constructor for class Complex
Parameters: none
Return Value:none
**************************************************************************************/
public Complex ()
{
this (0, 0);
}
/**********************************************************************************
get
Inputsnewvaluesinto the complex(values from user)
Parameters: The console object to get input from
Returns Value: none(void)
**************************************************************************************/
public void get (Console c)
{
c.print ("enter a value for a");
a = c.readDouble ();
c.print ("enter a value for b");
b = c.readDouble ();
}
/****************************************************************************************** ***************
toString
Createa a string representation of the contents of the object
prints the complex number in correct form with regards to +/- zeroes and ones and correct spacing
(eg) if a=5 and b=-1, it prints:5 - i
****************************************************************************************** **************/
public String toString ()
{
String s = "";
boolean noA = false;
if (a == 0 && b == 0)
s = "0";
else if (a == 0)
{
s = s;
noA = true;
}
else
s = s + a;
if (b == 0)
s = s;
else if (b == 1)
{
if (!(noA))
s = s + " + i";
else
s = s + "i";
}
else if (b == -1)
if (!(noA))
s = s + " " + "- i";
else
s = s + "" + "-i";
else if (b < 0)
if (!(noA))
s = s + " " + "- " + - b + "i";
else
s = s + "" + b + "i";
else if (b > 0)
{
if (!(noA))
s = s + " + " + b + "i";
else
s = s + "" + b + "i";
}
return s;
}
/**********************************************************************************
Method add
adds the Complex to a given complex number
Parameters: none
Returns Value: new a and b values, as result of addition, a new complex
***********************************************************************************/
public Complex add (Complex s)
{
Complex result = new Complex ();
result.a = (a + s.a);
result.b = (b + s.b);
return result;
}
/**********************************************************************************
Method subtract
subtracts the Complex by a given complex number
Parameters: none
Returns Value: new a and b values, as result of subtraction, a new complex
************************************************************************************/
public Complex subtract (Complex s)
{
Complex result = new Complex ();
result.a = (a - s.a);
result.b = (b - s.b);
return result;
}
/* Method multiply
multiplies the Complex to a given complex number
Parameters: none
Returns Value: new a and b values, as result of multiplication, a new complex
*/
public Complex multiply (Complex s)
{
Complex result = new Complex ();
result.a = (a * s.a) - (b * s.b);
result.b = (a * s.b) + (s.a * b);
return result;
}
/* Method divide
divides the Complex by a given complex number
Parameters: none
Returns Value: new a and b values, parts of a new complex
*/
/* Method pow
returns the value of (a+bi)^n for the current complex
(r^n)*cos(nt)+r^n(sin(nt))i
Parameters: none
Returns Value: value of (a+bi)^n, a double
*/
public Complex pow (double n)
{
double r = abs ();
double t = Math.atan (b / a);
Complex result = new Complex ();
result.a = Math.pow (r, n) * (Math.cos (n * t));
result.b = Math.pow (r, n) * (Math.sin (n * t));
return result;
}
/* method lessThan
compares the current complex to another to determine if the current complex is less than another,
returns true or false, true if the complex is less than another,
false, if the complex is not less than a given complex
Parameters: Complex y, a given complex
Returns Value:boolean */
public boolean lessThan (Complex y)
{
if (abs () < y.abs ())
return true;
else
return false;
}
/* method greaterThan
compares the current complex to another to determine if the current complex is greater than another,
returns true or false,true if the current complex is greater than than a given complex
false,if the complex is not greater than a given complex
Parameters:Complex y,agiven complex
ReturnsValue:boolean */
public boolean greaterThan (Complex y)
{
if (abs () > y.abs ())
return true;
else
return false;
}
/*
method equalTo
compares the current complex to another to determine if the current complex is equal to another,
returns true or false,true if the current complex is equal to a given complex
false,if the complex is not equal to a given complex
Parameters:Complex y,a given complex
ReturnsValue:boolean
*/
public boolean equalTo (Complex y)
{
if (abs () == y.abs ())
return true;
else
return false;
}
}
12-01-2005, 11:29 PM
aniseed
Would you please edit your post and use the code tags for formatting the code?