Today:
- Conditionals and Buggle hurdling
- A JAVA application with methods (Waves)
- Conditionals and Bagel Picture Frames
Please download the folder lab3_programs
from the
cs111d
account on puma
.
Conditionals
Total check marks:Today's goal is to give you some experience programing with conditionals
in different contexts, so there are lots of exercises today.
HurdleWorld

Buggles are putting on their sneakers in preparation for one of their favorite activities: hurdle jumping! In this activity, a number of hurdles (walls one grid cell high) are placed at the first row of a BuggleWorld grid. A buggle starting at position (1,1) must run across the first row of the grid jumping all of the hurdles in its path, until it reaches the opposite wall. The buggle does not know the placement of the hurdles in advance and so must detect them when it encounters them. Here is a sample hurdle configuration:

and here is the result of a buggle jumping all of the hurdles:

Your goal in this problem is to implement a hurdling buggle. Your Hurdler buggle jumps all the hurdles in the grid. All hurdles are 1 cell high. (If you have time, later in this lab you can write more complicated versions of the hurdler buggle).
Exercise 1: Hurdler 
Open HurdleWorld.java
. It currently contains two classes: HurdleWorld and Hurdler. The HurdleWorld class is complete, and you will add code to complete the Hurdler class:
public class HurdleWorld extends BuggleWorld { // Some code that you can ignore is omitted here. public void run () { Hurdler happy = new Hurdler(); happy.tick16(); } } // closes HurdleWorld class class Hurdler extends TickBuggle { public void tick() { } public boolean atFinishLine() { // Return true if the buggle is facing a wall (as opposed to a hurdle) // and false otherwise. // Should leave the buggle in the same position and heading as when it starts. // Should not leave any paint marks behind. } public void jumpHurdle() { // the buggle will jump the hurdle it is facing } } // closes Hurdler class
The HurdleWorld
class is responsible for changing the
size of the grid and populating it with a random number of
hurdles. You do not have to understand the code that performs these
actions. All that you have to understand about
HurdleWorld
is that it has a run()
method
that creates a Hurdler
instance
and tells this buggle to move for 16 ticks. The Hurdler
class is a subclass of the TickBuggle
class.
Hurdler
has a tick()
method that specifies
what it does on each clock tick. This method overrides the default
tick()
method supplied by the TickBuggle
superclass, which, as you know, does nothing.
EXERCISE 2:
Drawing ASCII patterns (a Java application)
You'll write a Java program (from scratch) to create this text picture in the DrJava Interactions pane:>>>> <<<< >>> <<< >> << > < >> << >>> <<< >>>> <<<< >>> <<< >> << > < >> << >>> <<< >>>> <<<< >>> <<< >> << > < >> << >>> <<< >>>> <<<<Here are the constraints:
- Call your program
Waves.java
- You could easily write 19
System.out.println
statements to produce that picture above
(but that's not very elegant; we strive for elegance in cs111) - You can only use each of the following 4 statements ONE TIME in your program
-
System.out.println("> <");
-
System.out.println(">> <<");
-
System.out.println(">>> <<<");
-
System.out.println(">>>> <<<<");
-
- There are 12 spaces in between the single > and <;
there are 10 spaces between the >> and <<;
there are 8 spaces between the >>> and <<<;
there are 6 spaces between the >>>> and <<<<.
- The key to success here: methods.
Here are some possibilities (there are many, many more) for solving this problem.2 methods
(green, red)3 methods
(green, red, blue)4 methods
(purple, red, yellow, green) - You'll need to write a main method to test your code.
In your main method, you'll call your methods in order to produce the wave pattern above.
The signature of a main method always looks like this:
public static void main (String [] args)
EXERCISE 3
- Bagel picture frames
You're going to write code to create lovely bagelled picture frames (sans pictures). There is a template of code called
FrameWorld.java
. You'll add your code to
FrameWorld.java
file. These frames work when the BuggleWorld
grid has a height and width that are odd numbers. Below is a frame in
a 11x11 grid.
Here are the rules for making the frames:
- The buggle starts in the lower left corner (1,1), facing EAST, brush down, red
- The buggle ends in the position shown in the picture
- Bagels are dropped in cells that have an odd-numbered row and odd-numbered column
- You may use the
getPosition()
method from the Buggle contract. - Your program will work for ANY NxN BuggleWorld grid where N is an odd number
- Like the Hurdler above, you will use the
tick()
method to control the progression of your buggle

BuggleWorld grid size = 11 x 11
![]() Grid size = 7 x 7 |
![]() Grid size = 19 x 19 |
![]() Grid size = 3 x 3 |
A bit of guidance for solving this problem:
tick()
method may be called more times than is needed for your particular grid, so make sure your code does the right thing
For example,
10 % 3 = 1
and 15
% 6 = 3
(because 6 goes into 15 two times to make 12, and the
remainder (15 - 12) is 3). Exercise 4a (revisiting our hurdler buggle): HighHurdler
In this exercise, we're going to extend the capablities of our hurdler buggle. Newsflash: the Olympic Officials have suddenly changed the rules! Hurdles can be one or two cells high! And they are randomly placed in the track. So, a hurdler must figure out whether to jump one or two cells high with each hurdle. Here are two sample hurdle runs:
Open up the HighHurdleWorld.java
file to write code for
this problem. (As you might guess, for the Hurdler
definition, you are at a good starting place if you
just copy the relevant code from HurdleWorld.java
file.
Note: Your hurdler may not jump 2 cells high if the hurdle ahead is only 1 cell high.
Exercise 4b: TiredHurdler
After a long day of hurdling, buggles are often too tired to jump
all of the hurdles in a configuration. The TiredHurdler
class describes buggles who will jump no more than a specified number
of hurdles. The constructor method for TiredHurdler
takes an integer that is the maximum number of hurdles the buggle will
jump. For instance, new TiredHurdler(3)
creates a buggle
that will jump no more than three hurdles. If it encounters a fourth
hurdle, it will stop moving.