This assignment will involve the material of HtDCH chapters 16-19 (abstract classes and refactoring, designing hierarchies of classes, access specifiers, overloading, local variables, assignment and mutation, sequence, void methods), as well as some material given out in class on arrays and loops.
Do as many problems as you can from the "Beginning Language", "Intermediate Language", and "Advanced Language" sections.
Re-read the Web page on Adages of Software Engineering. Choose one (or several closely-related) adages, or one longer article, that mean more to you than they did in January. Write an essay of one or more well-structured paragraphs: what does the adage really mean in practice? Do you agree or disagree? Support your claims with specific examples from your own programming experience.
For all the programming assignments, be sure to follow the design recipe; here are some practical suggestions on how to do that in Java.
As in Scheme, be sure to choose meaningful names for functions and parameters, and watch for opportunities to re-use functions you, I, or the textbook have already written.
Write and test each method before going on to the next one; in some cases, the next one depends on the previous one.
Also turn in a log of how many errors of different kinds you encountered in the assignment, with brief comments describing each one ("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.
Modify your HotelRoom
and
ConsRoomList
classes from homework
7 by making all the fields private
and adding
"getter" methods for them. This may require changing some
test cases to use the getters rather than the field names. I recommend
also changing your same
method to use getters rather than
field names, even though it's in the same class and it could
use the field names.
Add a method raisePriceMutating
to
the HotelRoom
class which takes in an int (how many cents
to raise the price), and increases the price of the
HotelRoom
by that much. It should return nothing at all.
Define a class RoomArray
which
stores a bunch of HotelRoom
s in a single field of type
HotelRoom[]
. Write several examples of
RoomArray
s.
Write a method countRooms
for the
RoomArray
class which tells how many hotel rooms are in the
RoomArray
.
Add a method anyOK
for the
RoomArray
class which takes in a number of people and a
boolean indicating whether they want smoking or non-smoking, and tells
whether there is an ok room anywhere in the list. Use an auxiliary
method that does recursion on natural numbers.
Add a method countOK
for the
RoomArray
class which takes in a number of people and a
boolean indicating whether they want smoking or non-smoking, and tells
how many room in the list are OK. Again, use an N-recursive
auxiliary method.
Add a method named same
which takes in another RoomArray
and tells whether it is the
same as this one. Use an N-recursive auxiliary method.
Add a method raisePricesMutating
for the
RoomArray
class which takes in a number of cents,
mutates the rooms in the array to raise their prices, and
returns nothing. Use an N-recursive auxiliary method which returns
nothing.
Add a method cheapest
for the
RoomArray
class which
returns the lowest-priced room in a given list. Note that this doesn't
make sense for an empty list of rooms; in that case, have it return the
special value null
(a predefined constant, to which you can
refer the same way you refer to true
or false
).
Hint: write an auxiliary method
cheapestWithBackup
which takes in a HotelRoom
and works on any RoomArray
: it pretends the given
room were part of the list, and returns the cheapest room in this larger
list. It may also help to write another auxiliary method
chooseCheaper
which chooses the cheaper between two
HotelRoom
s. You may need yet another helper function
doing recursion on natural numbers, or you can add a natural-number
parameter to cheapestWithBackup
and have it do the
recursion itself.
Define another class LoopRoomArray which
also stores a bunch of HotelRooms in an array, but implements
all the same methods (anyOK
, countOK
,
same
, raisePricesMutating
, cheapest
)
with while-loops or for-loops rather than recursion.
Hint: It has a lot in common with
RoomArray
; see how much you can refactor.
Error log: /25
(I'm not grading on how many or how few errors you encountered,
only on whether you recorded them adequately.)
Method or class name | Class def'n | Fields | Constructor | Examples | Method Header | Examples | Body |
---|---|---|---|---|---|---|---|
modified HotelRoom class |
/5 | ||||||
Getter methods | /5 | /5 | /10 | ||||
raisePriceMutating method |
/5 | /10 | /10 | ||||
RoomArray class |
/5 | /5 | /5 | /10 | |||
countRooms method |
/5 | /5 | /10 | ||||
anyOK method |
/10 | /10 | /15 | ||||
countOK method |
/10 | /10 | /15 | ||||
same method |
/20 | /20 | /30 | ||||
raisePricesMutating method |
/10 | /10 | /15 | ||||
cheapest method |
/20 | /20 | /30 | ||||
LoopRoomArray class |
/5 | /5 | /5 | /10 | |||
countRooms method |
/5 | /5 | /10 | ||||
anyOK method |
/10 | /10 | /15 | ||||
countOK method |
/10 | /10 | /15 | ||||
same method |
/20 | /20 | /30 | ||||
raisePricesMutating method |
/10 | /10 | /15 | ||||
cheapest method |
/20 | /20 | /30 |
Following directions | /10 |
Writing contracts from word problems | /10 |
Choosing examples | /10 |
Choosing names | /10 |
Coding | /10 |
Code re-use and function composition | /10 |