Lab 6 In-Class: More Selection and Boolean Expressions

Lab Objectives

  • Introduce the switch statement
  • Work with boolean expressions
  • Practice writing conditional statements
  • Pay attention to testing programs

As usual, create a subdirectory for this lab, open up the Web version of this handout in Firefox, and open Emacs.

Exercise #1: Rock, Paper, Scissors

Program contains a skeleton for the game Rock, Paper, Scissors. Open it and save it to your lab6 directory.
  1. 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 the plays and announces the winner (and why). For example, one run of your program might look like this:
        Enter your play: R, P, or S
        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 the appropriate method in the String class). Use a switch statement to convert the randomly generated integer for the computer's play to a string.

  2. Test your program to make sure it works at this point. In testing a program with several conditions you want to make sure all possible paths through the program are correct and that it works correctly on all valid input. On the "Testing" sheet (handed out separately), answer the first two questions (#1(a), (b)). Use your answers to thoroughly test your program.

  3. 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. (Of course, if the computer's play is illegal it is a programming error - your random number is incorrect. However, you should still check.) If both plays are ok, go ahead and print the computer's (legal) play and give the winner (this is the code you already have). 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. The structure of your program should look like this:
      // your current code to get the user's play and the computer play
      // Catch any illegal input
      if (the computer's play is illegal)
         print message
      else if (the person's play is illegal)
         print message
         // this part is your current code starting with printing the computer play
         print computer play
         determine and print winner 

  4. Test your program. You now want to be sure to catch invalid input. List some test data on the "Testing" sheet (#1(c)).

  5. Make sure your code is properly indented (CTRL-X CTRL-P, then CTRL-ALT-\) then save and print your completed program to turn in.

Exercise #2: Date Validation

Computers use dates all the time. For example, a program that keeps keeps track of your bank account may need your birth date; a program that keeps track of books in a library must deal with due dates; or the college radio station WRKE may need a program to generate a random "WRKE Super Prize Birthday" (remember Lab 3?). 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 Open this program and save it to your lab6 directory. As indicated by the comments in the program, fill in the following (one at at time!):
  1. Add an assignment statement that sets validMonth to true if and only if the month entered is between 1 and 12, inclusive.

  2. Test to make sure this is correct. Look on the "Testing" sheet at the "Testing the Month" part of #2. Note that there are 5 dates to use to see if your program is checking the month correctly. Pay attention to the choice of test dates - errors frequently crop up in programs at the "boundaries" of data ranges. In this case the correct range for the month is 1 to 12, inclusive, so 4 of the test cases have a month at the boundaries (one date just inside and one just outside). Make sure your program works correctly for each of these. Check the My Program column if it works correctly the first time - if not put and X and correct your program. You will not be penalized if the X is there as long as you correct the program!

  3. Add an assignment statement that sets validYear to true if and only if the year is between 1000 and 1999, inclusive. Modify the print statement (near the end of the code) so that it prints valid when both the month and year are valid.

  4. Test to make sure the year is correct. On the "Testing" sheet list 5 dates (three valid and two not) that would be good for testing the year. Make sure your program works for those.

  5. Add an assignment statement that sets leapYear to true if and only if the year is a leap year. Here is the leap year rule (there's more to it than you may have thought!): A year is a leap year if the year is divisible by 400 OR if the year is divisible by 4 but NOT divisible by 100.

  6. Inside the current if statement that prints the valid/not valid message, in the case that the date is valid, add an if statement to print a message indicating whether or not the year is a leap year (this message should be printed after the valid message).

  7. Test to make sure your leap year condition is correct. Because the condition for a leap year is a bit complex there are several cases you need to test. There are basically three conditions involved in the expression for leap year - "year is divisible by 400", "year is divisible by 100", "year is divisible by 4". In general when a complex condition is formed from 3 simpler conditions there are 8 possible combinations of truth values. However, in this case because the 3 conditions are not independent (for example, if a year is divisible by 400 it is automatically divisible by 4 so it isn't possible for "year divisible by 400" to be true and simultaneously "year divisible by 4" to be false). On the "Testing" sheet fill in the eight combinations of truth values and for each determine if it is possible. For those that are possible provide an example year. Test your program on those years.

  8. Add an if statement or a switch 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.

  9. Add an assignment statement that sets validDay to true if the day entered is legal for the given month and year.

  10. Modify the if statement that currently prints the Valid/Not Valid message so that if the month, day, and year entered are all valid, it prints "Date is valid" and indicates whether or not it is a leap year (so leave the if that checks for a leap year). If any of the items entered is not valid, just print "Date is not valid" without any comment on leap year.

  11. Test your program. To test that the day is correct, you should test at the "boundaries" - the beginning and end of every month and just outside the boundaries. On the "Testing" sheet write down test dates to thoroughly test February (note that you need 6 not 4 cases - why?). Test your program for these dates plus a few others.

  12. After the current if that prints whether or not the date is valid add code to advance the date to the next day and print the result, appropriately labeled. For example, if the user enters 4 30 1950 for the date, the next day is 5/1/1950. Think carefully about the logic of this - some ifs are required but don't make it harder than it is!

  13. Testing your next day. As before February is special - list 3 dates in February that you should test. There is one other date that definitely should be tested - what is it? Write these four dates plus two other good dates to test on the "Testing" sheet.

  14. Be sure your code is properly indented before printing it to turn in.

What to Hand In