©2006 Felleisen, Proulx, et. al.

3  Designing Methods -- Part 2

We continue with the theme of the photo images. Our goal is to design methods that answer questions about lists of images and manipulate these lists.

We will also work with geometric shapes and learn to draw geometric shapes as images on the Canvas.

3.1  Methods for Self-Referential Data

In the previous lab you designed a list of photo images. We first design the method that counts the images in our list. (We use the simpler version of the class Photo that was defined in Lab 1.

Note: Of course, you will quickly realize that this method will look the same regardless of the pieces of data contained in the list. We will address that issue later on, once we are comfortable dealing with lists that contain specific items.

Below is an example of how to design a method that counts the number of pictures in a list of photo images.

The method deals with ListOfPhotos. We have an interface ListOfPhotos and two classes that implement the interface, MTListOfPhotos and ConsListOfPhotos. When the DESIGN RECIPE calls for the method purpose statement and the header, we include the purpose statement and the header in the interface ListOfPhotos and in all the classes that implement the interface.

Including the method header in the interface serves as a contract that requires all classes that implement the interface to define the method with this header. As the result, the method can be invoked by any instance of a class that implements the interface - without the need for us to distinguish what is the defined type of the object.

We can now proceed with the DESIGN RECIPE.

Design the methods that will help you in dealing with your photo collection:

  1. Before burning a CD of your photos, you want to know what is the total size in bytes of all photos in the list of photos.

  2. You now want to go over the list of photos and select only the photos in the jpeg format.

  3. Finally, you want to sort the list of photos by the name of the image (as typically these are generated by your camera and represent the date and time when the photo was taken).

3.2  Quiz

You have 10 minutes.

3.3  Methods for Self-Referential Data -- Part 2 Graphics and Key Events

In the previous lab you defined classes that represent different geometric shapes - a circle, a square, and a shape that is a combination of two shapes, the top and the bottom one. Recall the data definition as given by the class diagram:

                        +-------+                            
                        | Shape |<--------------------------+
                        +-------+                           |
                        +-------+                           |
                            |                               |
                           / \                              |
                           ---                              |
                            |                               |
         ---------------------------------------            |
         |                  |                  |            |
  +-------------+    +-------------+    +--------------+    |
  | Square      |    | Circle      |    | Combo        |    |
  +-------------+    +-------------+    +--------------+    |
+-| Posn nw     |  +-| Posn center |    | Shape top    |----+
| | int size    |  | | int radius  |    | Shape bottom |----+
| | Color color |  | | Color color |    +--------------+ 
| +-------------+  | +-------------+                     
+----+ +-----------+
     | |
     v v
  +-------+
  | Posn  |
  +-------+
  | int x |
  | int y |
  +-------+

  1. Design the method that computes the total area of a shape. For the shape that consists of two components add the areas - as if you were measuring how much paint is needed to paint all the components.

    You will need to use math functions, such as square root. The following example show how you can use the math function, and how to test doubles for equality. (You can only make sure they are different only 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 testSquared = 
        check this.f.squareRoot() expect 4.0 within 0.01;
    }
    

  2. Design the method that produces a new shape moved by the given distance in the vertical and horizontal direction.

  3. Design the method that determines whether the given point is within this shape.

  4. Of course, we would like to draw the shapes on a canvas. The following code (that can be written within the Examples class) shows how you can draw one circle:

    import draw.*;
    import colors.*;
    import geometry.*;
    
    class Examples{
      Examples() {}
    
      Canvas c = new Canvas(200, 200);
      
      boolean makeDrawing = 
        this.c.show() && 
        this.c.drawDisk(new Posn(100, 150), 50, new Red());
    }
      

    The three import statements on the top indicate that we are using the 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 the teachpacks for How to Design Classes to find out more about drawing and the Canvas.

Save all your work -- the next lab will build on the work you have done here!

If you have some time left, work on the Etudes part of the homework.

Last modified: Sunday, January 28th, 2007 11:02:11pm