Data Structures Test

  1. Suppose that you are writing a class TPass to represent a monthly T pass. It contains two pieces of information: an int telling the date of its expiration (e.g. 20061231) and a String indicating its type ("Bus", or "Subway" only, to simplify things, and assume that a "Subway" pass also works for the bus (like a combo pass on the MBTA)).

    A TPass can be created as you would expect, and its variables can be retrieved:

    TPass pass = new TPass("Subway", 20070131);
    int date = pass.getDate();
    String type = pass.getType();

    When a TPass is converted to a String, it looks like this:

    Bus pass (expires 20061230)

    Subway pass (expires 20070131)

    The compareTo() method is used to check if a pass is usable in a given context. For example, if I write pass.compareTo(new TPass("Subway", 20061219)), I am asking the question, "Can this pass be used to ride the subway on December 19, 2006?" The pass is automatically "less", returning a negative number, if it is a "Bus" pass being compared to a "Subway". Otherwise, compareTo() should return a positive number if this expires after the date of the given pass, a negative number if it expires before, and 0 if they expire on the same day.

    Write the entire TPass class below. You do not need to write an equals() method. Move on to the next page if you need more space.













  2. For this problem, refer back to the Code for Data Structures Test handout, which defines a Book class. Here are some of Mr. Z.'s favorite books:

    Book dickensTale = new Book("Dickens, Charles",
          "Tale of Two Cities, A", 345, 1859);
    Book hellerCatch = new Book("Heller, Joseph",
          "Catch 22", 463, 1961);
    Book dostoevskyCrime = new Book("Dostoevsky, Fyodor",
          "Crime and Punishment", 671, 1866);
    Book dostoevskyBrothers = new Book("Dostoevsky, Fyodor",
          "Brothers Karamazov, The", 814, 1879);

    a) Can I change the variables of a book after I have created it? If so, how? If not, why not?



    b) Suppose that I have a book book and I want to find out if it is more than 200 pages long. Write an if statement that will check if this is the case.



    c) Suppose that I want to find out if book was written by Dickens (that is, its author is equal to "Dickens, Charles"). Write an if statement that will check if this is the case.



    d) What will be output if I type the following?

    hellerCatch.toString()




    e) For each of the following, say whether the return value is positive (+), negative (-), or zero (0). Remember, if I ask an object to compare itself to another object, it will return 1 if it is greater, -1 if it is less, and 0 if it is equal to that other object.

    dickensTale.compareTo(hellerCatch);

    hellerCatch.compareTo(hellerCatch);

    dostoevskyCrime.compareTo(dostoevskyBrothers);

    f) Suppose that I have two different editions of the same book. They are published in different years, with different numbers of pages, but have the same author and title. Will they be considered equals() to each other? Will doing compareTo() of one to the other return 0? Explain.


  3. The data structure shown to the right is for a farebox to be used with the T Pass system you worked on in the first problem.

    Suppose I have a variable Farebox box.

    a) How can I find out whether it is busy?


    b) How can I find out how much fare is charged by the coin slot?


    c) How can I add $.25 to the amount paid in the coin slot?



    d) If the card reader breaks, how would I create a new card reader, charging the same fare, and replace the old one with the new one?
    (Make reasonable assumptions about CardReader's constructor)




  4. The setPaid() method in CoinSlot is actually a bit more complicated than you would expect, because if the amount paid so far is more than the fare, then paid is set back to 0. Also, once paid is set, the farebox is set to be busy or not depending on whether paid is 0.

    Write in the setPaid() method in the class below.

    public class CoinSlot {
       private FareBox box;
       private double fare;
       private double paid;