JAVA Programming Assignment 4 Fall 2016 Due by 115959 PM Fri
JAVA Programming Assignment 4 Fall 2016 Due by 11:59:59 PM Friday, October 14, 2016 Your output format should exactly match the sample output format including the messages to be printed. 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
D://calc.txt (Save this file under D Drive then the path of th efile pointing to it is D://calc.txt)
13 + 4
2 * 3
4 - 1
5 / 5
-999
__________________
Code:
import java.io.File;
import java.io.IOException;
import java.util.Scanner;
public class Calculator {
public static void main(String[] args) {
//Declaring constants
final String OPERATOR1=\"+\";
final String OPERATOR2=\"*\";
final String OPERATOR3=\"-\";
final String OPERATOR4=\"/\";
//Declaring variables
int count_lines=0,res,a,b,min=0,max = 0;
//Assigning the file name to the String variable
String fname = \"D://calc.txt\";
//Creating the Scanner class reference
Scanner sc=null;
String arr[]=null;
String line=null;
try{
//Opening the file
sc= new Scanner(new File(fname));
//This loops continue to execute until the end of the file
while (sc.hasNext()){
line=sc.nextLine();
if(!line.equalsIgnoreCase(\"-999\"))
{
//Counting no of lines
count_lines++;
System.out.println(\"\ Operation :\"+line);
arr=line.split(\" \");
a= Integer.parseInt(arr[0]);
b=Integer.parseInt(arr[2]);
/* If the operator is equal to \"+\"
* then perform addition operation
*/
if(arr[1].equals(OPERATOR1))
{
//calculating result.
res=a+b;
//Displaying result
System.out.println(\"Result : \"+res);
min=res;
max=res;
}
/* If the operator is equal to \"*\"
* then perform multiplication operation
*/
else if(arr[1].equals(OPERATOR2))
{
//calculating result.
res=a*b;
//Displaying result
System.out.println(\"Result : \"+res);
//Finding minimum an maximum value
if(res>max)
max=res;
if(res<min)
min=res;
}
/* If the operator is equal to \"-\"
* then perform subtraction operation
*/
else if(arr[1].equals(OPERATOR3))
{
//calculating result.
res=a-b;
//Displaying result
System.out.println(\"Result : \"+res);
//Finding minimum an maximum value
if(res>max)
max=res;
if(res<min)
min=res;
}
/* If the operator is equal to \"/\"
* then perform division operation
*/
else if(arr[1].equals(OPERATOR4))
{
//calculating result.
res=a/b;
//Displaying result
System.out.println(\"Result : \"+res);
//Finding minimum an maximum value
if(res>max)
max=res;
if(res<min)
min=res;
}
//Displaying debugging information
System.out.println(\"\ Debug Information :\");
System.out.println(\"Number of Lines read :\"+count_lines);
System.out.println(\"Current Max :\"+max);
System.out.println(\"Current Min :\"+min);
}
}
} catch (IOException e)
{
//Displaying the exception
System.out.println(\"Exception :\"+e);
}
System.out.println(\"\ Number of lines read :\"+count_lines);
System.out.println(\"Maximum calculated value :\"+max);
System.out.println(\"Minimum calculated value :\"+min);
}
}
_______________________________
Output:
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
______________________Thank You


