CS111, Wellesley College 2007

Lab 10

Wedn, November 7, 2007

Looping Structures in Java

The purpose of this lab is to give you practice with the Java looping structures, namely FOR and WHILE loops. You will use iteration tables to show the sequence of values assigned to parameters and variables used. You will work on problems regarding Lists, problems from PictreWorld and TurtleWorld.

Download the folder lab10 from cs111d in puma. You should see three subfolders in there: ListProblems, PictureProblems and TurtleProblems.

Iteration on Lists

For the List-problems work in the LabOpsIteration.java file, within the ListProblems in the lab10 folder.

Task 1: isMember() -- pencil and paper

Last week in Lab we wrote a recursive implementation for the isMember() method, which takes a String and a StringList as arguments, and returns true if the string is found in the list, false otherwise. As a reminder, here is that recursive implementation:

     public static boolean isMemberRec(String s, StringList L) {
        if (isEmpty(L)) 
           return false;
        if (s.equals(head(L))) 
           return true;
        return isMemberRec(s, tail(L));
 }
 
Is this a tail-Recursion? Why? Are there any others means (constructs) in Java that we can use to provide an other iterative solution for this problem?

As a matter of fact, here is an other iterative solution that uses the "while-loop" construct:


     public static boolean isMemberWhile(String s, StringList L) {
        boolean found = false;  //"found" starts out false, 
                                //and will become true when (and if) s is found in L
        String current; //"current" will hold the head of L as we walk down the list
     
        while (!isEmpty(L) && !found) {
          current = head(L);
          if (current.equals(s))  //string s was found!
            found = true;
          else 
             L = tail(L);
        }
        return found;
     }

Assume we have a StringList L1 = ["here","then","and","now"]. Given the while-loop implementation above, draw -on paper- the iteration table for isMemberWhile("and", L1) and for isMemberWhile("today", L1).

Task 2: listSize() -- WHILE loop

In LabOpsIteration.java, write a class method named listSize() that takes a StringList L as an input and returns the number of elements in the list. In this implementation, use the WHILE-loop construct. Make sure to test your method thoroughly!

Task 3: implodeFor() -- FOR loop

Recall from last week's lab the implode() method. implode() takes a string list and returns a string that is the result of concatenating all the strings in the input list in order. Write a class method implodeFor() the uses the FOR-loop JAVA constract. You will need to use the previously defined method listSize().


Loops in PictureWorld: RowWorld

For the PictureWorld tasks you need to work in the RowWorld.java file, within the PictureProblems in the lab10 folder. In the following two tasks you will use the looping structures we learned recently to produce a row of multiple copies of an image.

Task 4: many pictures in a row -- WHILE loop

rowWhile(p,n) produces a picture with picture p arranged in n equally spaced columns (i.e. a row with n elements).
For example,

row(smile(Color.black,Color.yellow),1) row(smile(Color.black,Color.yellow),4)

Write the definition of the rowWhile(p,n) method, and run the program RowWorld.java to test it.

Task 5: many pictures in one row -- FOR loop

This time define the method rowFor(p,n) which works like the method in the previous task, but it uses the FOR-loop structure instead of a while-loop.

Loops in TurtleWorld: Polygons and Flowers

For the TurtleWorld tasks you need to work in the PolygonWorld.java file (tasks 6 & 7), and the FlowerWorld.java file (tasks 8, 9 & 10). Both files are within the TurtleProblems sub-folder in the lab10 folder. For the following tasks you will program turtles to draw regular polygons and flower-looking structures.

A regular polygon has sides of equal length and angles of equal length, as shown in the following images:

Task 6: Polygons WHILE-loop

Write the definition of the polygonWhile() class method, that takes as parameters the number of sides, and the length of each side (both are integers). Run the program in PolygonWorld.java to test it.

Task 7: Polygons FOR-loop

Re-write your previous method to use a for-loop this time.

Now that you can draw a polygon, you can use this method to draw a polygon flower. A polygon flower is defined by the number of petals and the number of sides of each petal. Each petal is a regular polygon, and the petals are rotated with respect to one another. The angle of rotation is equal to (360.0/petals). Some sample flowers are as follows:

As with the polygon, we will write methods to draw these flowers using for and while loops. You will need to work in the FlowerWorld.java You should make use of one of the polygon methods you wrote in the last section (Note that FlowerMaker extends PolygonMaker).

Task 8: Flowers WHILE-loop

Write the definition of the method

public void flowerWhile(int petals, int sides, int length)

that draws the flower using a while-loop.

Task 9: Flowers FOR-loop

Define an other method, flowerFor() to perform the same task as above, this time using the for-loop structure.

Task 10: Flowers nested FOR-loop

Now, just for practice, we will write a method to draw these flowers using a nested for loop. So, do not make use of the Polygon methods you wrote previously. Name your method flowerNestedFor().