Notes Make sure to follow all rules concerning programming a

 Notes:     Make sure to follow all rules concerning programming assignments found on    codingStandardHandout on the Blackboard.        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 

Solution

import java.util.Scanner;
import java.io.*;
public class Calc
{
public static void main(String[] args)
{
try
{
// Declare all needed constants as
final char OP1 = \'+\';
final char OP2 = \'-\';
final char OP3 = \'*\';
final char OP4 = \'/\';
final int SENT = -999;
char c;   
//Scanner scan = new Scanner(System.in);
  
// Declare all variables for
int num1 = 0;
int num2 = 0;
int min = 0;
int max = 0;
int n=0;
int result=0;
  
File file = new File(\"calc.txt\");
Scanner in = new Scanner(file);

// Start the sentinel loop
while(in.hasNextLine())
{
if(in.hasNextInt()) num1=in.nextInt();
else
{
System.out.println(\"Invalid Operand\");
break;
}
if(num1==SENT) break;
  
// Read in the operator and convert it to a char using scan.next().charAt(0);
c=in.next().charAt(0);
if(in.hasNextInt()) num2=in.nextInt();
else
{
System.out.println(\"Invalid Operand\");
break;
}

if(c==OP1)
{
result=num1+num2;
  
}
else if(c==OP2)
result=num1-num2;
else if(c==OP3)
result=num1*num2;
else if(c==OP4)
{
if(num2==0)
{
System.out.println(\"Division by zero\");
break;
}
else
result=num1/num2;
}

n++;
if(n==1 || result <min) min=result;
if(result > max) max=result;
System.out.println(\"\ Operation: \"+num1+\" \"+c+\" \"+num2);   
System.out.println(\"Result: \"+result);

System.out.println(\"\ Debug Information:\ \\tNumber of lines read = \"+n+\"\ \\tCurrent max= \"+max+\"\ \\tCurrent Min \"+min);




  

// 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]

}
catch(Exception e)
{
System.out.println(e);
}
} // End of main method
} // End of Calc class

 Notes: Make sure to follow all rules concerning programming assignments found on codingStandardHandout on the Blackboard. Make sure to call your program Calc.j
 Notes: Make sure to follow all rules concerning programming assignments found on codingStandardHandout on the Blackboard. Make sure to call your program Calc.j

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site