CS 2510 Spring 2012: Lab 4 - Methods for Self-Referential Data; Abstracting over Data Definitions

copyright 2012 Felleisen, Proulx, et. al.

Goals

The goals of this lab are to practice designing methods for complex class hierarchies, to learn a bit about the Java class that handles Strings, and to learn how to design and use abstract classes and subclasses to eliminate code duplication and to extend the functionality of libraries.

1  Methods for Self-Referential Data: Mobiles

Start the lab by downloading the file Mobile.java. Build a project named Mobile and import the file Mobile.java into it. Add the tester.jar to the classpath, set us a new RunConfiguration that allows you to run the program, and run the program.

The goal of this problem is to design class hierarchy that represents hanging mobiles (shown below) and design some methods that provide information about the specific mobile structure.

The file Mobiles.java contains the data definitions, examples of data, and the method countWeights.

  1. Make an additional example of mobile data that represents the following mobile (The number of dashes in the struts and lines represents their length)

                         |
              - - - - - - - - - - - -
             |                       |
            60                       |
           blue           - - - - - - - - - -
                         |                   |
                         |                  40
                      - - - - - -           red
                     |           |
                    10           |
                   green         5
                                red
    
  2. Design the method totalWeight that computes the total weight of a mobile. The weight of the lines and struts is given by their lengths (a strut of length n has weight $n$).

  3. Design the method height that computes the height of the mobile. We would like to hang the mobile in a room and want to make sure it will fit in.

Make sure you keep updating the TEMPLATE as you go along. (We have already started you on your way.)

When designing a method, copy the template from its fixed position, add to the template any new information based on the arguments the method consumes, then design the method. Once you have the method body, you can delete the template you have used.


2  Understanding the String class

For this problem start with the file Strings.java that defines a list of Strings.

Note: The following method defined for the class String may be useful:

// does this String come before the given String lexicographically?
// produce value < 0   --- if this String comes before that String
// produce value zero  --- if this String and that String are the same
// produce value > 0   --- if this String comes after that String
int compareTo(String that)
  1. Design the method isSorted that determines whether the list is sorted in alphabetical order.

  2. Hint: You may need a helper method. You may want remember to the accumulator style functions we have seen in Scheme.

  3. Design the method merge that consumes two sorted lists of Strings and produces a sorted list of Strings that contains all items in both original lists (including duplicates).


Abstracting over Data Definitions

3  Review of designing methods for unions of classes

A file in a computer can contain either a text, or an image, or an audio recording. Every file has a name and the owner of the file. There is additional information for each kind of file as shown in the program Files.java.

Download the file and work out the following problems:

  1. Make one more example of data for each of the three classes and add the tests for the method size that is already defined.

  2. Design the method downloadTime that determines how many seconds does it take to download the file at the given download rate.

    The rate is given in bytes per second.

  3. Design the method sameOwner that determines whether the owner of this file is the same as the owner of the given file.

Save the work you have done. Copy the file and continue.

3  Abstracting over data definitions: Lifting fields, lifting methods.

Save your work. Possibly start a new project and import the file into it. Alternatively, save the a copy of the file you have been working on in another folder.

  1. Look at the code and identify all places where the code repeats --- the opportunity for abstraction.

    Lift the common fields to an abstract class AFile. Make sure you include a constructor in the abstract class, and change the constructors in the derived classes accordingly. Run the program and make sure all test cases work as before.

  2. For each method that is defined in all three classes decide to which category it belongs:

    Now, lift the methods that can be lifted and run all tests again.

Note: You can lift the method sameOwner only if you change its contract. Do so --- make sure you adjust the test cases accordingly.

Last modified: Mon Jan 30 17:11:16 EST 2012