Lab 11: switch


Due Friday, October 28 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 Months.java

Download the Months.java file, and open it in jGrasp (or a text editor of your choice). The program will ask for an integer between 0 and 11, inclusive. It will then print out which month corresponds to the input integer, or else the message “Invalid month: MONTH”, where “MONTH” represents the integer the user input. Integers are mapped to months in the following manner:

Month Integer ID
January 0
February 1
March 2
April 3
May 4
June 5
July 6
August 7
September 8
October 9
November 10
December 11

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

Enter month (0-11): 9
October

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

Enter month (0-11): 4
May

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

Enter month (0-11): -1
Invalid month: -1

Step 2: Edit MonthsOnAndAfter.java

Download the MonthsOnAndAfter.java file, and open it in jGrasp (or a text editor of your choice). This program works similarly to the one you wrote in the previous step, except that it also prints the months that come after the given month. Example output of the program is shown below, with user input shown in bold:

Enter month (0-11): 0
January
February
March
April
May
June
July
August
September
October
November
December

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

Enter month (0-11): 9
October
November
December

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

Enter month (0-11): 4
May
June
July
August
September
October
November
December

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

Enter month (0-11): 12
Invalid month: 12

Step 3: Edit Compass.java

Download the Compass.java file, and open it in jGrasp (or a text editor of your choice). This program will randomly print out a compass direction, given a seed value for produce a random number with java.util.Random. Compass directions are mapped to integers according to the following table:

Compass Direction Integer ID
North 0
Northeast 1
East 2
Southeast 3
South 4
Southwest 5
West 6
Northwest 7

Further details are in the comments of Compass.java.

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

Download the CompassTest.java file, being sure to put it in the same folder/directory as your Compass.java file. This file contains tests for the code 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: Turn in Your Solution Using Canvas

Log into Canvas, and go to the COMP 110 class. Click “Assignments” on the left pane, then click “Lab 11”. From here, you can upload your answers and your code. Specifically, you must turn in the following four 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 four files.

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