CIS 201 Labs: Lab 4
Chapter 2 Lab: Nested for loops and the Scope of Things

Some of the examples in this laboratory are based on problems and examples from the text Building Java Programs.

Objectives

In this lab you will work on

Directions

  1. Work with your partner and read the handout carefully as you go along. Make sure you EACH try every exercise.
  2. There are 6 checkpoints in this lab. If you need help with any problem, raise your hand. Call us over to check you off when you and your partner reach each checkpoint.
  3. You and your partner each have a role. One of you enters things at the keyboard while the other one reads. After EACH CHECKPOINT, swap roles with your partner.
  4. You and your partner print out a copy of the checksheet.

Getting Started

  1. Log into your (or your partner's account).
  2. Change directories so that you are in your 201 directory.
  3. Now copy the lab materials to your account. You will need to use the -r option on the Unix cp command in order to copy not only files, but also directories. To copy, enter
               
             cp -r /home/student/Classes/201/Labs/Lab04 .
    
    Don't forget the space and the dot . at the end of the command. Also, don't worry that you get a message indicating that you can't copy the Solution subdirectory.

    After running this copy command, do an ls you will see a new directory called Lab04. What you copied was actually a directory, plus all of its contents. Everything you need for the lab exercises today is contained in this new directory.

  4. Change directories into this new subdirectory and list the contents.
  5. Open a window to emacs.

Predicting the Behavior of Nested for Loops

  1. This first activity is paper and pencil. Both you are your partner print the following list. For each code segment indicate its output. Please don't just enter the code and run it. You won't be able to do that on your first midterm.
    1 Show us your answers when you have them. You will get this checkpoint when you have the correct answers and can explain to your lab assistant why some randomly selected answers in this problem set are correct.

Writing Nested for Loops and Using Global Constants

  1. Open the file Pattern1.java. This is the shell of a program. You are writing a nested for loop to produce the following output:
        !!!!!!!!!!!!!!
        \\!!!!!!!!!!//
        \\\\!!!!!!////
        \\\\\\!!//////
         
    Replace the comments that say /*something in terms of the row*/.
    Some Help: Notice the number pattern of characters in each line:
    Line  Number of \  Number of !  Number of /
    1 0 14 0
    2 2 10 2
    3 4 6 4
    4 6 2 6

    2 Show us your working program. Be able to explain how you came up with the expression you used in each loop.

  2. Now save the file Pattern1.java as Pattern2.java. You can do this from emacs by select File and Save Buffer As .... In the minibuffer, enter Pattern2.java and enter. Notice that the name of your buffer changes to Pattern2.java. In this new file, be sure to change the class name to match your new file name.
  3. Modify Pattern2.java to use a global constant for the figure's height. The above figure used a constant height of 4. Here is the output for a constant height of 6.
        !!!!!!!!!!!!!!!!!!!!!!
        \\!!!!!!!!!!!!!!!!!!//
        \\\\!!!!!!!!!!!!!!////
        \\\\\\!!!!!!!!!!//////
        \\\\\\\\!!!!!!////////
        \\\\\\\\\\!!//////////
        
    3 Show us your working program. We will ask you to change the value of your constant, recompile and run your program. So make sure it produces the correct output for different heights.

  4. Open Digits.java. Again this is a shell. In the main() method, write nested loops to produce the following output:
        000111222333444555666777888999
        
    Think about what the outer loop should control. Then think about what the inner loop should control for each cycle of the outer loop.

  5. Once you have the above program working, modify it so that it gives you the following output:
        000111222333444555666777888999
        000111222333444555666777888999
        000111222333444555666777888999
        
    4 Show us your working program.

  6. Now make one final modification. Make it easy to change the program to print any number of rows with the same number of digit repetitions. That is, to print n rows with n repetitions of the digits 0-9 per row. For example, here is the program printing 5 rows with 5 repetitions of the digits 0-9 per row.
         00000111112222233333444445555566666777778888899999
         00000111112222233333444445555566666777778888899999
         00000111112222233333444445555566666777778888899999
         00000111112222233333444445555566666777778888899999
         00000111112222233333444445555566666777778888899999
         
    5 Show us your working program. We will ask you to change the value of your constant, recompile and run your program. So make sure it produces the correct output for different heights.

Variable Scope

  1. Print off a copy of the program in the following file.
  2. Draw a box diagram (like we did in class) to show the scope of each variable.
  3. Using your diagram, circle any statement that would NOT compile because the variable does not have scope in that part of the program.
    6 Show us your diagram and the circled errors for this checkpoint.