CS 101 Laboratory #4
Conditionals and Iteration

Objective: To gain experience with using Java conditional and iteration constructs as problem-solving tools.
Due date is the beginning of your lab section next week:

Lab prep: Read this lab assignment.

Written Exercises: Read Brookshear chapter 5 (particularly section 4), write up these exercises, and turn them in along with your lab work.

  1. In your own words, what is the difference between pseudocode and Java source code?
  2. Do exercise 22 on page 244.

In this lab, you will continue to use the DrJava IDE to develop Java programs. In particular, you will write programs that utilize the if-else construct, as well as the while and for constructs, to solve new programming challenges.

For each program that you write, be sure to include comments at the beginning indicating your name, your lab section, the exercise number, and what the program does. Please turn in a printout of your Java source file for each program that you write, and please make sure all pages are stapled together.


(The code on the blackboard is in the C language, a predecessor of Java. See the resemblance?)


  1. Write a program named WaitForTable.java that determines whether or not to wait for a table at a restaurant you've just entered. Your program should take two command-line arguments: patrons and isHungry. The argument patrons should be a String that indicates how many patrons the restaurant has. For our program, we will assume that it can be one of three values: "none", "some", or "full". The argument isHungry should be a String that indicates whether or not you are hungry. It can be one of two values: "yes" or "no". Your program should output either Wait! or Leave!, depending on whether or not you should wait. It determines this as follows: If patrons is "none", then the restaurant probably isn't very good, so you shouldn't wait. If patrons is some, then you'll probably get a table soon, so you should wait. If patrons is "full", then it depends on whether or not you're hungry. If isHungry is "no", then you should wait. Otherwise, you should leave to try to find a quicker way to eat.

    Note: use the equals method for strings to determine equality. For example, the expression s.equals("yes") will be true if s is the string "yes", and false otherwise.

    In DrJava, run your program in the Interactions tab of the Interactions pane (rather than using the Run button). Note that if you don't supply valid command-line arguments, you will get an unfriendly error report when you try to run your program. Give a "usage" message that shows how to use the program if no command-line arguments are given. (We'll learn ways to more completely deal with errors later.) Here are examples of how your program should work:

    > java WaitForTable some yes
    Wait!
    
    > java WaitForTable full yes
    Leave!
    
    > java WaitForTable
    Usage: java WaitForTable <patrons ("none"|"some"|"full")> <hungry? ("yes"|"no")>
    

    Make sure that your program works for all possible combinations of command-line arguments. Turn in a printout of your Java source code.

  2. Using a while loop, write a program named SquareRoots.java that prints out a list of square roots from 1 to n where n is a positive integer supplied as a command-line argument. Give a "usage" message if no command-line arguments are given.

    Note that you can use the built-in Integer.parseInt method to convert a String to an int. For example, the value of the expression Integer.parseInt("15") is the integer 15. Further note that you can use the built-in method Math.sqrt to compute the square root of a number. For example, the expression Math.sqrt(9) has the value 3.0.

    Here is an example of how your program should work:

    > java SquareRoots
    Usage: java SquareRoots <n>
    
    > java SquareRoots 5
    1 1.0
    2 1.4142135623730951
    3 1.7320508075688772
    4 2.0
    5 2.23606797749979
    

    Make sure your program works for several values of n. Turn in a printout of your Java source code.

  3. (from the Reges & Stepp text) Using for loops, write a program named Pattern.java that prints out the pattern below. You may use multiple System.out.print statements, but each should only print one or two characters.

    Here is an example of how your program should work:

    > java Pattern
    ****** //////////// ******
    *****  //////////\\  *****
    ****   ////////\\\\   ****
    ***    //////\\\\\\    ***
    **     ////\\\\\\\\     **
    *      //\\\\\\\\\\      *
           \\\\\\\\\\\\       
    

    Turn in a printout of your Java source code, along with a transcript showing that your program works correctly (print the Interactions Pane by selecting "Print Interactions" from the DrJava Tools menu).

  4. Write a program named Calendar.java that prints out the 31 days of some 31-day month (such as March) in "calendar" format. Your program should take one command-line argument, an integer, specifying which day of the week to start from (0 indicates Sunday, while 6 indicates Saturday).

    Look carefully at the sample runs below to understand how your program should work. Before printing any days, your program should print three blank spaces for each day of the week prior to the start day. Then, your program should use a loop to print the days from 1 to 31. For days numbered 1 through 9, it should print two blank spaces before printing the number. For days numbered 10 to 31, it should print just one blank space before printing the number. You may find the System.out.printf useful for printing these leading spaces. Furthermore, your program should keep track in a separate variable how many days have been printed for that week. When seven days have been printed, your program should start a new line. Note that for the first line of the calendar, the number of days that have been printed should be initialized to the value specified on the command line, whereas for subsequent weeks, it should be reset to zero. Give a usage message if no command-line argument is given.

    Here are examples of how your program should work:

    > java Calendar
    Usage: java Calendar <0--6>
    
    > java Calendar 0
      S  M  T  W  R  F  S
      1  2  3  4  5  6  7
      8  9 10 11 12 13 14
     15 16 17 18 19 20 21
     22 23 24 25 26 27 28
     29 30 31
    
    > java Calendar 4
      S  M  T  W  R  F  S
                  1  2  3
      4  5  6  7  8  9 10
     11 12 13 14 15 16 17
     18 19 20 21 22 23 24
     25 26 27 28 29 30 31
    

    Make sure your program works for all values of n from 0 to 6. Turn in a printout of your Java source code, along with a transcript showing that your program works correctly.

  5. Extra credit: Write a program named Calendar2.java that takes two integer command-line arguments: a month (from 1 to 12) and a four-digit year (1800 or higher). Have your program print the actual month of that year. Hints: January 1, 1800, was a Wednesday. Years divisible by four are leap years with the following exception: years divisible by 100 are not leap years unless they are also divisible by 400 (e.g., 1800 was not a leap year, but 2000 was a leap year.) Include a usage message for incorrect command-line input.

  6. About how long did it take you to complete this assignment?

Back to Computer Science 101 Home
Department of Computer Science