2015-12-12 41 views
1

不知道如果我這樣做的權利,但這是計劃的延續,我在這裏工作... Homework Help PT1作業幫助P t2中(數學Complex類)

我掙扎了很多與這個家庭作業...

**(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.** 

這是我到目前爲止。兩類...

// 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; 
    } 
} 

這裏是簡的幫助後,我更新的代碼。我已經創建了3種方法(減法,乘法和除法)。我不應該在每種方法中使用comp1和comp2,而是將它們分別命名爲?我們的目標是在最後同時打印每種方法的結果。這些有相同的名字會混淆嗎?

我也想知道什麼時候應該實現可複製的接口。

最後,根據文本,複數實際上看起來像是由空格分隔的兩個數字。 (即3.5 5.0而不是僅3.5)。如果我爲這兩個複數的後半部分再添加兩個掃描器輸入,我將不得不更改我的代碼。我需要創建新的獲取者和接受者來接收這個數字嗎?如imaginaryPart2和realPart2?

再次感謝您的所有幫助。

回答

0

一些專題來糾纏後:

變量的作用域

參數傳遞給方法只有在整個該方法是可見的。因此,爲您的每個方法命名您的兩個操作數comp1comp2完全沒問題。

但是:

面向對象

你的方法應該只有一個參數。假設您有一個名爲x的複雜實例。你想添加到另一個名爲y的實例。然後給定您的代碼,任何x.add(x,y)y.add(x,y)甚至z.add(x, y)的操作都會得到相同的結果。

所以:放下你的一個參數。您可能想要添加空檢查。

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

現在你可以寫

Complex z = x.add(y); 

getter和setter

當你的加/減/除法/乘法運算都返回一個複數,你可能想使Contex 不可變 - 即:不提供setter。複數可以通過構造函數創建。您可以通過調用現有的計算來獲得新的複數。但你不能更改一個號碼。

所以我的建議:刪除setter。

複數

而不是閱讀double S的輸入,你可能要考慮讀一String和匹配正則表達式的字符串。你可以在主體中使用它作爲實用程序方法,甚至可以作爲Complex的一個構造函數,從而允許使用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的是你添加到您的類聲明的接口:

public class Complex implements Cloneable { 

另外,你應該實現一個clone()方法:

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

的toString()

,一個0虛部中的字符串輸出被排除你的分配請求。所以你可能想再次檢查。這應該是一個簡單的if()