CS 2102 - Bterm 09

Homework 2 - Designing Classes with the Design Recipe

Due: Thursday, November 5 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


Organizing your homework solutions

Please consider that you are writing up solutions for other people to read. Organize your work as follows: The TAs have to look at a lot of programs. You probably want the TA to be happy when he or she is grading your solution - do whatever you can to make it easy to follow your program.

Problems

  1. Using the Design Recipe for Data Definitions*, convert the data definitions for cities from Homework 1 to data definitions in the Beginner ProfessorJ language. Your class should be named City. You should have a separate class definition for Location to represent the locality's longitude and latitude. Make examples of data in an Examples class.

    Define a class hierarchy to represent a list of cities. Make examples of data.

    Define a class hierarchy that represents a list of strings (this data definition could be used to represent a list of states, where each state is identified by a two-letter string). Make examples of data.

    *Design Recipe for Data Definitions

  2. 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 shapes. Use the CartPt class to represent the positions of shapes. Use the IColor class to represent the colors of the shapes. Use the Helpdesk in DrScheme to learn how to access the ProfessorJ colors library (Teachpacks/HtDC Teachpacks).

    Make examples of data.

  3. 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 the method toPosn that produces an instance of the class Posn from this CartPt. The Posn class is defined in the ProfessorJ geometry library (look in the Helpdesk for information about the geometry library).

    Design the method distanceTo() that computes the distance (a double) from this CartPt to the given CartPt.

    Design the method contains() that determines whether this shape contains the given CartPt. (Note: To do this correctly for the class Line requires a bit of geometrical computation. You are allowed to substitute, in this case, a method that produces false for all inputs.)

    Design the method draw() that draws this shape on the given Canvas (refer to the documentation in the draw library on Canvas). You can display shapes on a Canvas by providing statements such as the following in your Examples class:

      Canvas c = new Canvas(100, 100);
    
      boolean showShape = this.c.show() && this.myShape.draw(this.c);
    
    where myShape is an example of a shape that has been defined in the Examples class.

    *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.