For this code I have 4 classes Add,Multiply,Subtract and Divide. How can I make
ID: 3681795 • Letter: F
Question
For this code I have 4 classes Add,Multiply,Subtract and Divide. How can I make them into BinaryOperation in order to public void equal_keyPressed() can output the right number in 4 different operation. for now it can only output the right number in Add operation.
package part2.code;
import part1.code.Add;
import part2.operations.BinaryOperation;
import part2.user_interface.CalculatorUI;
public class Calculator {
private int _value;
private CalculatorUI _ui;
private BinaryOperation _bo;
private Add _addOperator;
private Subtract _subOperator;
private Multiply _mulOperator;
private Divide _divOperator;
public Calculator(CalculatorUI calculatorUI) {
_ui = calculatorUI;
_value = 0;
}
public void digit_0_keyPressed() {
_value = _value * 10 + 0;
_ui.updateDisplay();
}
public void digit_1_keyPressed() {
_value = _value * 10 + 1;
_ui.updateDisplay();
}
public void digit_2_keyPressed() {
_value = _value * 10 + 2;
_ui.updateDisplay();
}
public void digit_3_keyPressed() {
_value = _value * 10 + 3;
_ui.updateDisplay();
}
public void digit_4_keyPressed() {
_value = _value * 10 + 4;
_ui.updateDisplay();
}
public void digit_5_keyPressed() {
_value = _value * 10 + 5;
_ui.updateDisplay();
}
public void digit_6_keyPressed() {
_value = _value * 10 + 6;
_ui.updateDisplay();
}
public void digit_7_keyPressed() {
_value = _value * 10 + 7;
_ui.updateDisplay();
}
public void digit_8_keyPressed() {
_value = _value * 10 + 8;
_ui.updateDisplay();
}
public void digit_9_keyPressed() {
_value = _value * 10 + 9;
_ui.updateDisplay();
}
public void clear_keyPressed() {
_value = 0;
_ui.updateDisplay();
}
public int getValue() {
return _value;
}
public void divide_keyPressed() {
// TODO Define this method
_divOperator = new Divide(_value);
_value = 0;
}
public void multiply_keyPressed() {
// TODO Define this method
_mulOperator = new Multiply(_value);
_value = 0;
}
public void subtract_keyPressed() {
// TODO Define this method
_subOperator = new Subtract(_value);
_value = 0;
}
public void add_keyPressed() {
// TODO Define this method
_addOperator = new Add(_value);
_value = 0;
}
public void equal_keyPressed() {
// TODO Define this method
_value = _addOperator.perform(_value);
_ui.updateDisplay();
}
}
Explanation / Answer
In equal key pressed u have written add operator . perform so it is printing only add value . once check it . I think may be there is a name collision have occurred . keep the function name as add1 r anything otherthan add.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.