nn CS111 Lab 2

CS111

Lab 2

Wed Feb. 1, 2012

Java Execution Model and Methods

Total checkmarks:

Today's agenda:


Go to the CS111 download directory using the cs111d account on puma, and download the lab2_programs folder to your desktop. Rename the folder so that it includes your name, e.g. betty_lab2_programs, like this:
image of renamed folder

Buggles and BuggleWorld

Exercise 1: JEM

Draw a JEM for the following program. Click here to see what it produces when it runs.


Exercise 2: Welcome to ChecksWorld!!!

Open Checks.java in DrJava.

What picture does Checks.java produce when run?

Run the program to see the answer.

Click here for the answer


For all the Checkerboard tasks below, you will be creating new java files from scratch.

Task 1 -- Drawing a pattern using a Method

The goal of this task is to write a java program to draw the simple check you just saw, but the important difference is that your program will use a method [a method is a block of Java code with a name, e.g. the drawL() method above].
The structure of your program will be very similar to the PuppyWorld.java program you saw in lectures, and is included in the lab2_programs folder. Use it as your guide, if you want!

Create a new file called Checks1.java, and save it in your lab2_programs folder. In this file, include the definitions of two Java classes:

  1. Checks1 class, and
  2. CheckerBuggle class
Similar to the PuppyWorld class, the Checks1 class should:
  • extend BuggleWorld
  • have a setup() method
  • have a main() method, and
  • have a run() method
Similar to the PuppyBuggle class, the CheckerBuggle class should:
  • extend the Buggle class, and
  • have a method to draw one checker triangle, as you saw in Checks.java

Click for some pieces of advice to consider as you are writing your code.

At the end, your Checks1.java program, should behave the same exact way the Checks.java above behaves.

You can now use it as a starting point for the other checker exercises.

Task 2 Flexibility: The same pattern in any color!

Save your Checks1.java into a new file named Checks2.java, in your working folder. Now, change the method in the CheckerBuggle class, so that the tower pattern can be drawn in a specified color. The three images below were all produced by the same method, but each method invocation had a different color parameter.
purple tower cyan tower blue tower

You'll rewrite your method to include a parameter. This way, just the one method can produce the tower in any given color. Reusability of code! Cool!
Add several lines of testing code in the run() method of your program.

Note: If you get an error compiling your code where drJava complains about more than one class definition, you can work around this by compiling one file at a time, like this:

(Clicking the button in drJava compiles ALL the currently open files, and this can cause confusion if multiple files contain definitions of the same class, e.g. CheckerBuggle).


Exercise 3: - Bagel Rugs

Here's a simple rug that the buggles worked on before they came up with their more sophisticated design appearing in your homework assignment. Can you figure out how to put this rug together? There are many different solutions. We have supplied some helper methods that you may use if you choose.

Starting code for this exercise can be found in LabRugWorld.java file, within the lab2_programs folder. Study the given code, because you may need to use parts of it as you are building your rug!

Here are some of the helper methods that you might find helpful (you are also encouraged to write your own, as you see fit):

  • drawPattern1(Color.yellow) produces this pattern:
  • drop5Bagels(Color.blue) produces this pattern:
  • drawPattern3(Color.red) produces this pattern:
  • drawPattern4(Color.blue) produces this pattern:
If you'd like a bit of pictorial guidance, please click here.

In case you have more time or would like more practice, here are some more problems:

Exercise 4: - More Complex Checkerboards

Task 1 -- Drawing a checkerboard using Methods

In this task, you will write a program to draw the following checkerboard picture. The idea is to define methods to accomplish smaller tasks, and then put them together to get the whole picture. The run() method of your program should be very short, just a couple of line of code. Your program should be flexible on the colors of the board it is producing.

Think: Assuming that you have a way to draw a simple check triangle -as Exercise 1, Task 2-, how can you get the picture below?
Please do not call the setPosition method!

Implementation: Now that you have a plan, write your code in a new file named Checksboard.java. You are encouraged to start by copying and renaming your Checks2.java file.

Your aim here, and in every program you are writing, is to make your run() method short, and easy to understand! For this, your run() method should mainly contain method invokations, and of course, a statement to create an instance of a CheckerBuggle.

In the end of the program the buggle must be positioned as shown below. However, it may have any color (not black, as on the picture).

Task 2 -- Draw the checkerboard that is shown in the next picture, using more methods.

Create a new file called BigChecksboard.java, with a 15x15 grid in BuggleWorld and a CheckerBuggle class.

Write BigChecksboard.java to solve the problem:

Task 3 -- And even bigger and fancier checkerboard (using more Methods...).

Create a new file called SuperBigChecksboard.java, with a 45x45 grid in BuggleWorld and a CheckerBuggle class. SuperBigChecksboard.java should create this picture:

Task 4 -- Big and Flipped Checkerboard

Create a new file called FlippedSuperBigChecksboard.java with a 45x45 grid to solve this problem: