Lab 7: Random Numbers and if...else if...else With Testing


Due Monday, September 25 at 11:59 PM

Goals for This Lab

By the time you have completed this work, you should be able to:

Provided files:

Step-by-Step Instructions

Step 1: Edit Guess.java

Download the Guess.java file, and open it in jGrasp (or a text editor of your choice). This program will ask the user to guess a number between 0 and 2 inclusive, and will report if the user's guess is the same as some number the computer chose. A caveat is that while the computer is choosing a “random” number, it always chooses 0 when run interactively, as it will always use the same “seed” value of 123456. Example output of the program is shown below, with user input shown in bold:

Guess a number between 0 and 2, inclusive: 1
Guess was incorrect.  The correct guess was: 0

Further example output of the program is shown below, with user input shown in bold:

Guess a number between 0 and 2, inclusive: 0
Guess is correct!

Step 2: Open GuessTest.java as a Test File

Download the GuessTest.java file, being sure to put it in the same folder/directory as your Guess.java file. This file contains a number of tests for the method you wrote in the previous step. Open this file in jGrasp as a test file, using the same instructions you've used in previous labs.

Run the tests in the file. Your code should pass all the tests. If your code does not pass all of the tests, edit your code in Guess.java until you can get all the tests to pass.

You do not need to write any of your tests in GuessTest.java.

Step 3: Edit CompareNums.java

Download the CompareNums.java file, and open it in jGrasp (or a text editor of your choice). You will need to implement a method that returns different Strings, depending on the input values the method receives. You may not modify the code in the main method. Example output of the program is shown below, with user input shown in bold:

Enter first integer: 1
Enter second integer: 2
The first integer is less than the second integer.

Further example output of the program is shown below, with user input shown in bold:

Enter first integer: 1
Enter second integer: 1
The first integer is equal to the second integer.

Further example output of the program is shown below, with user input shown in bold:

Enter first integer: 2
Enter second integer: 1
The first integer is greater than the second integer.

Step 4: Open CompareNumsTest.java as a Test File, and Edit It

Download the CompareNumsTest.java file, being sure to put it in the same folder/directory as your CompareNums.java file. This file contains a number of tests for the method you wrote in the previous step. Open this file in jGrasp as a test file, using the same instructions you've used in previous labs. You need to write a number of tests in this file, and all of them must pass. The comments in the file provide more details.

Step 5: Edit LetterGrade.java

Download the LetterGrade.java file, and open it in jGrasp (or a text editor of your choice). You will need to implement a method that takes a percentage between 0.0 and 100.0, and returns the appropriate letter grade as a String (e.g., “A+”, “A”, “A-”, etc.). The letter grade assignment should be based on the table in the second page of the syllabus. You may not modify the code in the main method. Example output of the program is shown below, with user input shown in bold:

Enter grade as percentage: 92.8
Letter grade: A

Further example output of the program is shown below, with user input shown in bold:

Enter grade as percentage: 79.4
Letter grade: C+

Step 6: Open LetterGradeTest.java as a Test File, and Edit It

Download the LetterGradeTest.java file, being sure to put it in the same folder/directory as your LetterGrade.java file. This file contains a number of tests for the method you wrote in the previous step. Open this file in jGrasp as a test file, using the same instructions you've used in previous labs. You need to write a number of tests in this file, and all of them must pass. The comments in the file provide more details.

Step 7: Turn in Your Solution Using Canvas

Log into Canvas, and go to the COMP 110L class. Click “Assignments” on the left pane, then click “Lab 7”. From here, you can upload your answers and your code. Specifically, you must turn in the following five files:

In addition, if you collaborated with anyone else, be sure to download collaborators.txt and write the names of the people you collaborated with in the file, one per line. Please submit this file along with the other five files.

You can turn in the assignment multiple times, but only the last version you submitted will be graded.