sixshotsix
sixshotsix

Reputation: 25

Homework Help (The Complex Class)

Continuation of Questions Related To This Program

First I'd like to apologize if I'm placing this in the wrong area. This is my first post and I haven't quite figured out how everything works yet.

I'm struggling a lot with this homework assignment...

**(Math: The Complex class) A complex number is a number in the form a + bi,
where a and b are real numbers and i is 2-1. The numbers a and b are known
as the real part and imaginary part of the complex number, respectively. You can
perform addition, subtraction, multiplication, and division for complex numbers
using the following formulas:
a + bi + c + di = (a + c) + (b + d)i
a + bi - (c + di) = (a - c) + (b - d)i
(a + bi)*(c + di) = (ac - bd) + (bc + ad)i
(a + bi)/(c + di) = (ac + bd)/(c2 + d2) + (bc - ad)i/(c2 + d2)
You can also obtain the absolute value for a complex number using the following
formula:
 a + bi  = 2a2 + b2
Design a class named Complex for representing complex numbers and the
methods add, subtract, multiply, divide, and abs for performing complexnumber
operations, and override toString method for returning a string representation
for a complex number. The toString method returns (a + bi) as a
string. If b is 0, it simply returns a. Your Complex class should also implement the
Cloneable interface.
Provide three constructors Complex(a, b), Complex(a), and Complex().
Complex() creates a Complex object for number 0 and Complex(a) creates
a Complex object with 0 for b. Also provide the getRealPart() and
getImaginaryPart() methods for returning the real and imaginary part of the
complex number, respectively.
Write a test program that prompts the user to enter two complex numbers and
displays the result of their addition, subtraction, multiplication, division, and absolute
value.**

Here is what I have so far. Two classes...

// ComplexTest.java

import java.util.Scanner;

public class ComplexTest {
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);

        System.out.println("Enter the first complex number: ");
        double realPart = input.nextDouble();

        System.out.println("Enter the second complex number: ");
        double imaginaryPart = input.nextDouble();

        Complex cn1 = new Complex(realPart, imaginaryPart);
        Complex cn2 = new Complex(realPart);
        Complex cn3 = new Complex();

        if (realPart == 0) {
            System.out.println(cn3.toString());
        }
        if (imaginaryPart == 0) {
            System.out.println(cn2.toString());
        }
        if(realPart != 0 && imaginaryPart != 0) {
            System.out.println(cn1.toString());
        }
    }
}

// Complex.java

import java.util.Scanner;

public class Complex {

    // cloneable interface
    public interface Cloneable { }

    // Instance Real + Getters and Setters (Accessors and Mutators)
    private double realPart;

    public double getReal() {
        return realPart;
    }

    public void setReal(double real) {
        this.realPart = real;
    }

    // Instance Real + Getters and Setters (Accessors and Mutators)

    private double imaginaryPart;

    public double getImaginary() {
        return imaginaryPart;
    }

    public void setImaginary(double imaginary) {
        this.imaginaryPart = imaginary;
    }

    // Constructor Method CN1
    public Complex(double a, double b) {
        realPart = a;
        imaginaryPart = b;
    }

    // Constructor Method CN2
    public Complex(double a) {
        realPart = a;
        imaginaryPart = 0;
    }

    // Constructor Method CN3
    public Complex() { }

    // Add Complex Numbers
    public Complex add(Complex comp1, Complex comp2) {
        double real1 = comp1.getReal();
        double real2 = comp2.getReal();
        double imaginary1 = comp1.getImaginary();
        double imaginary2 = comp2.getImaginary();

        return new Complex(real1 + real2, imaginary1 + imaginary2);
    }

    // Subtract Complex Numbers
    public Complex subtract(Complex comp1, Complex comp2) {
        double real1 = comp1.getReal();
        double real2 = comp2.getReal();
        double imaginary1 = comp1.getReal();
        double imaginary2 = comp2.getReal();

        return new Complex(real1 - real2, imaginary1 - imaginary2);
    }

    // Multiply Complex Numbers
    public Complex multiply(Complex comp1, Complex comp2) {
        double real1 = comp1.getReal();
        double real2 = comp2.getReal();
        double imaginary1 = comp1.getReal();
        double imaginary2 = comp2.getReal();

        return new Complex(real1 * real2, imaginary1 * imaginary2);
    }

    // Divide Complex Numbers
    public Complex divide(Complex comp1, Complex comp2) {
        double real1 = comp1.getReal();
        double real2 = comp2.getReal();
        double imaginary1 = comp1.getReal();
        double imaginary2 = comp2.getReal();

        return new Complex(real1 / real2, imaginary1 / imaginary2);
    }

    // toString to Change Display
    public String toString() {
        String result;
        result = realPart + " + " + imaginaryPart + "i";
        return result;
    }
}

Here is my updated code after Jan's help. I've created 3 more methods (subtract, multiply and divide). Should I not be using comp1 and comp2 in every method and instead name them separately from each other? The goal is to print the results of each method at the end at the same time. Will these having the same names mess with that?

I'd also like to know when I should implement the cloneable interface.

Lastly, according to the text a complex number actually looks like two numbers separated by a space. (i.e. 3.5 5.0 rather than just 3.5). If I add two more scanner inputs for the second halves of both of complex numbers, I will have to change my code. Will I have to create new getters and setters to receive this number? Such as imaginaryPart2 and realPart2?

Thank you again for all of the help.

Upvotes: 1

Views: 4490

Answers (1)

Jan
Jan

Reputation: 13858

Some hints to compile errors:

    Complex real1 = realPart;
    Complex real2 = (Complex) realPart.clone();
    Complex imaginary1 = imaginaryPart;
    Complex imaginary2 = (Complex) imaginaryPart.clone();

    double realTotal = real1 + real2;
    double imaginaryTotal = imaginary1 + imaginary2;

Your member variables real1, 2, imaginary1,2 need to be of type double not Complex

Now it compiles - but it will not work. You want to add to Complex numbers after all. So rename your method signature:

public Complex add(Complex comp1, Complex comp2) {
    double real1 = comp1.getReal();
    double real2 = comp2.getReal();
    double imaginary1 = comp1.getImaginary();
    double imaginary2 = comp2.getImaginary();

    return new Complex(real1+real2, imaginary1 +imaginary2);
}

Now go ahead an implement multiplication likewise...

Upvotes: 1

Related Questions