public class Calculator { private double register; //holds temporary results pri
ID: 3640345 • Letter: P
Question
public class Calculator {
private double register; //holds temporary results
private static int NUM_OP;
//default constructor
public Calculator()
{
register = 0;
}
public void clear() //clears register
{
register = 0;
}
public void set(double x) //sets the value of the register
{
register = x;
}
public double getResult()
{
return register;
}
public void add(double x) //add a value to the register
{
register += x;
NUM_OP ++;
}
public void printResult()
{
System.out.println("Result = " + register);
}
public static void main(String[] args)
{
Calculator cal = new Calculator();
//add 3 and 5 and print result
cal.set(3);
cal.add(5);
cal.printResult();
}
}
/* TODO:
1. Define a constructor to initialize register with a user-defined value
2. Define subtract, multiply, and divide methods
3. Define a method to copy one Calculator result to another
4. Define a private static variable "NUM_OP" to store the total number of operations (add, subtract, multiply or divide) --
5. Define a static method GetNumOP to return the value of NUM_OP
6. Write necessary sequence of statements in the main method to do this math with "cal" object and print the result:
(10 + 5) * 100 / 50
7. Create another calculator object "cal2" and do the math with "cal2":
120 / 50
8. Print the total number of operations done in Calculators "cal" and "cal2".
9. (Bonus point) Print sum of the register values stored in cal and cal2 objects.
Explanation / Answer
please rate - thanks
message me if any problems. I will fix them
public class Calculator {
private double register; //holds temporary results
private static int NUM_OP=0;
//default constructor
public Calculator()
{
register = 0;
}
public void clear() //clears register
{
register = 0;
}
public void set(double x) //sets the value of the register
{
register = x;
}
public double getResult()
{
return register;
}
public static int GetNumOP()
{
return NUM_OP;
}
public void add(double x) //add a value to the register
{
register += x;
NUM_OP ++;
}
public void sub(double x) //add a value to the register
{
register -= x;
NUM_OP ++;
}
public void mult(double x) //add a value to the register
{
register *= x;
NUM_OP ++;
}
public void div(double x) //add a value to the register
{
register /= x;
NUM_OP ++;
}
public void printResult()
{
System.out.println("Result = " + register);
}
public static void main(String[] args)
{
Calculator cal = new Calculator();
/*
//add 3 and 5 and print result
cal.set(3);
cal.add(5);
cal.printResult();
*/
//(10 + 5) * 100 / 50
cal.set(10);
cal.add(5);
cal.mult(100);
cal.div(50);
cal.printResult();
//120 / 50
Calculator cal2 = new Calculator();
cal2.set(120);
cal2.div(50);
cal2.printResult();
System.out.println("Total operations done: "+GetNumOP());
System.out.println("sum of the register values stored in cal and cal2: "+(cal.getResult()+cal2.getResult()));
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.