Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Make sure to call your program Calc.java Specification: You are to write a simple calculator program called Calc, which does addition, subtraction, multiplication and division.

Make sure to call your program Calc.java Specification: You are to write a simple calculator program called Calc, which does addition, subtraction, multiplication and division. The data will be read from a file, not from the keyboard. To run the program, use the command java Calc < Calc.txt where Calc.txt is a file you have created that contains the Sample Input given below. The number of input lines can vary. The program must handle any number of input lines! You will read in the lines, one part at a time. Store each part into a variable and calculate the result. Each input line contains a basic arithmetic command of the following form: number operator number Sample Examples of input file: 13 + 4 2 * 3 4 - 1 5 / 5 -999 where -999 is sentinel value. Note that the numbers are all integers and there will be only one arithmetic operation per line. The program must use .hasNextInt() to check for the integer operands. If one of the operands entered is not an int the program will display an error message and terminate using System.exit(0). Look for the output message in Sample output. Your program must check for a request to divide by zero. If such a request is made, the program should print following error message and terminate execution. Divide by Zero. Quitting the program. You are to read in these lines using a sentinel loop, reading one line at a time until the sentinel value "-999" is reached. The sentinel loop was covered in Chapter 4.4. Use named constants OPERATOR1, OPERATOR2, OPERATOR3 and OPERATOR4 for four operators +, -, *, / respectively. You can treat the operators as char variables or as Strings. If you treat them as Strings, remember that you cannot compare two Strings using ==. If the operator is not one of the legal four operators (+, -, *, /), the program will print an error message and terminate. Look for the output message in Sample output. After the loop has finished, the program shall print the total number of lines that were read, and shall print "max" and "min", the largest and smallest results that have been calculated. In this assignment you must initialize max and min to the result of the first valid calculation; a request for division by zero is not valid. To do this you will need to use an "if" statement in order to initialize max and min to the calculated value when they have not yet been initialized and when division by zero is not being requested. See the Sample Output below. ================== Simple calculator ================== Operation: 13 + 4 Result : 17 Debug Information: Number of lines read = 1 Current max: 17 Current min: 17 Operation: 2 * 3 Result : 6 Debug Information: Number of lines read = 2 Current max: 17 Current min: 6 Operation: 4 - 1 Result : 3 Debug Information: Number of lines read = 3 Current max: 17 Current min: 3 Operation: 5 / 5 Result : 1 Debug Information: Number of lines read = 4 Current max: 17 Current min: 1 Number of lines read : 4 Maximum calculated value: 17 Minimum calculated value: 1 ==================================== Program Outline: //Calc.java //Programming Assignment 4 //Fall 2016 //CS 1113 //Trine University //Name //Program Description import java.util.Scanner; public class Calc { public static void main(String[] args) { // Declare all needed constants as final char OP1 = '+'; [Add code Here] // Declare sentinel variable SENT and set it as -999; [Add code Here] Scanner scan = new Scanner(System.in); // Declare all variables for int num1 = 0; // first number in operation // second number in operation // minimum result value of all operations // maximum result value of all operations // result of a given operation // number of lines read in // operator of a given operation (declare as character variable) // Print out the header information as shown in sample output [Add Code Here] // Verify and read in the first number and store in num1 using defensive programming. [Add Code Here] // Start the sentinel loop while(//variable that has value [Add Code Here] != SENT) { // Read in the operator and convert it to a char using scan.next().charAt(0); [Add Code Here] //op = scan.next().charAt(0); // this will read the value for operator and store in varaible op // Read in the second number and store in another variable [Add Code Here] // Figure out which arithmetic operation you need to perform such as //if operator is +, perform addion, if operator is -, perform substraction //and so on.... using if-else-if statements [Add Code Here] //for division, check if the second value is 0 or not //if it is 0, then print "Division by zero" and quit the program, //if not, perform the division operation` [Add Code Here] //if the operator is not +, -, *, and /, then print error message //"Not a valid operator." //"Quitting the program!!!" //and quit the program. [Add Code Here] //Calculate the number of lines read in. [Add Code Here] //Calculate min and max //if only one line is read, then both minimum and maximum value will be result //otherwise use Math.max() and Math.min() methods to calculate maximum and minimum values. if(numLines == 1) [Add Code Here] // Print out operation and result as shown in sample output [Add Code Here] //Use defensive programming to read in another value and stor in num1 //this will be the updating condition of while loop [Add Code Here] } // End of Sentinel loop // Print out the rest of the results as shown in sample output [Add Code Here] } // End of main method } // End of Calc class

please use

java(NetBeanIDE)

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

More Books

Students also viewed these Databases questions

Question

Explain the different types of marketing strategies.

Answered: 1 week ago

Question

Explain product positioning.

Answered: 1 week ago

Question

Explain Industrial market segment.

Answered: 1 week ago