Site Navigation

Site Search

CS 111: Lab 5

Learning Objectives

After completion of this lab, you should be able to

Create a jGRASP project with 2 Java class files

lab5 uml

  1. The driver should start up jGRASP and create a New Standard Project saved at U:\labs\lab5\
    • Note: Use jGRASP's Project > New menu item, not the File menu, to create a new project

  2. Create two new Java files (File > New > Java) and save as

  3. Add the files to your lab5 project

Create a TestScores class

lab5 class diagram

Based loosely on Chapter 10, Programming Challenge 1 in your textbook

Write a class named TestScores based on the following UML class diagram.

Unit test TestScores class

Compile and unit test the TestScores class

lab5 unit test 1

Write a main method that uses your TestScores class

Write a main method in 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.

   double[] userInput = new double[5];
   Scanner keyboard = new Scanner(;

// Read in five scores from the user
   System.out.print("Enter 5 scores: ");
   for (int index = 0; index < userInput.length; index++)
      userInput[index] = keyboard.nextDouble();

// Create the TestScores object and display the values and mean
   TestScores exam = new TestScores(userInput);
   System.out.println("The scores are : " + exam);  
   System.out.println("Their average is " + exam.getAverage());

Lab5Demo Run I/O Sample Session
lab5 sample session 1

Throw an IllegalArgumentException in class TestScores

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 IllegalArgumentException

Compile and unit test the modified TestScores class.

TestScores unit test
lab5 unit test 2

Use a try..catch block in a main method

Modify the main method of by placing the TestScores instantiation and method calls inside a try..catch block.

If the 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
lab5 unit test 3

Lab5Demo Run I/O Sample Session - Test score greater than 100
lab5 unit test 4

Lab5Demo Run I/O Sample Session - Valid test score values
lab5 unit test 5

Use a Boolean variable to control the flow of your program

Modify the logic in your main method to keep prompting the user to enter the test scores until they enter 5 valid scores.

lab5unittest6 (11K)

Handle multiple exceptions

Scanner.nextDouble throws an InputMismatchException exception if the input scanned cannot be processed into a valid double value by nextDouble(). Note: class InputMismatchException is included in package java.util.InputMismatchException

   import 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.

lab5unittest7 (11K)

Note: you need to use the Scanner method nextLine() to skip over invalid data if the InputMismatchException is thrown.

Perform unit testing

4 Unit Tests
TestScores t = new TestScores(new double[]{75.0, 98.5, 55.8})
getAverage()76.4333toString()"75.0 98.5 55.8 "
TestScores t = new TestScores(new double[]{75.0, 98.5, 101.7, 55.8})
IllegalArgumentException thrown getMessage()"Invalid score: 101.7"
TestScores t = new TestScores(new double[]{75.0, -9.5, 99.7, 55.8})
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: Check with your grader or instructor for help setting up Junit with jGRASP if you want to try unit testing from your account.

To Receive Credit

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.

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.