CS111 Spring 2012

Lab 3

Wed, Feb. 8, 2012

Today:

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

The file HurdleWorld.java contains the complete definition of the HurdleWorld class. You do not need to edit this file at all.

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.

public class HurdleWorld extends BuggleWorld {
 	
	public void run () {
		Hurdler happy = new Hurdler();
		happy.tick16();
	} 
} // closes HurdleWorld class
   
The Hurdler class should be 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.

If you need some hints as you contemplate this problem, click here. When you are done, make sure to test your solution on several hurdle configurations to make sure it works well in all of them.

You will write the Hurdler class from scratch, in a separate file called Hurdler.java.

Make sure you run the HurdleWorld class, not the Hurdler one.


EXERCISE 2 - 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, in which a FrameBuggle object is created. You will write the FrameBuggle class from scratch. 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:

  1. The buggle starts in the lower left corner (1,1), facing EAST, brush down, red
  2. The buggle ends in the position shown in the picture
  3. Bagels are dropped in cells that have an odd-numbered row and odd-numbered column
  4. You may use the getPosition() method from the Buggle contract.
  5. Your program will work for ANY NxN BuggleWorld grid where N is an odd number
  6. Like the Hurdler above, you will use the tick() method to control the progression of your buggle
bagel frame size 11
BuggleWorld grid size = 11 x 11
bagel frame size 7
Grid size = 7 x 7
bagel frame size 19
Grid size = 19 x 19
bagel frame size 3
Grid size = 3 x 3

A bit of guidance for solving this problem:

  • What are the different possible conditions your buggle might encounter? (draw them out)
  • In each case, what is the right action for your buggle to take?
  • Construct your java code based on the condition/action pairs, making sure to order them carefully
  • Remember that the tick() method may be called more times than is needed for your particular grid, so make sure your code does the right thing
  • The modulo operator (%) gives the remainder after the division has been completed.
    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 3: 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:
    1. Call your program Waves.java
    2. 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)
    3. 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(">>>>      <<<<");
    4. 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 <<<<.

    5. The key to success here: methods.
      Here are some possibilities (there are many, many more) for solving this problem. The color bands below are used to indicate methods (e.g. the green band is produced by one method; the red band by another method).

      2 methods
      (green, red)
      3 methods
      (green, red, blue)
      4 methods
      (purple, red, yellow, green)
      Keep in mind that you can write methods that invoke other methods.

    6. 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 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:

    The HighHurdleWorld.java file creates a new HighHurdler to jump the new hurdle course. Your job is to write the HighHurdler class from scratch. As you might have already guessed, the Hurdler class can be copied and pasted into your new HighHurdler class, and then you can modify the code to handle the two different height hurdles.

    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.

    For this task, you'll need to create a new TiredHurdler class from scratch that will handle these different conditions.