/* * Intro to Programming CSE 1310 * University of Texas at Arlington */ package code19feb19; import java.io.File; import java.io.FileNotFoundException; import java.util.InputMismatchException; import java.util.NoSuchElementException; import java.util.Scanner; /** * * @author jcmtiernan */ public class Code19Feb19 { /** * @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("gradeInputErrEst.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 and class min grades String lineIn; Scanner userIn; double estGrade = 0; while (userInFile.hasNextInt()) { 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 // ^^ Conditional operator in output? // ^^ Find class max grade and class min grades studentCount++; System.out.print("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; } //userIn.nextLine(); // 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 "); 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(); } 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 is 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(); } 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; } 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; } } System.out.println("After user input final 40% is "+final40Pct); double courseGrade = test60Pct + final40Pct; System.out.println("\n***The final course grade is "+courseGrade+"\n"); // ^^ 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(); } catch (InputMismatchException imme) // # imme is a variable that will hold the exception info { estGrade = 0; } catch (NoSuchElementException nsee) { estGrade = 0; } if ((courseGrade > estGrade) && (estGrade != 0)) { System.out.println("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); // ^^ Conditional operator in output? // ^^ Find class max grade and class min grades } //System.out.println("Grades were calculated for "+studentCount+" students."); System.out.printf("Grades were calculated for %5d students.\n",studentCount); } }