My Professor創建了需要修改的代碼。唯一的問題是我自己並不理解他的風格,因爲我自己是一個相當新的程序員。分配的參數如下:
•修改setter以便忽略不合適的值(即除以零)
•實現從頂級Object類繼承的equals()方法
•實現小於比方法
•實現加,減,乘方法
修改分數類和測試驅動程序JAVA
•確保equals方法返回的任何兩個部分是算術等於真大。
•確保equals方法不會改變所比較分數的值。
•lessThan和greaterThan方法每個都必須返回一個布爾值,而不是一個字符串。
•提供的減少方法返回一個新的(減少)分數對象作爲其函數值
我完全失去了這個任務,因爲我沒有絲毫的線索在哪裏甚至開始做。任何和所有的幫助將不勝感激!我有這樣的感覺,一旦我看到它完成,這對我來說都是有意義的。我根本不習慣這種教學風格。
public class Fraction {
private int numer;
\t private int denom;
\t public Fraction() { // no-arg constructor
\t \t numer = 0;
\t denom = 1;
\t }
\t public Fraction(int numer, int denom) {
this.numer = numer;
\t this.denom = denom;
}
\t public Fraction(Fraction frac) { // copy constructor
\t numer = frac.getNumer();
\t denom = frac.getDenom();
\t }
\t // getters and setters
\t public int getNumer() {
\t return numer;
\t }
\t public void setNumer(int x) {
\t numer = x;
\t }
public int getDenom() {
\t return denom;
\t }
\t public void setDenom(int x) {
denom = x;
\t }
\t // Special Methods
\t public String toString() {
\t return numer + "/" + denom;
\t }
\t // Other Methods
\t public Fraction reduce() {
\t Fraction temp = new Fraction();
\t int GCD = gcd(numer, denom);
\t temp.setNumer(numer/GCD);
\t temp.setDenom(denom/GCD);
\t return temp;
\t }
\t
\t // Private Methods
\t private int gcd(int n1, int n2)
\t {
\t int M, N, R;
\t if (n1 < n2)
\t {
\t \t N = n1;
\t \t M = n2;
\t }
\t else
\t {
\t \t N = n2;
\t \t M = n1;
\t }
\t R = M % N;
\t while (R != 0)
\t {
\t \t M = N;
\t \t N = R;
\t \t R = M % N;
\t }
\t return N;
}
public static void main(String[] args) {
// test constructors
\t Fraction frac0 = new Fraction();
\t System.out.println("TESTING NO-ARG CONSTRUCTOR");
\t \t System.out.println("frac0: Result should be 0/1:");
\t System.out.println("Numer = " + frac0.getNumer());
\t System.out.println("Denom = " + frac0.getDenom());
\t System.out.println("TESTING int/int CONSTRUCTOR");
\t Fraction frac1 = new Fraction(2,4);
\t \t System.out.println("frac1: Result should be 2/4:");
\t System.out.println("Numer = " + frac1.getNumer());
\t System.out.println("Denom = " + frac1.getDenom());
\t System.out.println("TESTING Fraction CONSTRUCTOR");
\t Fraction frac2 = new Fraction(frac1);
\t \t System.out.println("frac2: Result should be 2/4:");
\t System.out.println("Numer = " + frac2.getNumer());
\t System.out.println("Denom = " + frac2.getDenom());
\t \t System.out.println("TESTING COPY CONSTRUCTOR frac1 frac2");
\t if (frac1.getNumer() == frac2.getNumer() &&
\t \t frac1.getDenom() == frac2.getDenom() &&
\t \t frac1 != frac2)
\t {
\t \t System.out.println("Copy constructor working");
\t }
\t else
\t \t System.out.println("PROBLEM with copy constructor");
\t // test equal method
\t System.out.println("TESTING EQUALITY OF frac1 and frac2 -");
\t \t System.out.println("SHOULD BE FOUND EQUAL:");
\t \t if (frac1.equals(frac2))
\t {
\t \t System.out.println("frac1 and frac2 found equal");
\t }
\t else
\t {
\t \t System.out.println("frac1 and frac2 NOT equal");
\t }
\t // test reduce method
\t System.out.println("TESTING reduce METHOD ON frac1");
\t \t Fraction reduced_frac1 = frac1.reduce();
\t System.out.println("Reduced frac1 = " + reduced_frac1);
\t // test getters and setters
\t frac2.setNumer(8);
\t frac2.setDenom(12);
\t System.out.println("Numer = " + frac2.getNumer());
\t System.out.println("Denom = " + frac2.getDenom());
\t // System.out.println("GCD of 2/4 = " + frac1.gcd(1,4));
\t }
\t //* TO BE COMPLETED *
}
僅供參考FYI - 代碼片段只能用Javascript運行。你在那裏有Java。 – Makoto 2014-09-22 17:25:57
所以你想讓我們做你的功課?你到目前爲止做了什麼? – home 2014-09-22 17:36:10