How can I do this on the code below: package part2.code; import part1.code.Add;
ID: 3681880 • Letter: H
Question
How can I do this on the code below:
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();
}
}
The other one is
package part2.operations;
public class Add implements BinaryOperation {
@Override
public int perform(int arg) {
// TODO Auto-generated method stub
return 0;
}
}
Explanation / Answer
package part2.operations;
public class Add implements BinaryOperation {
private int arg0;
public Add(int arg0){
this.arg0 = arg0;
}
@Override
public int perform(int arg) {
return this.arg0 + arg;
}
}
package part2.operations;
public class Subtract implements BinaryOperation {
private int arg0;
public Subtract(int arg0){
this.arg0 = arg0;
}
@Override
public int perform(int arg) {
return this.arg0 - arg;
}
}
package part2.operations;
public class Multiply implements BinaryOperation {
private int arg0;
public Multiply(int arg0){
this.arg0 = arg0;
}
@Override
public int perform(int arg) {
return this.arg0 * arg;
}
}
package part2.operations;
public class Divide implements BinaryOperation {
private int arg0;
public Divide(int arg0){
this.arg0 = arg0;
}
@Override
public int perform(int arg) {
return this.arg0 / arg;
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.