For each programming problem below, you should follow a 6-step recipe:
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.)
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.
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),
Square
class, with its instance variable(s).Square
class.toString
to the Square
class.getArea()
to the Square
class.getPerimeter()
to the Square
class.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.
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
:
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.
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.)
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.
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.
You've written three new classes: Square
,
Circle
, and WritingBuggle
, so you need to turn
in three files Square.java
,
Circle.java
, and WritingBuggle.java
.
(I don't need to see the trivial modification of BuggleWorld
.
Also, Java technically allows you to put several different classes in
one .java file, but I don't recommend that.)
I also want to see your development logs. If you prefer, the
names and types and the test cases may now be in the program file
itself, in comments, and the errors may be in
PSP, in which case
there's no separate log file to turn in. If not, I suggest logging the
information about the very-similar classes Square
and
Circle
in one file, with the information about
WritingBuggle
in another file.
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 |