CS 2102 - Dterm 10

Homework 2 - Designing Classes with the Design Recipe

Due: Friday, March 26 at 5pm


Acknowledgement: This assignment is a modified version of an assignment designed by Dr. Viera Proulx.

This assignment and all subsequent assignments are to be done with your homework partner.

Assignment Goals


Preliminaries

In this homework you will be displaying shapes on a Canvas. You will need to download the library files that allow you to draw, specify colors, and use the Posn and Canvas classes. Here's the link to the libraries. Go to the Downloads link, and from there save colors.jar, geometry.jar, and draw.jar in your EclipseJars folder (on your toaster drive or your own computer). When you create your Eclipse project you will need to add each of these libraries as "external jars" along with the tester library.

You can find documentation for the classes provided by these libraries here. Follow the links from the "draw" and "colors" libraries to the Javadocs (Java documentation) for draw, colors, and geometry. You will need import statements for any .java file that refers to a class that is defined in one of the libraries. Here are the import statements you will be using:

import colors.*;     // when you need to use an IColor
import geometry.*;   // when you need to use a Posn
import draw.*;       // when you need to use a draw method, or a Canvas
import tester.*;     // when you need to use checkExpect, checkInexact, or Tester

Restrictions (and why)

For this homework, and for all remaining labs and homework assignments, you will be restricted to using only those features of the Java language that we have previously covered during the lectures. For Homework 2, the only kinds of statements you may use in your method implementations are the if and return statements. In particular, you are not allowed to use any assignment statements (i.e. the "=" operator) except as they have been used in constructors. Also, you are not allowed to use any of Java's type-checking mechanisms - we will cover those at a later time, when their use is required.

There are a couple of reasons why we are restricting your use of Java in these ways. First, we want you to understand Java's dynamic-dispatch mechanism (polymorphism) which is at the heart of object-oriented programming. Let Java figure out types of objects for you! Secondly, we want to avoid assignment statements for the time being, because (as we began to discover in CS 1101) assignments introduce side-effects, making testing more difficult. When we get to problems that can't be solved any other way, we will then use assignment.

Problems

  1. Design a class hierarchy to represent shapes that may be drawn on a Canvas. Here are the shapes you should define:

    Draw a class diagram of a class hierarchy that represent shapes. You won't be submitting it, but you should always start by drawing a class diagram. If you come for office hour help, we will ask you to show us your diagram.

    Define a class CartPt to represent a Cartesian point with integer coordinates.

    Define the classes that represent the various shapes. Use the CartPt class to represent the positions of shapes. Use the IColor interface to represent the colors of the shapes.

    Make examples of data.

  2. In this part of the homework you will use the Design Recipe for Methods* to design methods for your Shape classes. Make sure you provide test cases in the Examples class as you develop each method.

    *Design Recipe for Methods

What to Turn In

Using web-based turnin, turn in a single file containing all code and documentation for this assignment. Follow the naming conventions when naming your file. Both partners' names and wpi login names should appear in a comment at the top of the file.