switch
Due Friday, September 29 at 11:59 PM
By the time you have completed this work, you should be able to:
switch
to write code with many int
-based conditionsswitch
Weekdays.java
Download the Weekdays.java
file, and open it in jGrasp (or a text editor of your choice).
The program will ask for an integer between 0
and 4
, inclusive.
It will then print out which weekday corresponds to the input integer, or else the message “Invalid day: DAY
”, where “DAY
” represents the integer they input.
Integers are mapped to weekdays in the following manner:
Weekday | Integer ID |
---|---|
Monday | 0 |
Tuesday | 1 |
Wednesday | 2 |
Thursday | 3 |
Friday | 4 |
Example output of the program is shown below, with user input shown in bold:
Enter day (0-4): 3 Thursday
Further example output of the program is shown below, with user input shown in bold:
Enter day (0-4): 5 Invalid day: 5
WeekdaysOnAndAfter.java
Download the WeekdaysOnAndAfter.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 days that come after the given day.
Example output of the program is shown below, with user input shown in bold:
Enter day (0-4): 0 Monday Tuesday Wednesday Thursday Friday
Further example output of the program is shown below, with user input shown in bold:
Enter day (0-4): 3 Thursday Friday
Further example output of the program is shown below, with user input shown in bold:
Enter day (0-4): 4 Friday
Further example output of the program is shown below, with user input shown in bold:
Enter day (0-4): -1 Invalid day: -1
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 |
In order to accomplish this, you will need to write two methods, namely numberToDirection
and randomDirection
.
Note that randomDirection
MUST call numberToDirection
!
Additionally, you must use switch
.
Further details are in the comments of Compass.java
.
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 numberToDirection
and randomDirection
methods 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.
Log into Canvas, and go to the COMP 110L class. Click “Assignments” on the left pane, then click “Lab 9”. From here, you can upload your answers and your code. Specifically, you must turn in the following four files:
Weekdays.java
WeekdaysOnAndAfter.java
Compass.java
CompassTest.java
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.