/* * Intro to Programming CSE 1310 * University of Texas at Arlington */ package code19feb19extras; import java.io.File; import java.io.FileNotFoundException; import java.util.InputMismatchException; import java.util.NoSuchElementException; import java.util.Scanner; /** * * @author jcmtiernan */ public class Code19Feb19extras { /** * @param args the command line arguments */ public static void main(String[] args) { // Choices /* Class has 3 tests worth 60% of final grade Final 40% Do a research project 40% OR Take two more tests for 40% of grade OR Write 8 lab reports for 40% of grade Assumption: all grades are out of 100 Algorithm for final grade: Average the 3 tests and mult by percentage (60) Find out how they are doing the final 40% if student chose the research project multiply project grade by 40% and save as final40Pct else if student chose two tests Average the 2 tests and mult by percentage (40) and save as final40Pct else if student chose 8 reports Average 8 reports and mult by percentage (40) and save as final40Pct // Final grade is 3 test value plus whichever they chose Final grade is 3 test value plus final40Pct */ /* SEE NOTES WITH LABEL # for new/interesting topics SEE gradeInputErr for types of invalid data In gradeInputErr, all data for single student is on single line */ // How do we enter grades for multiple students during the same run of the program? File inputFile = new File("gradeInputErrEst2.txt"); Scanner userInFile; boolean fileValid = true; try { userInFile = new Scanner(inputFile); } catch (FileNotFoundException fnfe) //fnfe is a variable that will hold the exception info { userInFile = new Scanner(System.in); System.out.println("-- Input file was not found. Enter the data as prompted. "); System.out.println(fnfe); // print the Exception error message fileValid = false; } //Scanner userIn = new Scanner(System.in); double test1=0, test2=0, test3=0; int studentCount = 0; // # keep a count of the students // # How would we calculate class average? Class minimum? Class maximum? // ^^ Find class max grade, class min grade and average double min = 101; double max = -1; double avg = 0; String lineIn; Scanner userIn; double estGrade = 0; boolean validData = true; while (userInFile.hasNextInt()) { validData = true; lineIn = userInFile.nextLine(); userIn = new Scanner(lineIn); // ^^** Added extra data on line of input file - student estimated grade // Will read in, then also print difference from actual with message // Use formatted output to print the floating point values // ^^** Modify nested if else with char response to be a switch // ^^## Find class max grade, class min grade and average // ^^## Read digit input as strings and convert // ^^## Conditional operator in output // ^^## Validate input range ex. (0 - 100) studentCount++; System.out.println("Student "+studentCount+": "); // # header for each student if (!fileValid) { System.out.println("Please enter the three test grades separated by spaces: "); } try // # error checking from input file data that could cause InputMismatchException { test1 = userIn.nextDouble(); test2 = userIn.nextDouble(); test3 = userIn.nextDouble(); } catch (InputMismatchException imme) // # imme is a variable that will hold the exception info { System.out.println("-- There was invalid test data in the file. Setting grades to 0"); System.out.println("-- "+imme); // # print the Exception error message test1 = 0; test2 = 0; test3 = 0; validData = false; } // ^^## Validate input range ex. (0 - 100) // This is hard coding the ranges and tests /* if (test1 > 110) { System.out.println("-- Test 1 value out of range high. Setting grade to 110"); test1 = 110; } if (test2 > 110) { System.out.println("-- Test 2 value out of range high. Setting grade to 110"); test2 = 110; } if (test3 > 110) { System.out.println("-- Test 3 value out of range high. Setting grade to 110"); test3 = 110; } if (test1 < 0) { System.out.println("-- Test 1 value out of range low. Setting grade to 0"); test1 = 0; } if (test2 < 0) { System.out.println("-- Test 2 value out of range low. Setting grade to 0"); test2 = 0; } if (test3 < 0) { System.out.println("-- Test 3 value out of range low. Setting grade to 0"); test3 = 0; } */ // using a method to validate test grades test1 = checkTestGrade(test1, 1, 110, 0); test2 = checkTestGrade(test2, 2, 110, 0); test3 = checkTestGrade(test3, 3, 110, 0); // Class has 3 tests worth 60% of final grade System.out.println("The input test grades are "+test1+", "+test2+", and "+test3+"\n"); double test60Pct = (test1 + test2+ test3)/3 * .6; System.out.println("The test average is "+test60Pct+" out of 60%"); // Find out how they are doing the final 40% if (!fileValid) { System.out.println("\nWhich choice did you make for the final 40% of your grade?"); System.out.println("A. Reseach project"); System.out.println("B. Two additional tests"); System.out.println("C. Eight reports"); System.out.println("Please enter A, B, or C for your choice:"); } char response = 'Z'; //String resp = ""; //resp = userIn.next(); //response = resp.charAt(0); // # Can this input from the file ever cause an error? [Special case is if file is empty] response = userIn.next().charAt(0); System.out.print("The entered choice is : "+response+"\n"); // we are NOT going to do input validation (but later...) // at this point response should have 'A' or 'B' or 'C' /* if student chose the research project multiply project grade by 40% and save as final40Pct else if student chose two tests Average the 2 tests and mult by percentage (40) and save as final40Pct else if student chose 8 reports Average 8 reports and mult by percentage (40) and save as final40Pct */ double final40Pct = 0; // ^^ Modify nested if else with char response to be a switch // nested if-else structure switch (response) { case 'A': case 'a': //if ((response == 'A') || (response == 'a')) // if (test for project choice) { // true - statement to execute double project; if (!fileValid) System.out.println("Enter the project grade 0.0 - 100.0 "); // ^^## Read digit input as strings and convert String proj = userIn.next(); // read input as String try { project = Double.parseDouble(proj); // try to convert String to double } catch (NumberFormatException nfe) // error if String is not numeric { System.out.println("-- There was invalid project data in the file. Setting grades to 0"); System.out.println("-- "+nfe); // # print the Exception error message project = 0; userIn.next(); validData = false; } project = checkGrade(project, "Project", 1, 100, 0); /* try // # handling possible invalid numeric input { project = userIn.nextDouble(); } catch (InputMismatchException imme) //# imme is a variable that will hold the exception info { System.out.println("-- There was invalid project data in the file. Setting grades to 0"); System.out.println(imme); // # print the Exception error message project = 0; userIn.next(); } */ //System.out.println("The input final project grade is "+project+"\n"); System.out.printf("The input final project grade is %6.2e\n\n",project); final40Pct = project * .4; //final40Pct = userIn.nextDouble() * .4; System.out.println("The project grade was "+project); break; } case 'B': case 'b': // else if ((response == 'B')|| (response == 'b')) { double test4=0, test5=0; if (!fileValid) System.out.println("Please enter the final two test grades separated by spaces: "); try // # handling possible invalid numeric input { test4 = userIn.nextDouble(); test5 = userIn.nextDouble(); } catch (InputMismatchException imme) //# imme is a variable that will hold the exception info { System.out.println("-- There was invalid final test data in the file. Setting grades to 0"); System.out.println("-- "+imme); // # print the Exception error message test4 = 0; test5 = 0; userIn.next(); validData = false; } test4 = checkGrade(test4, "Test", 4,110, 0); //^^## validating test grade range test5 = checkGrade(test5, "Test", 5, 105, 0); System.out.println("The input final test grades are "+test4+" and "+test5+"\n"); final40Pct = (test4 + test5)/2 * .4; System.out.println("The final test average is "+final40Pct+" out of 40%"); break; } case 'C': case 'c': // else if ((response == 'C')|| (response == 'c')) { double rpt1=0, rpt2=0, rpt3=0, rpt4=0, rpt5=0, rpt6=0, rpt7=0, rpt8=0 ; if (!fileValid) System.out.println("Please enter the eight report grades separated by spaces: "); try // # handling possible invalid numeric input { rpt1 = userIn.nextDouble(); rpt2 = userIn.nextDouble(); rpt3 = userIn.nextDouble(); rpt4 = userIn.nextDouble(); rpt5 = userIn.nextDouble(); // # Note that there was an error in the data for a C choice rpt6 = userIn.nextDouble(); // # that does not get caught. Why? rpt7 = userIn.nextDouble(); // # HINT: How many lines in input file vs. how many students printed rpt8 = userIn.nextDouble(); } catch (InputMismatchException imme) // # imme is a variable that will hold the exception info { System.out.println("-- There was invalid reports data in the file. Setting grades to 0"); System.out.println("-- "+imme); // # print the Exception error message rpt1 = 0; rpt2 = 0; rpt3 = 0; rpt4 = 0; rpt5 = 0; rpt6 = 0; rpt7 = 0; rpt8 = 0; userIn.next(); validData = false; } catch (NoSuchElementException nsee) { System.out.println("-- Not enough report grades in the file"); System.out.println("-- "+nsee); rpt1 = 0; rpt2 = 0; rpt3 = 0; rpt4 = 0; rpt5 = 0; rpt6 = 0; rpt7 = 0; rpt8 = 0; validData = false; } rpt1 = checkGrade(rpt1, "Report", 1, 100, 0); rpt2 = checkGrade(rpt2, "Report", 2, 100, 0); rpt3 = checkGrade(rpt3, "Report", 3, 100, 0); rpt4 = checkGrade(rpt4, "Report", 4, 100, 0); rpt5 = checkGrade(rpt5, "Report", 5, 100, 0); rpt6 = checkGrade(rpt6, "Report", 6, 100, 0); rpt7 = checkGrade(rpt7, "Report", 7, 100, 0); rpt8 = checkGrade(rpt8, "Report", 8, 100, 0); System.out.println("The input report grades are "+rpt1+", "+rpt2+", "+rpt3+", "+rpt4+", "+rpt5+", "+rpt6+", "+rpt7+", and "+rpt8+"\n"); final40Pct = (rpt1 + rpt2 + rpt3 + rpt4 + rpt5 + rpt6 + rpt7 + rpt8)/8 * .4; System.out.println("The final report average is "+final40Pct+" out of 40%"); break; } default: //else // they did not enter A, B or C { System.out.println("-- Your choice *"+response+"* was not valid"); final40Pct = 0; validData = false; } } System.out.println("After user input final 40% is "+final40Pct); double courseGrade = test60Pct + final40Pct; // ^^## Conditional operator in output ( test ? true result : false result ) System.out.println("\n***The final course grade is "+courseGrade +" and all data is "+(validData?"":"not ")+"valid.\n"); avg += courseGrade; // sum all class grades to find average at end // ^^## Find class max grade and class min grades if (courseGrade > max) max = courseGrade; // find max grade if ((courseGrade < min) && (courseGrade != 0)) min = courseGrade; // find min grade that isn't 0 // ^^## Added extra data on line of input file - student estimated grade // Will read in, then also print difference from actual with message // Use formatted output to print the floating point values try { estGrade = userIn.nextDouble(); // reading estimated grade } catch (InputMismatchException imme) // input isn't a double { estGrade = 0; } catch (NoSuchElementException nsee) // no input left to read { estGrade = 0; } if ((courseGrade > estGrade) && (estGrade != 0)) { System.out.println("Whoo-hoo! You did better than you expected! "); } else if (courseGrade == estGrade) { System.out.println("You estimated your grade perfectly!"); } else if ((courseGrade < estGrade) && (estGrade != 0)) { System.out.println("Uh-oh. You did not do as well as you expected."); } else if (estGrade == 0) { System.out.println("No estimated grade was found\n\n"); } if (estGrade != 0) System.out.printf("Estimate: %6.2f\tActual: %6.2f\n\n\n",estGrade, courseGrade); } // end of while loop to read from file //System.out.println("Grades were calculated for "+studentCount+" students."); System.out.printf("Grades were calculated for %3d students.\n",studentCount); System.out.printf("%20s%6.2f\n","Class average: ",(avg/studentCount)); System.out.printf("%20s%6.2f\n","Maximum grade: ",max); System.out.printf("%20s%6.2f\n","Minimum grade: ",min); System.out.println(""); // tiny table System.out.printf("%-8s%10s%10s\n","Class","Maximum","Minimum"); System.out.printf("%-8s%10s%10s\n","Average","Grade","Grade"); System.out.printf("%-8.2f%10.2f%10.2f\n",avg, max, min); } public static double checkTestGrade(double test, int num, double max, double min) { if (test > max) { System.out.println("-- Test "+num+" value out of range high. Setting grade to "+max); test = max; } if (test < min) { System.out.println("-- Test "+num+" value out of range low. Setting grade to "+min); test = min; } return test; } public static double checkGrade(double test, String element, int num, double max, double min) { if (test > max) { System.out.println("-- "+element+" "+num+" value out of range high. Setting grade to "+max); test = max; } if (test < min) { System.out.println("-- "+element+" "+num+" value out of range low. Setting grade to "+min); test = min; } return test; } }