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

I dont know where to start with this java program? The Calculator class has 1 pr

ID: 3565013 • Letter: I

Question

I dont know where to start with this java program?

The Calculator class has 1 private instance variable, number, a double, which stores the current calculated number.
It has one constructor which takes 1 parameter and sets number to it.
It has the following methods which each take one double parameter: add, subtract, multiply, divide.
Each of these methods will perform their operation, using the current number as the first operand and the parameter as the second operand.
(For example, if the current number is 10 and divide(5) is called, it will perform 10.0/5.0 and the current number will become 2.0.)
Each of these methods also returns the new current number.
(In the example above, divide(5) would return 2.)
It has the methods getNumber and setNumber.
It has a method clear, which has no parameters and no return value, and which sets number to 0.

Write a tester class named CalculatorClient. Use the Scanner and println() for input and output. Do not use GUI i/o. It should allow the user to input each of the 4 operations (+, -, *, /) as well as C for clear, and numbers. It should display the current number after each operation. This client class should not perform any math

Explanation / Answer


import java.util.Scanner;

class Calculator {
double number ;
// Constructor which takes 1 parameter and sets number to it.
public Calculator(double num)
{
this.number=num;
}
// Calculator class methods :add
public double add(double d){
number=number+d;
return number;
}
// Calculator class methods:substract
public double subtract(double d){
number=number-d;
return number;
}
// Calculator class methods : multiply
public double multiply(double d){
number=number*d;
return number;
}
// Calculator class methods : divide
public double divide(double d){
number=number/d;
return number;
}
// Calculator class methods : getNumber
public double getNumber(){
return number;
}
// Calculator class methods : setNumber Note it is void and dont return anything
public void setNumber(double d){
number=d;
}
// Calculator class methods : setNumber Note it is void and dont return anything
public void clear(){
number=0;
}
}
public class CalculatorClient {

  
public static void main(String[] args) {
String operator;
double num1,num2;
Scanner in = new Scanner(System.in);
  
// Calculator class object
Calculator cal =new Calculator(1);
System.out.println("Enter the operator : +, -, *, / ,C");
operator=in.next();
System.out.println("Enter the first input :");
num1=in.nextDouble();
// Set the value of current number as calculator
cal.setNumber(num1);
System.out.println("Enter the second input :");
num2=in.nextDouble();
//Checking operator value and calling function according to operator
if(operator.equals("+"))
cal.add(num2);
else if(operator.equals("-"))
cal.subtract(num2);
else if(operator.equals("*"))
cal.multiply(num2);
else if(operator.equals("/"))
cal.divide(num2);
else if(operator.equals("C"))
cal.clear();
  
//Display the current number after each operation
System.out.println(num1+operator+num2+"="+cal.getNumber());
  
  
}
}