copyright 2012 Felleisen, Proulx, et. al.

CS 2510 Spring 2012: Assignment 4 - Methods for Unions; Abstracting over Data Definitions

Practice Problems

Work out as the following exercises from the textbook. Do not add them to your assignment submission, though you should keep them in your electronic portfolio in your own svn repository.

Problems:

Problem 15.8 on page 175

Note: Of course, to do this problem you need to also define the classes that represent this problem and make examples of data - though all of that is already shown in the textbook.


Pair Programming Assignment

These problems should be checked into your pair's SVN repository by the due date.

Project naming requirements

The names of the projects and some of the project files must be exactly the same as specified in the assignment. Failure to do so makes it impossible for the graders to run your submission and results in immediate loss of at least 50% of the homework credit.

Every file you submit must have a header that identifies you and the problem it solves. So, for the first problem the header should look like this:

// Assignment 4 Problem 1
// Partner Name1
// partner1username
// Partner Name2
// partner2username
// 31 January 2012

Problem 1

Create a project with the name HW04Problem1.

You are trying to organize the file directories on your computer. Your friend gave you a start by designing the data definitions given in the FilesDirectories.java file. She even gave you some examples of data. Import this file into your project and run it to see what is going on.

  1. Make an additional example(s) of data that allows us to represent the following directory and its contents:

    Directory Pages
      contains the following:
          file index.html
          file pictures.html
          directory Pictures
          directory Quotations
    Directory Pictures
      contains the files:
          home.jpeg,
          friend.jpeg.
          brother.jpeg
    Directory Quotations
      contains the files:
          twain.txt,
          cervantes.txt
    

    Choose any sizes for these files. Assume the sizes are given in kiloBytes.

  2. Design the method totalSize that computes the size of the directory by adding the sizes of all files in it. Additionally, every directory needs 4 kiloBytes for keeping track of its list of files and subdirectories.

  3. Design the method allFiles that produces a list of all files of the given kind in the directory (and the names of all files in any of its subdirectories, sub-subdirectories...).

    Note: You must include the templates for all classes in this problem, except the interfaces and classes that represent empty lists.

    Note: Use helper method where appropriate.

Problem 2

Create a project with the name HW04Problem2. Your java file must be named CartPt.java and the Examples class inside this file must be named ExamplesCartPt.

  1. Copy the following class into the file CartPt.java:

    // to represent a location on the canvas
    class Posn{
      int x;
      int y;
    
      Posn(int x, int y){
        this.x = x;
        this.y = y;
      }
    }
     

    Next week, this class will be a part of the library that supports the design of interactive games. That means, we cannot add anything to it, and if we need to add new behavior, we need to extend this class.

  2. Design the class CartPt that extends the library class Posn. We will not add any new fields, but by extending the library class we can add new methods to it.

  3. Design the following methods for the class CartPt:

  4. Method distTo that computes the distance from this point to the given one.

  5. Method directionTo that produces a String that tells us in which direction do we have to travel from this point to the given one. Make sure you cover the following possibilities:

    going North, East, West, South, Northwest, Northeast, Southwest, and Southeast.


Problem 3

Work out the last problem from Lab 4: Abstracting over data definitions: Lifting fields, lifting methods.


Last modified: Tue Jan 31 17:53:43 EST 2012