![]() |
Lab 10
|
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 folderlab10
from cs111d
in puma. You should see three
subfolders in there: ListProblems
, PictureProblems
and
TurtleProblems
.
LabOpsIteration.java
file, within the
ListProblems
in the lab10
folder.
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).
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!
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()
.
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.
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.
rowFor(p,n)
which works like the method in the previous task,
but it uses the FOR-loop structure instead of a while-loop.
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:
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.
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
).
public void flowerWhile(int petals, int sides, int length)
that draws the flower using a while-loop.
flowerFor()
to perform the same task as above, this time
using the for-loop structure.
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()
.