For all the programming assignments, be sure to follow the design recipe.
Each problem asks you either to create a new class with some methods,
or to add a new method to a class you've already written.
For classes, you need to identify the names and types
of the instance variables, then write each of the methods.
For each method, go through
the usual contract, examples,
skeleton, inventory,
body, test steps.
I recommend writing the examples by using the
tester
library, as discussed in class on Feb. 11, so you
can easily run all your tests and confirm that they all pass.
Be sure to choose meaningful names for methods and parameters, and watch for opportunities to re-use methods you, I, or the textbook have already written.
Also turn in a log of time spent and errors encountered in the assignment, with brief comments describing each error ("mismatched parentheses" is self-explanatory, but more complex errors might need more description). You may do this using the PSP forms, or simply by keeping track in a text file or on paper and turning it in.
Dummy
class, write the following method:
smallestOf3
, which takes in three numbers and
returns the smallest of them.Posn
class I gave you, add several
more methods:
aboveDiagonal
, which given a Posn, tells whether it
is above the main diagonal (the line y=x) of a square graphics window.
Note that in computer graphics, larger x coordinates are to the right as usual,
but larger y coordinates are down, not up as in high school
algebra. So the diagonal is as shown in this picture; the green dot is
above it and the red dot is below it.same
, which given two Posns, tells whether they have
the same coordinates.Posn
represents a mouse-click on a
video-game window that's 300 pixels wide, and you want to do different
things depending on whether the mouse click was in the left third, the
middle third, or the right third of the window. Develop a method
region
, which given a Posn, returns one of the
strings "left"
, "middle"
, or
"right"
depending on whether the Posn is in the left 100
pixels, the middle 100 pixels, or the right 100 pixels of the
300-pixel-wide window.Voting
which has no instance
variables, but the following (static) methods:
count4votes
, which takes in five strings: the first
is a candidate name, and the other four are votes cast. It returns how
many of the votes were for that candidate. t.checkExpect (Voting.count4votes("Anne","Bob","Charlie","Bob","Hortense"), 0); t.checkExpect (Voting.count4votes("Anne","Bob","Anne","Phil","Charlie"), 1); t.checkExpect (Voting.count4votes("Anne","Anne","Bob","Anne","Mary"), 2); t.checkExpect (Voting.count4votes("Bob","Anne","Bob","Charlie","Bob"), 2);
whoWon
, which takes in three integers: how many
votes Anne got, how many Bob got, and how many Charlie got. It
returns the name of the winner, or "tie" if two or more candidates
tied for first place. (This method should not assume there
are only four voters; it should work correctly with hundreds or
thousands.)whoWon4votes
, which takes in four strings,
representing the votes cast by four voters; it returns the name of
the winner or "tie". You may assume for now that the only
candidates in the race are Anne, Bob, and Charlie (we'll drop this
assumption later). (Hint: look for opportunities
to re-use methods you've written before.)Dummy
class, write the method
cardRefund
, which takes in how much you charged on
your credit card, and tells how much refund you get at the end of
the year, according to the following rules:
Date
class from homework 3,
legal
method that, given a Date, tells
whether it's "legal": the month and day numbers are both positive,
and day number is no more than 30, and the month number is no more
than 12.same
method that, given two Dates, tells
whether they're the same. (You may assume that both Dates are
legal.)monthName
method that takes in a month
number and returns the name of that month. (Hint: At
present, you'll need to use if
; later on we'll see
a more elegant way.)toString
method so that it uses month
names properly.dayInYear
method so it knows that January is 31 days,
February 28, March 31, April 30, May 31, June 30, July 31, August 31,
September 30, October 31, November 30, and December 31. Don't worry
about leap years for now.
(Hint: You might want to write a "helper
method" to make this easier.)daysLater
method from homework 3 (or
add it if you didn't write it before) so it handles the lengths of
months correctly.You should end up with several classes: Dummy
,
Posn
, and Date
, as
well as a test class for each, with names like PosnTest
,
DateTest
, etc.
I recommend keeping all of these in one
BlueJ project (with a name like "Homework4"); zip up the whole
folder and attach it to an e-mail to me. If you've entered your time
and error logs using the on-line forms, I've already got them; if not,
e-mail me your log files.
Note: I won't actually grade every one of these methods, but a representative sample; the rest are for practice. If I were to grade them all, the points would look something like this:
Class/method name | Contract (for methods) | Examples (for methods) | Skeleton and inventory (for methods) or variable list (for classes) |
Class/Method definition |
---|---|---|---|---|
smallestOf3 |
/5 | /5 | /5 | /10 |
aboveDiagonal on Posns |
/5 | /5 | /5 | /10 |
same on Posns |
/5 | /5 | /5 | /10 |
region on Posns |
/5 | /5 | /5 | /10 |
count4votes |
/5 | /5 | /5 | /10 |
whoWon |
/5 | /5 | /5 | /10 |
whoWon4votes |
/5 | /5 | /5 | /10 |
cardRefund |
/5 | /5 | /5 | /10 |
legal on Dates |
/5 | /5 | /5 | /10 |
same on Dates |
/5 | /5 | /5 | /10 |
monthName |
/5 | /5 | /5 | /10 |
improved toString on Dates |
/5 | /5 | /5 | /10 |
XC: change dayInYear to handle month lengths |
/5 | /5 | /5 | /10 |
XC: change dayInYear to handle leap years |
/5 | /5 | /5 | /10 |
XC: change daysLater to handle month lengths |
/5 | /5 | /5 | /10 |
Following directions | /10 |
Writing contracts from word problems | /10 |
Choosing examples | /10 |
Names, white space, indentation, general readability | /10 |
Coding | /10 |
Code re-use and method composition | /10 |