SHARE
SPREAD
HELP

The Tradition of Sharing

Help your friends and juniors by posting answers to the questions that you know. Also post questions that are not available.


To start with, Sr2Jr’s first step is to reduce the expenses related to education. To achieve this goal Sr2Jr organized the textbook’s question and answers. Sr2Jr is community based and need your support to fill the question and answers. The question and answers posted will be available free of cost to all.

 

#
Authors:
Walter Savitch ,kenrick Mock
Chapter:
Exception Handling
Exercise:
Programming Projects
Question:6 | ISBN:9780132830317 | Edition: 5

Question

Write a program that can serve as a simple calculator. This calculator keeps track of a single number (of type double ) that is called result and that starts out as 0.0. Each cycle allows the user to repeatedly add, subtract, multiply, or divide by a second number. The result of one of these operations becomes the new value of result. The calculation ends when the user enters the letter R for “result” (either in upper- or lowercase). The user is allowed to do another calculation from the beginning as often as desired.

The input format is shown in the following sample dialogue. If the user enters any operator symbol other than + , −, * , or / , then an UnknownOperatorException is thrown and the user is asked to reenter that line of input. Defining the class UnknownOperatorException is part of this project.

Calculator is on.

result = 0.0

+5

result + 5.0 = 5.0

new result = 5.0

* 2.2

result * 2.2 = 11.0

updated result = 11.0

% 10

% is an unknown operation.

Reenter, your last line:

* 0.1

result * 0.1 = 1.1

updated result = 1.1

r

Final result = 1.1

Again? (y/n)

yes

result = 0.0

+10

result + 10.0 = 10.0

new result = 10.0

/2

result / 2.0 = 5.0

updated result = 5.0

r

Final result = 5.0

Again? (y/n)

N

End of Program


TextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbook

Answer

import java.util.*;
public class SimpleCalculator
{
	// definition of performOperation method
	public static double performOperation(double value,
			String activity) throws UnknownOperatorException
	{

		//Get the first character of the String action 
		String arthOpter = activity.substring(0, 1);
		if(!arthOpter.equals("+") && !arthOpter.equals("-")
				&& !arthOpter.equals("*") && !arthOpter.equals("/")
				&& !arthOpter.equals("R") && !arthOpter.equals("r"))
			throw new UnknownOperatorException(arthOpter);

		if(arthOpter.equals("R") || arthOpter.equals("r"))
			return value;

		//Get all the characters exclude the first character of the String
		// action
		double number = Double.parseDouble(activity.substring(1,
				activity.length()));

		System.out.print("result " + arthOpter + " " + number
				+ " = ");
		if(arthOpter.equals("+"))
			return value += number;
		else if(arthOpter.equals("-"))
			return value -= number;
		else if(arthOpter.equals("*"))
			return value *= number;
		else
			return value /= number;
	}
	// main method
	public static void main(String args[])
	{
		// object for Scanner class
		@SuppressWarnings("resource")
		Scanner input = new Scanner(System.in);
		String decision = "y";
		System.out.println("Calculator is on.");

		//Check whether the first character of the string decision is y or Y
		while(decision.charAt(0) == 'y'|| decision.charAt(0) == 'Y')
		{
			int count=0;
			double result = 0.0;
			String arthOpter = "";
			System.out.println("result = " + result);
			// Check whether the string operator is r or R
			while(!arthOpter.equals("R") && !arthOpter.equals("r"))
			{
				arthOpter = input.nextLine();
				try
				{
					result = performOperation(result, arthOpter);
                                         count++;
					if(!arthOpter.equals("R")
							&& !arthOpter.equals("r"))
					{
						if(count==1)
						{
						System.out.println(result);
						System.out.println("New result = "+ result);
						}
						else
						{
							System.out.println(result);
							System.out.println("Updated result = "
                                                  + result);
						}
					}
				}
				catch(UnknownOperatorException e)
				{
					System.out.println(e.getMessage());
					System.out.println("Reenter, your last line.");
				}
			}
			System.out.println("Final result = " + result);
			System.out.println("Again? (y/n)");
			decision = input.nextLine();
		}
		System.out.println("End of Program.");
	}

}
public class UnknownOperatorException extends Exception
{
	// definition of UnknownOperatorException constructor
		public UnknownOperatorException(String operator)
		{
			super(operator + " is an unknown operation.");
		} 
}

Sample output:

Calculator is on.
result = 0.0
+5
result + 5.0 = 5.0
New result = 5.0
*2.2
result * 2.2 = 11.0
Updated result = 11.0
%10
% is an unknown operation.
Reenter, your last line.
*0.1
result * 0.1 = 1.1
Updated result = 1.1
r
Final result = 1.1
Again? (y/n)
yes
result = 0.0
+10
result + 10.0 = 10.0
New result = 10.0
/2
result / 2.0 = 5.0
Updated result = 5.0
r
Final result = 5.0
Again? (y/n)
N
End of Program.
 

0 0

Discussions

Post the discussion to improve the above solution.