CSC 171
Homework 7

Assigned Apr 5, due Apr 24

This assignment will involve the material of HtDCH chapters 1-15: defining classes, mixed types (using interface and implements), self-referential types (like StringList), writing methods for all of these kinds of classes.

CodeLab

CodeLab is an on-line collection of (mostly very simple) Java exercises to give you practice writing Java code. Click on an exercise number and you'll see a simple problem with a space for you to type in an answer. Once you've typed the answer, click "Submit" and you'll get immediate feedback: whether it was correct or not, and if not, some hints about what you did wrong. You're welcome to revise your answer and re-submit as many times as you wish; there's no penalty.

Register for CodeLab

  1. Point a browser at turingscraft.com
  2. Click "Register" in the upper right corner
  3. choose "I am a student in a course..." and click CONTINUE
  4. Enter the section access code ADELPH-1113-2033 and click CONTINUE
  5. Fill out the rest of the form in the obvious way, being sure to enter a valid email address, first and last names (they'll appear on my roster)

Once you've done this, you can use CodeLab for a trial period of 10 days or 10 problems. In fact, you can look at as many problems as you wish, but you can only submit ten of them before paying for the CodeLab service (which costs $25.00).

CodeLab assignment

Please do as many as possible of the exercises in the "Beginner language" section.

Programming

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.

  1. Define a class HotelRoom with five fields: the room number, how many king-sized beds it has, how many twin beds it has, how much it costs per night (in cents), and whether it's non-smoking. Be sure to include a constructor that allows people to specify the values of all five fields. Also write at least two examples of instances of the class, showing how to extract the values of various fields from them.

  2. Add a method named capacity to the HotelRoom class that computes how many people can sleep in the room. Assume that two people fit in a king, one in a twin, and nobody's on the floor or in the bathtub.

  3. Add a method named ok which takes in a number of people and a boolean indicating whether they want smoking or non-smoking, and tells whether the specified room will suit their needs (having at least enough sleeping capacity and the right smoking flag).

  4. Add a method named acceptable which does the same thing as ok, except that it also takes in another boolean indicating whether the customer cares about smoking vs. non-smoking. (In other words, if this boolean is false, then it doesn't matter whether the room is non-smoking; we only care whether there's enough sleeping capacity.)

  5. Add a method named charge which takes in a number of nights and returns how much the room will cost for that many nights. Anybody staying more than 3 nights gets a 10% discount on the entire stay.

  6. New April 17: Add a method named same which takes in another HotelRoom and tells whether it has the same field values as this one does.

  7. Define a type RoomList to represent a list of HotelRooms. Write several examples of RoomLists.

  8. Write a method countRooms for the RoomList class which tells how many hotel rooms are in the RoomList.

  9. Add a method anyOK for the RoomList 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.

  10. Add a method countOK for the RoomList 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.

  11. New April 18: I wanted you to write a same method for RoomList, but the technique I described in class on April 17 apparently doesn't work, due to a bug in ProfessorJ. I've reported the bug, but for now I don't think it's possible to write same for a type defined by choices in Beginner language. There is a way to do it in Intermediate language, which we'll see shortly.

  12. Add a method extractOK for the RoomList class which takes in a number of people and a boolean indicating whether they want smoking or non-smoking, and returns a RoomList of only the rooms that are OK.

  13. Add a method raisePrices for the RoomList class which takes in a number of cents, and returns a RoomList just like the original, but with every room's price increased by that much.

  14. Add a method cheapest for the NonEmptyRoomList class (or whatever you named it) which returns the lowest-priced room in a given list. Note that this doesn't make sense for an empty list of rooms, so we won't define it for that class.

    Hint: write an auxiliary method cheapestWithBackup which takes in a HotelRoom and works on any RoomList: 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 HotelRooms.

Grading standards

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
HotelRoom class /5 /5 /5 /5  
capacity method   /5 /5 /10
ok method   /5 /5 /10
acceptable method   /5 /5 /10
charge method   /5 /5 /10
same method   /5 /5 /10
RoomList type /15 /10 /10 /15  
countRooms method   /10 /10 /15
anyOK method   /10 /10 /15
countOK method   /10 /10 /15
extractOK method   /10 /10 /15
raisePrices method   /10 /10 /15
cheapest method   /20 /20 /30

General skills:

Following directions /10
Writing contracts from word problems /10
Choosing examples /10
Choosing names /10
Coding /10
Code re-use and function composition /10

Total:         /500


Last modified:
Stephen Bloch / sbloch@adelphi.edu