Homework Help Pt2 (Math The Complex Class)

半腔热情 提交于 2019-12-08 06:56:58

问题


Not sure if I'm doing this right, but this is a continuation of the program I was working on here...Homework Help PT1

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.


回答1:


Some Topics to dwell upon:

Variable Scope

Parameters passed into a method are visible only throughout that method. So naming your two operands comp1 and comp2 for each and all of your methods is perfectly fine.

But:

Object Orientation

Your methods should only have one parameter. Say your have one instance of Complex named x. And you want to add to that another instance named y. Then given your code, any operation of x.add(x,y) and y.add(x,y) and even z.add(x, y) would yield the same results.

So: Drop one of your paramters. You might want to add nullchecks.

public Complex add(Complex toAdd) {
   return new Complex(this.realPart + toAdd.realPart,
        this.imaginaryPart + toAdd.imagineryPart); 
}

Now you can write

Complex z = x.add(y); 

Getters and Setters

As your add / subtract / divide / multiply operations all return a new Complex number, you might want to make Contex immutable - that is: Provide no setters. Complex number can be created through the constructors. You can get new Complex numbers by invoking calculations on existing ones. But you cannot change a number.

So my advice: Remove the setters.

Input of complex numbers

Instead of reading doubles, you might want to think about reading a String and match that string with a regular expression. You could use that as a utility method in your main or even as a consttructor for Complex, allowing to use a String as input.

Consider this method for matching String:

    Pattern complexFinder = Pattern.compile("(-?\\d+(\\.\\d*)?)?\\s*([-+]\\s*\\d+(\\.\\d*)?i)?");
    Matcher m = complexFinder.matcher(complexString);
    if (m.find()) {
        double realPart = 0;
        double imaginaryPart = 0;
        if (m.group(1) != null) {
            realPart = Double.parseDouble(m.group(1).replaceAll("\\s", ""));
        }
        if (m.group(3) != null) {
            imaginaryPart = Double.parseDouble(m.group(3).replaceAll("\\s", "").replace("i", ""));
        }
        Complex c = new Complex(realPart, imaginaryPart);
    }

Cloneable

Cloneable is an interface you add to your class declaration:

public class Complex implements Cloneable {

Additionally you should implement a clone() method:

public Object clone() {
   return super.clone();
}     

toString()

Your assignment requests that an 0 imaginary part be left out in String output. So you might want to check that again. This should be a simple if()



来源:https://stackoverflow.com/questions/34243287/homework-help-pt2-math-the-complex-class

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!