Results 1 to 11 of 11

# Thread: Help Me!

- 07-13-2008, 01:16 AM #1Member
- Join Date
- Jul 2008
- Posts
- 8

- Rep Power
- 0

## Help Me!

Okay heres the project.....

Objective: Implement a ComplexNumber class to represent complex numbers (z = a + bi) and a ComplexNumberDriver class to test the former class.

1)

Instance variables:

private double real; /* real part */

private double imag; /* imaginary part */

2)

Constructor: Overload the constructor by providing 3 different versions of it:

public ComplexNumber ( double real, double imag )

{ /* regular constructor */ }

public ComplexNumber ( double real )

{ /* initialize the imag part to 0.0 */ }

public ComplexNumber ( ComplexNumber num )

{ /* duplicate NUM */ }

3)

Get/Set functions:

public double getReal ( )

{ /* returns the real part */ }

public double getImag ( )

{ /* returns the imaginary part */ }

public void setReal ( double realVal )

{ /* sets real to realVal */ }

public void setImag ( double imagVal )

{ /* sets imag to imagVal */ }

4)

Other non-static functions:

public double calcAbs ( )

{ /* returns the absolute value of this object */ }

public void add ( ComplexNumber num )

{ /* add NUM to this object */ }

public void sub ( ComplexNumber num )

{ /* subtract NUM from this object */ }

public void mult ( ComplexNumber num )

{ /* multiply NUM with this object */ }

public void div ( ComplexNumber num )

{ /* divide this object by NUM */ }

public String toString ( )

{

/* print this object */

/* if real = 3.0 and imag = -4.0, then you should return “3.0 – 4.0i” */

}

5)

Comparable interface:

public int compareTo ( Object num )

{

// compare this object to NUM based on its absolute value

}

How do do the adding,subtracting,mult,div...with void? Ive seen other ways to do it but it wasn't with void

- 07-13-2008, 02:47 AM #2Member
- Join Date
- Jul 2008
- Posts
- 28

- Rep Power
- 0

So why are you using void as your return type?

- 07-13-2008, 02:49 AM #3Member
- Join Date
- Jul 2008
- Posts
- 8

- Rep Power
- 0

It is required...I don't understand it..cause every example ive seen doesn't use void ,but i have to for this one.

- 07-13-2008, 02:55 AM #4Member
- Join Date
- Jul 2008
- Posts
- 28

- Rep Power
- 0

It looks like your methods are designed to modify (mutate) the class. In other words, the methods are designed to modify a complex number. Thus, you need to keep track of this complex number within the class. It looks like you already have real and imaginary number instance variables. Why not just modify those?

- 07-13-2008, 02:58 AM #5Member
- Join Date
- Jul 2008
- Posts
- 8

- Rep Power
- 0

could you help explain...as the board says im new to java lol..

- 07-13-2008, 03:08 AM #6Member
- Join Date
- Jul 2008
- Posts
- 28

- Rep Power
- 0

Your class represents a complex number, right? So, I should be able to do something like this:

Java Code:ComplexNumber num1 = new ComplexNumber(2.2, 3.0); ComplexNumber num2 = new ComplexNumber(2.2, 3.0); num1.add(num2); System.out.println(num1.toString());

- 07-13-2008, 03:16 AM #7Member
- Join Date
- Jul 2008
- Posts
- 8

- Rep Power
- 0

heres what i had done so far....

public class ComplexNumber {

private double real; // real part

private double imag; // imaginary part

// 1st Constructor

public ComplexNumber(double real, double imag)

{

this.real=real;

this.imag=imag;

}

// 2nd Constructor

public ComplexNumber (double real)

{

this.real=real;

this.imag=0.0;

}

// 3rd Constructor

public ComplexNumber( ComplexNumber num )

{

this.real = num.real;

this.imag = num.imag;

}

public double getReal() // returns real number

{

return this.real;

}

public double getImag() // returns imaginary number

{

return this.imag;

}

public void setReal(double realVal)

{

this.real = realVal;

}

public void setImag(double imagVal )

{

this.imag = imagVal;

}

// finds the absolute value

public double calcAbs() {

return Math.sqrt(real*real + imag*imag);

}

// Adding Complex Numbers

public void add(ComplexNumber num)

{

ComplexNumber num1 = new ComplexNumber(2.0, 3.0);

ComplexNumber num2 = new ComplexNumber(2.0, 3.0);

num1.add(num2);

System.out.println(num1.toString());

}

// Subtracting Complex Numbers

public void sub(ComplexNumber num)

{

}

// Multiplying Complex Numbers

public void mult(ComplexNumber num)

{

}

// Dividing Complex Numbers

public void div (ComplexNumber num)

{

}

public String toString()

{

if (this.real == 0)

{

if (this.imag == 0)

{

return "0";

} else

{

return (this.imag + "i");

}

} else

{

if (this.imag == 0)

{

return String.valueOf(this.real);

} else if (this.imag < 0) {

return(this.real + " " + this.imag + "i");

} else {

return (this.real + " +" + this.imag + "i");

}

}

}

public int CompareTo(Object num)

{

ComplexNumber OtherObj = (ComplexNumber) num;

double myval = this.calcAbs();

double otherval = OtherObj.calcAbs();

if (myval < otherval)

return -1;

if (myval > otherval)

return 1;

else

return 0;

- 07-13-2008, 03:17 AM #8Member
- Join Date
- Jul 2008
- Posts
- 8

- Rep Power
- 0

does what u said fit into what i was doing

- 07-13-2008, 05:37 AM #9Member
- Join Date
- Jul 2008
- Posts
- 28

- Rep Power
- 0

No. All you did was copy my unit test and paste it into your add method. How does that solve your problem? The piece of code I gave you was supposed to show you how your methods would work if they were already written.

Did you write any of this code or was it given to you? It doesn't seem you're grasping the concept of object orientation. Let me help you out with an example.

Imagine you have a complex number in a box. This box is your object and the complex number is broken up into your instance variables. Now, if you want to add a complex number to your complex number, then you need to get a new complex number and push it into your box in the "add" slot. This add slot is like your add method. The slot (or method) will take the new complex number and add it to your complex number in the box. Now, to check to see if the correct addition took place, you can call the toString method which will grab the complex number and print it out for you. It would do this by taking the instance variables (real and imag) and format it accordingly.

Does that make sense?

- 07-13-2008, 08:29 AM #10Senior Member
- Join Date
- Jun 2008
- Posts
- 2,571

- Rep Power
- 11

- 07-13-2008, 03:14 PM #11Member
- Join Date
- Jul 2008
- Posts
- 28

- Rep Power
- 0

Yes.

*I did use it for printing purposes*.

I don't think you understand the purpose of that code block either. It's not code that is meant to be in his class. It's code that is meant to use his class. These types of code blocks are generally called USE cases. They are helpful in understanding how your class should work. I was simply providing him one to help me understand how is class should work. To help him visualize it.

## Bookmarks