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

SIMULATING A CALCULATOR step1 ) create a class which stores an integer value. Th

ID: 3633472 • Letter: S

Question

SIMULATING A CALCULATOR

step1 ) create a class which stores an integer value. The constructor will initialize the value stored to a given number (parameter). Implement also the accessor (getValue) / mutator (setValue) methods to access / modify the content of the stored value. Test all your code (constructor and methods) in a file called TestCalculator.java.
The output of the program should look like (user input in bold).

Give an initial value for the stored value: 7
Test on constructor and getValue() method:
The value stored in the object is 7.
Test on setValue()method:
Give a new value for the stored value: -10
The new value stored in the object is -10.
Notes:

Step 2:) provide the services requested (calculating)
Provide the following methods to the class:
? public void add (int val): will add a given integer value to the one stored in the class.
? public void substract (int val): will subtract the given value to the one stored (note that a-b is different from b-a)
? public void multiply (int val): will multiply the given value to the one stored
? public void divide(int val): will divide the given value to the one stored.

Explanation / Answer

Hi, Here are both steps done: 1) Calculator.java ********************************************************************************************** /** * @(#)Calculator.java * * * @author * @version 1.00 2011/3/29 */ public class Calculator { int number; public Calculator(int n) { this.number = n; } public int getValue(){ return this.number; } public void setValue(int no){ this.number=no; } public void add(int val){ int sum = this.number + val; System.out.println("Sum of "+this.number+" & "+val+" is "+sum); } public void subtract(int val){ int sub = this.number - val; System.out.println("Subtract of "+this.number+" & "+val+" is "+sub); } public void multiply(int val){ int pro = this.number * val; System.out.println("Multiply of "+this.number+" & "+val+" is "+pro); } public void divide(int val){ int result = this.number/val; System.out.println("Divide of "+this.number+" & "+val+" is "+result); } } ********************************************************************************************** 2) TestCalculator.java ********************************************************************************************** /** * @(#)TestCaculator.java * * * @author * @version 1.00 2011/3/29 */ public class TestCaculator { public static void main(String[] args){ Calculator cal1 = new Calculator(7); System.out.println("Value stored in Variable is: "+cal1.getValue()); cal1.setValue(-10); System.out.println("New value stored in Variable is: "+cal1.getValue()); cal1.add(3); cal1.subtract(3); cal1.multiply(3); cal1.divide(3); } } ********************************************************************************************** Please compile both the class and run TestCalculator.java. and u ill get output as: ********************************************************************************************** --------------------Configuration: -------------------- Value stored in Variable is: 7 New value stored in Variable is: -10 Sum of -10 & 3 is -7 Subtract of -10 & 3 is -13 Multiply of -10 & 3 is -30 Divide of -10 & 3 is -3 Process completed. ********************************************************************************************** Thanks & Regards, Sachin