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.
Posn
class I gave you, add several
more methods:
addCoords
, which given a Posn returns the sum of
its x and y coordinates.scalePosn
, which given a Posn and a double,
returns, a Posn whose x and y coordinates are those of the given
Posn, multiplied by the double.addPosns
, which given two Posns, returns a Posn
whose x and y coordinates are the sums of the x and y coordinates
(respectively) of the parameters.distance
, which given two Posns, returns the
straight-line distance between them, using the formula Book
which represents a book, with a
title, author, year of publication, and ISBN. It should have at least
the following methods:
getTitle
, getAuthor
, etc.). Make
your instance variables private
.toString
, which given a Book, returns a nicely
formatted, human-readable String form of it.Date
which represents a date (day,
month, and year). For simplicity, we'll pretend that a year is 360
days long, with 12 months of 30 days each (we'll make this more
realistic in a week or two).
It should have at least the following methods:
private
. toString
, which given a Date, returns a nicely
formatted, human-readable String form of itdayInYear
, which given a Date, returns how many
days it has been since last New Year's Eve (for example, Jan. 1
would be day 1; Feb. 4 would be day 34; etc.)daysLater
, which
given a Date and a positive integer, returns a new Date that many days
later. RunnerLog
which represents an entry
in a runner's log book. Each entry has the date of the run, how many
miles, how many hours, and comments.
It should have at least the following methods:
private
.toString
, which given a RunnerLog, returns a nicely
formatted, human-readable String form of itavgSpeed
, which given a RunnerLog, returns the
average running speed in miles per hour for that run. addComment
, which given a RunnerLog and a String,
returns a new RunnerLog just like it, but with the String added onto
the end of whatever comments were already there.You should end up with several classes: Posn
,
Book
, Date
, and RunnerLog
, as
well as a test class for each, with names like PosnTest
,
BookTest
, DateTest
, and
RunnerLogTest
. I recommend keeping all of these in one
BlueJ project (with a name like "Homework3"); 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 |
---|---|---|---|---|
addCoords |
/5 | /5 | /5 | /10 |
scalePosn |
/5 | /5 | /5 | /10 |
addPosns |
/5 | /5 | /5 | /10 |
distance |
/5 | /5 | /5 | /10 |
Book class |
/10 | /10 | ||
Book constructor |
/5 | /5 | /5 | /10 |
Book getters |
/5 | /5 | /5 | /10 |
Book toString |
/5 | /5 | /5 | /10 |
Date class |
/10 | /10 | ||
Date constructor |
/5 | /5 | /5 | /10 |
Date getters |
/5 | /5 | /5 | /10 |
Date toString |
/5 | /5 | /5 | /10 |
Date dayInYear |
/5 | /5 | /5 | /10 |
Date daysLater (extra credit) |
/5 | /5 | /5 | /10 |
RunnerLog class |
/10 | /10 | ||
RunnerLog constructor |
/5 | /5 | /5 | /10 |
RunnerLog getters |
/5 | /5 | /5 | /10 |
RunnerLog toString |
/5 | /5 | /5 | /10 |
RunnerLog avgSpeed |
/5 | /5 | /5 | /10 |
RunnerLog addComment |
/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 |