Lab 6 In-Class: More Selection
This lab is designed to give you more practice writing conditional statements.
As usual, create a subdirectory for this lab, open up the Web version of
this handout in Mozilla, and open emacs.
Exercise #1: Activities at Lake Lazydays
Write a program Temp.java that prompts the user for a temperature, then
prints out an appropriate activity as described in #1 of the
prelab. As noted in the prelab, you should use a single cascading if
and should keep your conditions as simple as possible.
When this works, add the extreme temperature condition from #2 in the
prelab. Add a boolean variable extremeTemp and
give it a value as described in the prelab;
then modify
your if statement
so that if the temperature is too low or too high, the program
prints "Visit our shops!" instead
of any of the other activities.
Print your final program to turn in.
The Switch Statement
The Java switch statement (p. 144-148) is
another conditional statement.
Switch provides a compact, efficient syntax that is useful when you
would otherwise have a cascading if statement in which each
condition checks the value of the same expression. For example, the
if and switch statements below do the same thing -- either could be used
as shown in the program:
System.out.println("Enter size box you want");
int boxSize = Keyboard.readInt();
double price;
if (boxSize == 1)
{
System.out.println("Small");
price = 2.5;
}
else if (boxSize == 2)
{
System.out.println("Medium");
price = 3.75;
}
else if (boxSize == 3)
{
System.out.println("Large");
price = 5.0;
}
else
price = -1;
|
switch (boxSize)
{
case 1:
System.out.println("Small");
price = 2.5;
break;
case 2:
System.out.println("Medium");
price = 3.75;
break;
case 3:
System.out.println("Large");
price = 5.0;
break;
default:
price = -1;
}
|
if (price > 0)
System.out.println("Price is $" + price);
else
System.out.println("Sorry, we don't have that size");
A few things to note about switch statements:
- The expression you are switching on (boxSize in the example above)
must have an integer or character value. It cannot be a boolean,
floating point, or object.
- The expression is evaluated once and then is matched against
each of the cases, starting at the top. When a case matches, all the
rest of the code including later cases is executed. However,
very often
you will only want to execute the code for a single case. To do this, put a
break statement after the code for that case; this will
break control out of the switch statement, sending it to the
next statement in the program.
- If no other case matches, default always matches (like else
in an if statement).
You will use a switch statement (as well as a nested if) in the next exercise.
Exercise #2: Rock, Paper, Scissors
Program Rock.java contains a skeleton for the
game Rock, Paper, Scissors. Open it and save it to your lab6 directory.
Add statements to the program as indicated by the comments so that the program
asks the user to enter a play, generates a random play for the computer,
compares them and announces the winner (and why). For example, one run
of your program might look like this:
$ java Rock
Enter your play: R, P, or S
r
Computer play is S
Rock crushes scissors, you win!
Note that the user should be able to enter either upper or lower case
r, p, and s. The user's play is stored as a string to make it easy to
convert whatever is entered to upper case.
Use a switch statement to convert the randomly generated integer for
the computer's play to a string.
When your program works, modify it so that if either the user or the
computer does not have a legal play (R,P,S), it prints a message
telling which is wrong (user or computer)
and terminates without playing the game. (Note that this will
mean modifying the switch statement so that if the value is not 0,1, or 2
it assigns a different letter, maybe I for illegal, to computerPlay.)
If both plays are ok,
go ahead and print the computer's (legal) play and give the
winner. Think about the condition for this: the user's play
should be R or P or S; if it's not
one of these, there's a problem. Similar reasoning holds for the computer's
play. So the structure of the last part of your program should now
look like this:
if (the computer's play is illegal)
print message
else if (the person's play is illegal)
print message
else
{
print computer play
determine and print winner
}
Print your completed program to turn in.
Exercise #3: Date Validation
In this exercise you will write a program that checks to see if a date
entered by the user is a valid date in the second millenium.
A skeleton of the
program is in Dates.java. Open this program
and save it to your lab6 directory. As indicated by the
comments in the program, fill in the following:
- An assignment statement that sets monthValid to true if the
month entered is between 1 and 12, inclusive.
- An assignment statement that sets yearValid to true if
the year is between 1000 and 1999, inclusive.
- An assignment statement that sets leapYear to true if the year
is a leap year. Here is the leap year rule (there's more to it than
you may have thought!):
- If the year is divisible by 400, it's a leap year.
- Otherwise, if the year is divisible by 100 it is not a leap year.
- If neither of the above applies and the year is divisible by 4, then
it is a leap year.
Put another way, it's a leap year if a) it's divisible by 400, or b) it's
divisible by 4 and it's not divisible by 100.
- An if statement that determines the number of days in the month
entered and stores that value in variable daysInMonth. If the month
entered is not valid, daysInMonth should get 0. Note that to figure out
the number of days in February you'll need to check if it's a leap year.
- An assignment statement that sets dayValid to true if the day entered
is legal for the given month and year.
- If the month, day, and year entered are all valid, print "Date is valid"
and indicate whether or not it is a leap year. If any of
the items entered is not valid, just print "Date is not valid" without
any comment on leap year.
Print your completed program to turn in.
What to Hand In
- Printouts of Temp.java, Rock.java, and Dates.java
- Tar up your lab6 directory and e-mail it to your instructor at roanoke.edu.
Put cpsc120 lab6 in the subject line.