Results 1 to 11 of 11

Thread: Help Me!

  1. #1
    wardd85 is offline Member
    Join Date
    Jul 2008
    Posts
    8
    Rep Power
    0

    Default 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

  2. #2
    Jeremy is offline Member
    Join Date
    Jul 2008
    Posts
    28
    Rep Power
    0

    Default

    So why are you using void as your return type?

  3. #3
    wardd85 is offline Member
    Join Date
    Jul 2008
    Posts
    8
    Rep Power
    0

    Default

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

  4. #4
    Jeremy is offline Member
    Join Date
    Jul 2008
    Posts
    28
    Rep Power
    0

    Default

    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?

  5. #5
    wardd85 is offline Member
    Join Date
    Jul 2008
    Posts
    8
    Rep Power
    0

    Default

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

  6. #6
    Jeremy is offline Member
    Join Date
    Jul 2008
    Posts
    28
    Rep Power
    0

    Default

    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());
    The add() method doesn't need to return anything because it is modifying the complex number, not returning it. (num1 in the example.) If you want to get the complex number, then you'll need to call the toString() method.

  7. #7
    wardd85 is offline Member
    Join Date
    Jul 2008
    Posts
    8
    Rep Power
    0

    Default

    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;

  8. #8
    wardd85 is offline Member
    Join Date
    Jul 2008
    Posts
    8
    Rep Power
    0

    Default

    does what u said fit into what i was doing

  9. #9
    Jeremy is offline Member
    Join Date
    Jul 2008
    Posts
    28
    Rep Power
    0

    Default

    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?

  10. #10
    masijade is offline Senior Member
    Join Date
    Jun 2008
    Posts
    2,571
    Rep Power
    8

    Default

    Quote Originally Posted by Jeremy View Post
    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());
    The add() method doesn't need to return anything because it is modifying the complex number, not returning it. (num1 in the example.) If you want to get the complex number, then you'll need to call the toString() method.
    Not necessarily the toString() (although that should also be defined for ease in printing purposes), but notice the "outline" he was given in his homework contains a getReal and getImag method. Those are what should be used to work with the number itself.

  11. #11
    Jeremy is offline Member
    Join Date
    Jul 2008
    Posts
    28
    Rep Power
    0

    Default

    Quote Originally Posted by masijade View Post
    Not necessarily the toString() (although that should also be defined for ease in printing purposes), but notice the "outline" he was given in his homework contains a getReal and getImag method. Those are what should be used to work with the number itself.
    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.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •