Java Reference
In-Depth Information
The methods implementing the arithmetic operations are similar: they
store in operand_2 the result and replace operand_1 with the default value
of Rational .
public class Calculator {
private Rational operand_1 # new Rational();
private Rational operand_2 # new Rational();
private Format format # new FixedPointFormat();
public void addOperand(String newOperand) throws
FormatException {
Rational previous # operand_2;
operand_2 # format.parse(newOperand);
// can throw exception
operand_1 # previous;
}
public void add()
operand_2 # operand_1.plus(operand_2);
operand_1 # new Rational();
}
public void subtract() {
operand_2 # operand_1.minus(operand_2);
operand_1 # new Rational();
}
public void multiply() {
operand_2 # operand_1.mul(operand_2);
operand_1 # new Rational();
}
public void divide() {
operand_2 # operand_1.div(operand_2);
operand_1 # new Rational();
}
public void delete() {
operand_2 # operand_1;
operand_1 # new Rational();
}
public String firstOperand(){
return format.toString(operand_1);
}
public String secondOperand(){
return format.toString(operand_2);
}
public void setFormat(Format newFormat){
format # newFormat;
}
public Format getFormat(){
return format;
}
}
Search WWH ::




Custom Search