©2010 Felleisen, Proulx, et. al.

4  Abstracting over Data Definitions, Methods

Practice Problems

Practice problems help you get started, if some of the lab and lecture material is not clear. You are not required to do these problems, but make sure you understand how you would solve them. Solving them on paper is a great preparation for the exams.

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.

Problems:

  1. Problem 19.4 on page 271

  2. Problem 19.5 on page 271

  3. Problems 19.6 - 19.11 on page 276-279

    You may complete this part with animation using the draw library.

Pair Programming Assignment

4.1  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: In the revised version below signal that the transaction is not valid.

Revise the solution and hand in only the revised version.

  1. Define an abstract class AAccount and lift into it all fields that are common to all accounts.

  2. Revise the method amtAvailable for the classes that represent bank accounts: can it be lifted to the abstract class? - or does it have to be defined in each class anyway?

  3. Revise the method moreAvailable that determines whether one account has more available for withdrawal than another account: can it be lifted to the abstract class? - or does it have to be defined in each class anyway?

  4. Revise the method withdraw that produces a new account with the given amount withdrawn: can it be lifted to the abstract class? - or does it have to be defined in each class anyway?

    If the transaction is not valid throw an exception and describe the problem using a different message for the different kinds of accounts.

  5. Define the method sameName that determines whether two accounts have the same name.

4.2  Problem

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

Design the following methods for the class CartPt:

Design the class WorldPt that extends the CartPt as follows:

4.3  Problem

Make a final revision of your game. Where appropriate, add lists of game components, or other collection of objects.

The grading rubric for this problem will be as follows:

Last modified: Thursday, May 20th, 2010 3:49:20pm