CS 2102 - Bterm 08

Homework 3 - Designing Methods for Unions and Self-Referential Data

Due: Friday, November 7 at 11:59pm

Assignment Goals


The Assignment

In this assignment you will write some methods for geometric shapes, similar to what we were discussing in lecture.

Problems

For some of these problems you may need to use math functions in your solutions. The following example shows how you can use math functions (the example uses sqrt), and how to test doubles for equality (you can only make sure they are equal within some given tolerance).
class Foo{
  double x;

  Foo(double x){
    this.x = x;}

  double squareRoot(){
    return Math.sqrt(this.x);}
}

class Examples {
  Examples () {}

  Foo f = new Foo(16.0);

  boolean testSqRoot = 
    check this.f.squareRoot() expect 4.0 within 0.01;
}
  1. Using ProfessorJ Beginner language, write the data definitions that correspond to the following class diagrams. Make sure every class definition is accompanied by examples of data in the Examples class.
    
                            +-------+                            
                            | Shape |<--------------------------+
                            +-------+                           |
                            +-------+                           |
                                |                               |
                               / \                              |
                               ---                              |
                                |                               |
             ---------------------------------------            |
             |                  |                  |            |
      +--------------+   +--------------+   +--------------+    |
      | Square       |   | Circle       |   | Combo        |    |
      +--------------+   +--------------+   +--------------+    |
    +-| CartPt nw    | +-| CartPt center|   | Shape top    |----+
    | | int size     | | | int radius   |   | Shape bottom |----+
    | | IColor color | | | IColor color |   +--------------+ 
    | +-------------+  | +--------------+                     
    +----+ +-----------+
         | |
         v v
      +-------+
      | CartPt|
      +-------+
      | int x |
      | int y |
      +-------+
    

  2. Design the method totalArea that computes the total area of a shape. For the shape that consists of two components, add the areas. (The ProfessorJ Math library defines a constant called PI that you will need for this problem). For this and any other problem that asks you to design a method, make sure that your method is adequately tested in the Examples class.

  3. Design the method moveBy that produces a new shape moved by the given distance in the vertical and horizontal directions.

  4. Design the method isWithin that determines whether the given point is within this shape. If you get stuck, refer to Section 12.1 in the text for help.

  5. It would be nice to be able to draw the shapes on a canvas. The following code shows how you can draw one circle:
    import draw.*;
    import colors.*;
    import geometry.*;
    
    class Examples{
      Examples() {}
    
      Canvas c = new Canvas(200, 200);
      
      boolean makeDrawing =
        // initialize the canvas... 
        this.c.show() && 
        // ...and draw a solid circle on the canvas
        this.c.drawDisk(new Posn(100, 150), 50, new Red());
    }
    
    The three import statements on the top indicate that we are using code programmed by someone else and available in the libraries named draw, colors, and geometry. Open the Help Desk and look under the Teachpacks for How to Design Classes to find out more about drawing and the Canvas class. Understanding how to draw will be helpful when you design a game in the next homework assignment.

    Design the method drawShape that draws this shape on the given Canvas.


    What to Turn In

    Using web-based turnin, turn in a single file hw3.username.bjava (where username is the wpi login name of one of the partners in your homework pair) containing all code and documentation for this assignment. Both partners' names and wpi login names should appear in a comment at the top of the file.