Graphic by Keith Ohlfs

CS111, Wellesley College, Fall 1997

Lab #6
[CS111 Home Page] [Syllabus] [Students] [Lecture Notes] [Assignments] [Programs] [Documentation] [Software Installation] [FAQ] [CS Dept.] [CWIS]

CS111 Lab #6: Conditionals

Due Date: Thursday, October 23, 6:00 p.m.

In lecture you were introduced to Buggleworld. In this world you can create "Buggles" that can move in different directions and draw onto their grid world. You saw how to write a Java method to have a Buggle draw a square and in the Prelab you wrote a method to draw an "S" shaped Snake. In this lab we will review buggles, and introduce the concept of "Conditionals". The Conditional statement allows you to test for something, and carry out different sets of statements depending on whether the results of the test are true or false. The form of a conditional statement in Java is as follows:

if (expression)
{
statement1;
statement2;
...
statementn;
}
else
{
statementa;
statementb;
...
statementLast;
}

If expression is true, then the first set of statements (1, 2,..., n) are carried out. If expression is false, then the second set of statements (a, b, ..., Last) are carried out. For example, in the hurdles.java code in the Buggles folder, the program tests to see if the buggle is facing a wall. If it is, it jumps and if it isn't it moves forward:

if (isFacingWall())
{
this.jump();
}
else
{
this.forward();
}

The following exercises make use of conditionals to control the motion of Buggles.

Exercise 1: Drawing a Checkerboard

1) First, download the folder, Buggles, from the download directory for cs111 on Nike. This folder contains lots of java source files, html files, class files and one project file. Make sure all the icons are displaying the proper images (e.g. a coffee cup on the class files). If they are not, download the fetch preferences again and try again.

2) Open the project window and double click on "MyBuggleWorld2.java". This is the version of Buggleworld in which Buggles can draw squares. Change the name of the class to Checkers. Choose "Save As..." from the file menu and save this file as "Checkers.java". Double click on "MyBuggleWorld.html". Change the name of the class that it invokes to "Checkers.class". Choose "Save As..." and save this file as Checkers.html. Choose "Bring Up to Date", and use the Applet Viewer to look at the html file, "Checkers.html". When the applet appears, you can click on the "Run" button to see the buggles draw some stuff.

3)Add a new method called "toggleBrush" to the MyBuggle2 class that appears at the bottom of the Checkers.java file. This method should use the conditional statement to test to see whether the brush is down (Use the function "isBrushDown()"). If it is down, the brush should be moved up. If it is up, the brush should be moved down.

4) Now add more methods to the MyBuggle2 class that draw a checkerboard as shown below. One method should be called "drawCheckers()". Be sure to use "divide and conquer" to create supplementary methods that draw subsets of the checkerboard. One way to start is to imagine buggle moving across the bottom line (8 steps), toggling the brush with each step, then turning, moving up one step, turning again and moving across the next line (8 steps), etc. By snaking back and forth, the buggle will be able to fill in all the rows with a checkerboard pattern.

5) Change the code inside the "run()" method of Checkers. First, delete all of the old code, except for the first two lines:
public void run () {
MyBuggle2 becky = new MyBuggle2();
becky.setColor(Color.red);
}
Now, add one more statement to invoke the drawCheckers method, and have becky draw the checkerboard. If you are unsure of how to do this, look at MyBuggleWorld2.java and see how the method to draw a square is invoked.

5) Finally, bring your project up to date, and use the applet viewer to view "Checkers.html". Click on the "Run" button to see becky draw the checkerboard.

Exercise 2: Running an Obstacle Course

1. Drag the files named "Obstacles.java" and "Obstacles.html" into the Buggle.proj project window. Choose Bring Up to Date from the Build menu. Use the AppletViewer to look at the obstacle course that is set up in this program. It should look as follows:

2. In place of the "move" method provided in the ObstacleRunner class, create a new "move" method that tests to see if the Buggle is facing a wall. If it is, the Buggle should turn left and move forward one step. If it is not, the Buggle should simply move forward one step.

3. Using this new "move" method, write a method for the Buggle to run the obstacle course by turning left every time it reaches a wall. This method is the "runObstacles" method, in place of the one that is there. The Buggle should take a total of 48 steps to run the course (use divide and conquer so that you don't have to write 48 lines of code; write whatever supplementary methods you need to do this).

4. Bring the project up to date and use the applet viewer to view the result of the buggle running the obstacle course. You should see a recognizable pattern.

Turning in Lab 6

When you are finished. Save six files (Checkers.java, Checkers.html, Checkers.class, Obstacles.java, Obstacles.html and Obstacles.class) in a folder named "username_lab6", where username is your username. Do not include the Buggleworld.proj file. Upload this folder into the lab6 drop folder on Nike.