Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

JAVA package code; import user_interface.CalculatorUI; public class Calculator {

ID: 3677130 • Letter: J

Question

JAVA

package code;

import user_interface.CalculatorUI;

public class Calculator {
  
   private int _value;
   private CalculatorUI _ui;
  
   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 * 20 + 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 + 8;
       _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;
   }
   public void double_keyPressed() {
       _value = _value * 2;
       _ui.updateDisplay();
   }
   public void half_keyPressed() {
       _value = _value / 2;
       _ui.updateDisplay();
   }
   public void square_keyPressed() {
       _value = _value + _value;
       _ui.updateDisplay();
   }
   public void clear_keyPressed() {
       _value = -1;
       _ui.updateDisplay();
   }
   public void negate_keyPressed() {
       _value = _value;
       _ui.updateDisplay();
   }

   public int getValue() {
       return 0;
   }

}

Please help to fix the follow bugs in above code

The following hint(s) may help you locate some ways in which your solution may be improved:

After pressing '3' then '4' the value of the calculator should be 34, but it was 0.

After pressing '4' then '3' the value of the calculator should be 43, but it was 0.

After pressing '9' the value of the calculator should be 9, but it was 0.

After pressing '8' the value of the calculator should be 8, but it was 0.

After pressing '7' the value of the calculator should be 7, but it was 0.

After pressing '6' the value of the calculator should be 6, but it was 0.

After pressing '4' then '6' the value of the calculator should be 46, but it was 0.

After pressing '6' then '4' the value of the calculator should be 64, but it was 0.

After pressing '6' then '4' then 'x*2' the value of the calculator should be 128, but it was 0.

After pressing '6' then '4' then 'x/2' the value of the calculator should be 32, but it was 0.

After pressing '6' then '5' then 'x/2' the value of the calculator should be 32, but it was 0.

After pressing '8' then 'x^2' (square) the value of the calculator should be 64, but it was 0.

After pressing '6' then '4' then 'negate' the value of the calculator should be -64, but it was 0.

After pressing '6' then '4' then 'negate' and then 'negate' again the value of the calculator should be 64, but it was 0.

After pressing '5' the value of the calculator should be 5, but it was 0.

After pressing '4' the value of the calculator should be 4, but it was 0.

After pressing '1' the value of the calculator should be 1, but it was 0.

After pressing '2' the value of the calculator should be 2, but it was 0.

After pressing '3' the value of the calculator should be 3, but it was 0.

Explanation / Answer

In the given code, the following bugs are found.

1)

public void digit_3_keyPressed() {
       _value = _value * 20 + 3;
       _ui.updateDisplay();
   }

-> when the key '3' is pressed the value should be 'value*10 + 3' but in your code '_value = _value * 20 + 3; ' .change this line of code as '_value = _value * 10 + 3;'

2)

public void digit_6_keyPressed() {
       _value = _value * 10 + 8;
       _ui.updateDisplay();
   }

-> when the key '6' is pressed the value should be 'value*10 + 6' but in your code '_value = _value * 10 + 8; ' .change this line of code as '_value = _value * 10 + 6;'

3)

public void digit_9_keyPressed() {
       _value = _value * 10 + 9;
   }

-> you have not updated the value to display when key '9' is pressed.

so change above code as :

public void digit_9_keyPressed() {
       _value = _value * 10 + 9;

_ui.updateDisplay();

}

4)

  public void square_keyPressed() {
       _value = _value + _value;
       _ui.updateDisplay();
   }

-> in the above method when square key pressed you have to get square value not double of the value.

so change above code as :

  public void square_keyPressed() {
       _value = _value * _value;
       _ui.updateDisplay();
   }

5)

public void clear_keyPressed() {
       _value = -1;
       _ui.updateDisplay();
   }

-> after pressing clear key the value should be default to zero.

so change above code as :

public void clear_keyPressed() {
       _value = 0;
       _ui.updateDisplay();
   }

6)

public void negate_keyPressed() {
       _value = _value;
       _ui.updateDisplay();
   }

-> in the above method negative key pressed negative value should be displayed.

so change your code to display as negative numbers when negative key pressed as shown below.

public void negate_keyPressed() {
       _value =-1*( _value);
       _ui.updateDisplay();
   }

7)

public int getValue() {
       return 0;
   }

-> in the above method you are not returning the updated value, you are returning the 'zero' value. so change above method as shown below.

public int getValue() {
       return _value;
   }

you have send some of the code to me.

in the above code some of the methods are available to me. update.display(); and main() methods not available to me. may be in the remaining code some bugs are needed to fix. 7 bugs are fixed in the above code.