/* * Intro to Programming CSE 1310 * University of Texas at Arlington */ package code7feb19extras; import java.io.File; import java.io.FileNotFoundException; import java.util.InputMismatchException; import java.util.Scanner; /** * * @author jcmtiernan */ public class Code7Feb19extras { /** * @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("gradeInputErr.txt"); Scanner userIn; boolean fileValid = true; try { userIn = new Scanner(inputFile); } catch (FileNotFoundException fnfe) //fnfe is a variable that will hold the exception info { userIn = 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 // # Ho would we calculate class average? Class minimum? Class maximum? while (userIn.hasNextInt()) { 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 { 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); 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; // nested if-else structure 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; } System.out.println("The input final project grade is "+project+"\n"); final40Pct = project * .4; //final40Pct = userIn.nextDouble() * .4; System.out.println("The project grade was "+project); } 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; } 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%"); } 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; } 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%"); } 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\n"); userIn.nextLine(); // # this moves to next line in input file } System.out.println("Grades were calculated for "+studentCount+" students."); } }