CS 111: Lab 5 main content
Student Learning Objectives
Demonstrate the ability to
- Use the jGRASP programming environment to create a new Java project at
u:\Lab5\ with two Java code files: TestScores.java and Driver.java.
- Throw an Exception in a method.
- Catch an Exception in your main method
Lab 5 Assignment
Do Chapter 10, Programming Challenge 1: TestScores Class
- Write a class named TestScores.
- The constructor method ahould accept an array of test scores (as
doubles) as its argument
- If any of the test scores passed in is negative or greater than 100, throw an
IllegalArgumentException
- Write a method named getAverage() that returns the average of the test scores
- Write a main method to allow the user to enter 5 test scores and calculate their average.
- Catch the exception and throw an error message if the constructor method throws an exception.
Sample Sessions
Enter 5 scores: 55.5 66.6 77.7 88.8 99.9
The average of the scores is 77.7
Enter 5 scores: 55.5 66.6 -15.5 88.8 99.9
Invalid score found -- Element: 2 Score: -15.5
Enter 5 scores: 55.5 66.6 77.7 88.8 110.1
Invalid score found -- Element: 4 Score: 110.1
To Receive Credit
- If you finish during Tuesday's lab, ask your TA to come over to check your program and give you credit.
Note: The TA will check your program on your computer. You do not need to print out the source code.
- If you are unable during Tuesday's lab, turn in the printouts of both Java files by the start of class on Wednesday.
Programming Challenge
If you finish early, get yourself checked off first!
As a programming challenge (not required but recommended):
Do Chapter 10, Programming Challenge 2: TestScores Class Custom Exception