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

Using the class Calculator to write an improve calculator that will have the fol

ID: 3553956 • Letter: U

Question

Using the class Calculator to write an improve calculator that will have the followig commands. 1) e for ending the program 2) c for clear; set result to zero 3) m for save in memory; set memory equal to result 4) r for recall memory; displays the value of memory but does change result.


the class calculator and the class for the two exceptions are the following



public class DivideByZeroException extends Exception

{


public DivideByZeroException()

{

super ("Dividing by Zero!");

}

public DivideByZeroException(String message)

{

super (message);

}

}



Explanation / Answer

I'm posting only the Calculator.java in which i made the changes

Look for "// __Change__#" comment lines for the addtitions i made


DO RAte if satisfied


import java.util.Scanner;

public class Calculator {


private double result;

//__Change__#1

private double memory;

private double precision = -0.0001;


public static void main(String[] args) {

Calculator clerk = new Calculator();


try {

System.out.println("Calculator is on.");

System.out.println("Format of each line: ");

System.out.println("operator space number");

System.out.println("For example: + 3");

System.out.println("To end, enter the letter e.");

System.out.println("To clear the result enter the letter c.");

System.out.println("To save a resul enter the letter m.");

System.out.println("To recall a the save result enter the letter r.");

clerk.doCalculation();

} catch (UnknownOpException e) {

clerk.handleUnknownOpException(e);

} catch (DivideByZeroException e) {

clerk.handleDivideByZeroException(e);

}

System.out.println("Calculator program ending.");

}


public Calculator() {

result = 0;

//__Change__#2

memory = 0;

}


public void reset() {

result = 0;

}


public void setResult(double newResult) {

result = newResult;

}


public double getResult() {

return result;

}


public double evaluate(char op, double n1, double n2) throws DivideByZeroException, UnknownOpException {

double answer;

switch (op) {

case '+':

answer = n1 + n2;

break;

case '-':

answer = n1 - n2;

break;

case '*':

answer = n1 * n2;

break;

case '/':

if ((-precision < n2) && (n2 < precision)) {

throw new DivideByZeroException();

}

answer = n1 / n2;

break;

default:

throw new UnknownOpException(op);

}

return answer;

}


public void doCalculation() throws DivideByZeroException, UnknownOpException {

Scanner keyboard = new Scanner(System.in);

boolean done = false;

result = 0;

System.out.println(" result = " + result);

while (!done) {

char nextOp = (keyboard.next()).charAt(0);

if ((nextOp == 'e') || (nextOp == 'E')) {

done = true;

}//__Change__#3

else if((nextOp == 'm') || (nextOp == 'M')) {

memory = result;

System.out.println("result stored in memory");

}else if((nextOp == 'c') || (nextOp == 'C')) {

result = 0.0;

System.out.println("result = 0.0");

}else if((nextOp == 'r') || (nextOp == 'R')) {

System.out.println("recalled memory Value = " + memory);

System.out.println("result = " + result);

}else {

double nextNumber = keyboard.nextDouble();

result = evaluate(nextOp, result, nextNumber);

System.out.println("result " + nextOp + " " + nextNumber + " = " + result);

System.out.println("updated result = " + result);

}

}

}


public void handleDivideByZeroException(DivideByZeroException e) {

System.out.println("Dividing by zero.");

System.out.println("Program aborted");

System.exit(0);

}


public void handleUnknownOpException(UnknownOpException e) {

System.out.println(e.getMessage());

System.out.println("Try again from the beginning: ");

try {

System.out.println("Calculator is on.");

System.out.println("Format of each line: ");

System.out.println("operator space number");

System.out.println("For example: + 3");

System.out.println("To end, enter the letter e.");

System.out.println("To clear the result enter the letter c.");

System.out.println("To save a resul enter the letter m.");

System.out.println("To recall a the save result enter the letter r.");

doCalculation();

} catch (UnknownOpException e2) {

System.out.println(e2.getMessage());

System.out.println("Try again at some other time.");

System.out.println("Program ending.");

System.exit(0);

} catch (DivideByZeroException e3) {

handleDivideByZeroException(e3);

}

}


public void setValueToZero() {

}


}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote