After completion of this lab, you should be able to
Based loosely on Chapter 10, Programming Challenge 1 in your textbook
Write a class named
TestScores based on the following UML class diagram.
doubletest scores as its argument
Compile and unit test the TestScores class
Write a main method in
Lab5Demo.java that prompts the user to enter in five test scores and uses the values to create a
TestScores object. Use the object's methods to display the values stored and their averages.
To simplify input, assume there are exactly five valid double scores entered by the user from the keyboard.
Lab5Demo Run I/O Sample Session
Modify the constructor method in class
TestScores so that if any of the test scores passed in are negative or greater than 100, throw an
Compile and unit test the modified TestScores class.
TestScores unit test
Modify the main method of
Lab5Demo.java by placing the
TestScores instantiation and method calls inside a
TestScores constructor throws an
IllegalArgumentException, catch the exception in your main method and display the exception's error message.
Lab5Demo Run I/O Sample Session - Negative test score
Lab5Demo Run I/O Sample Session - Test score greater than 100
Lab5Demo Run I/O Sample Session - Valid test score values
Modify the logic in your main method to keep prompting the user to enter the test scores until they enter 5 valid scores.
Scanner.nextDouble throws an
InputMismatchException exception if the input scanned cannot be processed into a valid double value by nextDouble().
InputMismatchException is included in package java.util.InputMismatchException
Place the for loop that reads in five scores inside the
try..catch block and catch the
InputMismatchException. Display a simple error message to the user if they enter an illegal double value. Allow the user to correct their mistake.
Note: you need to use the Scanner method
nextLine() to skip over invalid data if the
InputMismatchException is thrown.
|getAverage()||76.4333||toString()||"75.0 98.5 55.8 "|
|IllegalArgumentException thrown||getMessage()||"Invalid score: 101.7"|
|IllegalArgumentException thrown||getMessage()||"Invalid score: -9.5"|
Unit test file: Your grader will use automated unit testing to test the correctness of your program. If you want to learn more, the unit test file is available for download: TestScoresTest.java. Check with your grader or instructor for help setting up Junit with jGRASP if you want to try unit testing from your account.
The lab assignment is due at the start of class on Friday
Save your work in your
U:\labs\lab5 folder. It will be graded directly online, there is nothing you need to submit for grading purposes.
IllegalArgumentExceptionthrown from TestScores constructor (2 points possible)
InputMismatchExceptionfor illegal double values (2 point possible)
No late CS 111 lab or programming assignments will be accepted. However, you are allowed to drop one lab and one programming assignment for the entire quarter. Students completing all labs and programming assignments will receive extra credit to offset points lost on other assignments.