(Solved) : Input 80 50 10 0 Seventy 100 Output Invalid Grade Grade 0 100 Invalid Grade Grade Integer Q35784057 . . .


input:
80
50
-10
0
Seventy
100
output:
Invalid grade. Grade should be between 0 and 100.
Invalid grade. Grade should be an integer.
80 50 0 100
Average: 57.5
Gradebook grades : ArrayList<Integer> +Gradebook(0) + Gradebook(grades: int[) +addGrade(grade: int)void +getAverage double +toString(): String // Getters and Setters InvalidGradeException message : String +InvalidGradeException() InvalidGradeException(message: String) +getMessage) String In this lab, you will be creating a Gradebook class which stores grades in an ArrayList of integers Gradebook class The default (no-parameter) constructor should just initialize the ArrayList. The other constructor takes an existing ArrayList as a parameter and assigns it to the ArrayList inside the Gradebook class: this.grades- grades The class has a method to add a grade to the list called addGrade(). This method takes arn integer a grade as a parameter, and adds it to the ArrayList. Before adding it to the list, this method checks if the grade is valid. A valid grade is a grade between 0 and 100, inclusive. So grades like -5 or 105 are invalid, but 0 and 100 are valid. If the grade is invalid, throw a new InvalidGradeExeption The class also has a method to calculate the average of all those grades in the ArrayList called getAverage(). It returns the average as double InvalidGradeException class This class extends the Exception class provided by Java It has a private String message variable holding the message “Invalid grade. Grade should be between 0 and 100.” The default (no-parameter) constructor should call the superclass constructor super() The other constructor takes a String a message as a parameter and calls the superclass constructor with the same parameter super(message) Also, provide a method getMessage() that returns the message as a String, so that you use it in the try-catch statements in the Tester class. Example try // some code catch (InvalidGradeException e){ System.out.println(e.getMessage0): This way, you fully control the output of the exception/error message Tester class Write a Tester class that will Initialize a Gradebook object with the default constructor. Read grades from grades.txt which should be placed in the eclipse project, outside the src folder. Note that grades.txt contains one grade on each line Use BufferedReader with FileReader . . After reading each line, use Integer.parselnt() to convert the String to an int. . Add the integer grade to the gradebook using addGrade( Surround this method by a try-catch block, trying to catch the InvalidGradeException. Also, catch the NumberFormatException that might arise if Integer.parselnt) was given a String that could not be converted to an integer. If you catch the latter exception, print “Invalid grade Grade should be an integer.”. At the end, print the gradebook using the toString() method Then print the result of getAverage() on another line. . . Sample Input (grades.txt) 80 50 -10 0 Seventy 100 Sample Output Invalid grade. Grade should be between 0 and 100 Invalid grade. Grade should be an integer 80 50 0 100 Average: 57.5 Notes Notice how the printed grades do not include the negative grade and the String “seventy”. This is because the addGrade) method does not accept negative grades, and the Integer.parselnt() can’t convert “seventy” to an integer. Both will throw exceptions Also, notice how the program did not stop running after the exceptions were thrown by “-10” and “seventy”. This is because you catch the exception using try-catch statements, to prevent the program from crashing Show transcribed image text
Expert Answer
Answer to Input 80 50 10 0 Seventy 100 Output Invalid Grade Grade 0 100 Invalid Grade Grade Integer Q35784057 . . .
OR