Exercise 1: JEM
Draw a JEM for the following program:
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:
-
Checks1 class, and
-
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
Alternatively, you can open the file Checks1.java in DrJava. Compile and run it.
You can now use it as a starting point for the other checker exercises.
Click for some pieces of advice to consider as you are writing your code.
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.
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:
|