2012-08-03 41 views
-1
public class Operations { 

    private int add; 
    private int sub; 
    private int mul; 
    private int div; 
    private double sqrt; 

    public void setadd(int a, int b) { 
     add = a + b; 
    } 

    public void setsub(int a, int b) { 
     sub = a - b; 
    } 

    public void setmul(int a, int b) { 
     mul = a * b; 
    } 

    public void setdiv(int a, int b) { 
     div = a/b; 
    } 

    public void setsqrt(double sqt) { 
     sqrt = Math.sqrt(sqt); 
    } 

    public int getadd() { 
     return add; 
    } 

    public int getsub() { 
     return sub; 
    } 

    public int getmul() { 
     return mul; 
    } 

    public int getdiv() { 
     return div; 
    } 

    public double getsqrt() { 
     return sqrt; 
    } 

} 

我必須做一個這樣或Java的原型,這是沒有必要的,我怎麼在這裏使用靜態方法,而不是setter和getter ..我試圖做一個計算器..我的方法是否可以?靜態方法或setter或getters?

回答

2

讓所有的操作(加法,乘法,除法等)計算器的靜態方法類:

class Calculator{ 

    public static int add(int a, int b){ 
      return a+b; 
    } 
    ... 
2

我真的不明白設置和獲取的角度來看,爲什麼不把你的計算器是這樣的:

public class Calculator { 
public int add(int a, int b){ 
    return a + b; 
} 
public int sub(int a , int b){ 
    return a - b; 
} 
public int mul(int a, int b){ 
    return a * b; 
} 
public int div(int a, int b){ 
    return a/b; 
} 
public double sqrt(double sqt){ 
    return Math.sqrt(sqt); 
} 
+0

也可能是值得做這些靜態的;你並不需要一個Calculator對象的實例來完成這些操作。 – 2012-08-03 21:14:36

+0

我認爲即時消息只是在這裏做實踐,所有這些都只是學習和實驗 – user1535963 2012-08-03 21:18:53

+0

這些方法真的應該是靜態的 - 它們不在任何實例領域運行。 – 2012-08-03 21:19:01

0

只是回答沒有回答但問題的一部分:

你不需要在Java中的原型。

1

你的方法都是錯誤的,因爲你錯誤地模擬了你的操作。它不應該包含它的結果,它應該只做一個操作,而不是所有的操作。操作對象應該是不可變的,它應該給出給定兩個操作數的特定操作的答案。您應該將二元運算與一元運算分開。

interface BinaryOp { 
    double calculate(double left, double right); 
} 
interface UnaryOp { 
    double calculate(double operand); 
} 
private static final BinaryOp ADD = new BinaryOp() { 
    double calculate(double left, double right) { 
     return left + right; 
    } 
}; 
private static final BinaryOp SUB = new BinaryOp() { 
    double calculate(double left, double right) { 
     return left - right; 
    } 
}; 
private static final BinaryOp MUL = new BinaryOp() { 
    double calculate(double left, double right) { 
     return left * right; 
    } 
}; 
private static final BinaryOp DIV = new BinaryOp() { 
    double calculate(double left, double right) { 
     return left/right; 
    } 
}; 
private static final UnaryOp SQRT = new UnaryOp() { 
    double calculate(double operand) { 
     return Math.sqrt(operand); 
    } 
}; 

現在,您可以按名稱組織您的運營商:

private static final Map<String,BinaryOp> opByName = new HashMap<String,BinaryOp>(); 
static { 
    opByName.put("+", ADD); 
    opByName.put("-", SUB); 
    opByName.put("*", MUL); 
    opByName.put("/", DIV); 
} 

有了這個地圖,你可以用你的行動來執行計算你:

String op = "+"; 
double left = 123; 
double right = 456; 
double res = opByName.get(op).calculate(left, right); 
0

這看起來像一個良好用於枚舉或兩個:

enum BinOp { 
    ADD { 
     @Override 
     public int eval(int leftArg, int rightArg) { 
      return leftArg + rightArg; 
     } 
     @Override 
     public String symbol() { 
      return "+"; 
     } 
    }, 
    SUBTRACT { 
     @Override 
     public int eval(int leftArg, int rightArg) { 
      return leftArg - rightArg; 
     } 
     @Override 
     public String symbol() { 
      return "-"; 
     } 
    } 
    // etc. 
    ; 
    public abstract int eval(int leftArg, int rightArg); 
    public abstract String symbol(); 
} 

和一元運營商的類似枚舉(目前只有SQRT)。

如下,您可以用這些:

int left = 3; 
int right = 2; 
for (BinOp op : BinOp.values()) { 
    System.out.println("The value of " 
     + left + " " + op.symbol() + " " + right " is " 
     + op.eval(left, right) 
    ); 
}