CSC 171
Homework 3

Assigned Feb. 13, due Feb. 26

Design Recipe

For each programming problem below, you should follow a 6-step recipe:

  1. Choose names and data types as necessary for the problem at hand. Write these down on paper or in a text file, or (if you prefer) directly as javadoc comments in your program file. (You can write javadoc comments for methods you haven't written yet; they won't show up in the generated Web page until you write at least a method header, but that's OK.)
  2. Write test cases: a sequence of things you plan to type in the Interactions window, and what answer each one should produce, in order to confirm that it works correctly. You may write these in a separate text file, or (in comments) in your program file, whichever you prefer.
  3. Write the Java code in the Definitions Window.
  4. Proofread the code, looking for misspellings, mismatched braces, missing semicolons, etc.
  5. Compile the code. If there are syntax error messages, log them for future reference, fix them, and return to the "Proofread" step above, until there are no error messages.
  6. Test the code by typing into the Interactions window the things you said you were going to try. If any of them produce answers different from what you expected, log what happened for future reference, fix the error, and return to the "Proofread" step above, until everything works the way you expect.

Error logging

I want a log of everything that went wrong in the development of your programs. You may do this on paper or in a text file, as in homework 2, or using the on-line PSP forms.

Programming assignments

Writing a class

Do problems P2.13 and P2.14 from the textbook. This entails writing two new classes, Circle and Square, each with a constructor, a getArea() method and a getPerimeter method. (The textbook doesn't say it, but I'd like you also to write a toString method for each class.) If you don't remember the formulæ for the area and perimeter of a circle and a square, look them up in a math textbook.

I recommend doing this in a sequence of small steps or versions. For example (you don't have to use this precise sequence),

  1. write the Square class, with its instance variable(s).
  2. add a constructor to the Square class.
  3. add toString to the Square class.
  4. add getArea() to the Square class.
  5. add getPerimeter() to the Square class.
  6. do the same steps for the Circle class.

As usual, follow the design recipe for each version: choose names and types, choose test cases, write code, proofread, compile, and test before going on to the next version.

Modifying and extending a class

In DrJava, open the BuggleWorld.java program. About 7-8 lines down from the beginning, find the lines that say

  public int rows = 9;
  public int cols = 9;
  
These control the size of the grid in the BuggleWorld. Change both numbers to 21. Compile the program and create a new BuggleWorld from the Interactions window; it should work just as before, but have a 21x21 grid of smaller squares.

Next, with BuggleWorld still open, click the "New" button to open a new program file. In this file, we'll build another class named WritingBuggle:

  1. Write a new class WritingBuggle which extends the existing class Buggle. It should work exactly the way Buggle does: you should be able to say things like
    WritingBuggle billy = new WritingBuggle();
    and test billy exactly as before.

  2. Add a method named "drawS" to the class. It should draw the letter S, in the current color, 3 squares wide by 5 squares high, starting at the WritingBuggle's current location. The WritingBuggle should end up two squares to the right of the lower-right corner of the S. You should be able to write this in 15-20 lines. Here's an example, showing the result of two successive calls to drawS. (Click on the image to enlarge it.)


  3. Add three more methods drawC, draw1, and draw7, similarly. The letter "C" and the number "7" should be 3 squares wide by 5 squares high; the number "1" should be 1 square wide by 5 squares high. Each of these methods will probably be 10-20 lines long.

  4. Add a method drawCourseName to the class. It should draw the letters "CSC171", side by side, each in a different color. You should be able to do it in 10-15 lines. Here's an example, showing two calls to drawCourseName.

As usual, each version should follow the design recipe, and (in particular) be tested before you go on to the next version.


What to turn in

Grading standards

Problem Names and types Test cases Java Code
Square class and its instance variables /5 /5 /10
Square constructor /5 /5 /10
Square's toString() method /5 /5 /10
Square's getArea() method /5 /5 /10
Square's getPerimeter() method /5 /5 /10
Circle class and its instance variables /5 /5 /10
Circle constructor /5 /5 /10
Circle's toString() method /5 /5 /10
Circle's getArea() method /5 /5 /10
Circle's getPerimeter() method /5 /5 /10
WritingBuggle class /5 /5 /10
drawS() /5 /5 /10
drawC() /5 /5 /10
draw1() /5 /5 /10
draw7() /5 /5 /10
drawCourseName() /5 /5 /10
Error log, overall /80
Total: /400

Last modified:
Stephen Bloch / sbloch@adelphi.edu