©2010 Felleisen, Proulx, et. al.

3  Methods for Simple Classes

Portfolio Problems

Work out as complete programs the following exercises from the textbook. You need not work out all the methods, but make sure you stop only when you see that you really understand the design process.


  1. The following Scheme data definition describes the contents of a web site:

    ;;A Web Page (WP) is (make-wp String String [Listof Item])
    (define-struct wp (url title items))
    ;; An Item is one of
    ;; -- String
    ;; -- Image
    ;; -- Link
    ;; An Image is (make-image String int Symbol) 
    (define-struct image (file-name size file-type))
    ;; A Link is (make-link String WP)
    (define-struct link (name page))

    1. Describe in English, on paper a web page with at least two of each kind of items and at least two levels of links.

    2. Draw a class diagram for the classes that represent this data definition.

    3. Define FunJava classes that represent web pages as defined above.

    4. Design the data representation of the example you have defined earlier.

    5. Ask your neighbor for their data representation and describe the information it represents.

  2. Problem 10.3 on page 97

  3. Problem 10.4 on page 97

  4. Problem 11.2 on page 116

  5. Problem 12.1 on page 129

  6. Problem 12.4 on page 131

Pair Programming Assignment

3.1  Problem

Start with the file City.java from Problem 2.1 from the previous assignment.

Design the following methods for the class that represents one city:

  1. the method sameState that determines whether a city is in the given state.

  2. the method isSouthOf that determines whether one city is located South of another city.

  3. Design the method distanceTo that computes the distance from one city to another. Estimates tell us that one degree of longitude (at our latitude) is approximately 55 miles and one degree of latitude is approximately 70 miles. (Feel free to make more accurate estimates. If you do so, include a comment explaining how you arrived at your estimates.)

  4. Design the method toPosn that produces a Posn that corresponds to the location of this city in a 100 x 100 Canvas. (Add import geometry.*; statement to the beginning of your program.)

    Assume that the latitude and longitude lines are parallel to the Canvas boundaries (parallel projection). Assume that

    • the left edge of the Canvas is the 125 degrees of longitude (in the Western hemisphere)

    • the right edge is 65 degrees of longitude (in the Western hemisphere)

    • the top of the Canvas is 50 degrees of latitude (in the Northern hemisphere)

    • the bottom is 20 degrees of latitude (in the Northern hemisphere)

    We focus only on the contiguous continental states (omitting the beautiful states of Alaska and Hawaii).

  5. Design the method draw that shows this city as a small disk in the given Canvas. Assume the Canvas has the size 100 x 100. You may want to also show the name of the city.

    Note: These is no way one can test this method. However, include the code that will display at least three cities in a Canvas.

3.2  Problem

The file Banking.java contains the definitions of classes the represent bank accounts.

  1. Make examples of the following accounts:

    • A checking account for Adam Smith with id 123, a minimum balance of $50 and current balance of $150.

    • A savings account for Betty Jones with id 456, a balance of $120 and interest rate of 2.5%.

    • A certificate of deposit account for Pat Malloy with id 334, a balance of $300 that has not yet matured.

  2. Design the method amtAvailable for the classes that represent bank accounts that produces the amount that the customer can withdraw from the account.

  3. Design the method moreAvailable that determines whether one account has more available for withdrawal than another account.

  4. Design the method withdraw that produces a new account with the given amount withdrawn. If the amount the customer wants to withdraw exceeds the available amount, no money will be withdrawn.

    Note: Later we will learn how we can signal that the transaction is not valid.

3.3  Problem

Creative Project

Select the simplest version of your game. Your data definitions should not contain self-reference (no lists, binary trees, combo shapes). If you wish, your world may contain a fixed number of objects of the same kind (e.d. five fish, four invaders, etc.).

Design the following methods for this simple version of your game:

  1. the method draw that will display the world state in the given Canvas.

    Include in the Examples class a visual test that shows the initial world and a world at some point during the game. The lab sample program DrawFace.java shows you how to make this happen.

  2. Add to your class the following method:

      // signal the end of the world and display the final message
      MyWorld endOfWorld(String message){
    	  return this;

    (do not change anything here, other than the name of your world class. Invoke it in the two methods you define below, when the conditions for the ending of the game are satisfied.

  3. the method onKeyEvent that consumes a String and produces a new instance of your world in response to the given key. The arrow keys are defined as "left", "right", "up", and "down", the space bar is defined as "space".

    Note: If some key event leads to the end of the game, that case should return this.endOfWorld("end of world message").

  4. the method onTick that produces a new instance of your world after one clock tick elapsed.

    Note: If on tick we determine that the game ends, that case should return this.endOfWorld("end of world message)".

Design one method at a time, make sure you follow the Design Recipe, and once all the parts are there, you are almost ready to run the game.

Note: I will be more impressed with a well designed simple game than with a game that has all kinds of fancy options, but the code is not readable, methods are jumbled together, there are no tests, and there are no purpose statements.

When you are ready to run the game do the following:

3.4  Problem

Finish the last problem from Lab 3 that deals with Strings and submit it as the fourth problem of this assignment.

Last modified: Wednesday, September 22nd, 2010 2:58:16pm