CS 101 (Spring 2000)
Lab 2: Objects as containers: Cartoon apparel

Lab Assigned
Design Due
(In class)

10 AM
Implement
(In Lab)
Demo
(In Lab)
Lab Due
(In class)
Friday
10 AM
31 Jan None 1-2 Feb 8-9 Feb 11 Feb


Goals:

By the end of this lab, you should Labs in this course typically contain the following three parts:
  1. Problem statement
  2. Design
  3. Implementation
For all labs, we provide you with a problem statement---a characterization of the problem you are to solve. In this particular lab, we take you through the process of design. Specifically, we will articulate Although your graded work for this lab is the implementation you will carry out based on our design, it is important that you follow our design and design process, because in a few weeks, you will be designing solutions to your labs.

You will hear the the following said frequently this semester: there is no right or wrong design; however, there are good and bad designs. In other words, many designs can serve to solve a particular problem. But each design may have its own strengths and weaknesses. This is also true of the design you have before you for this lab. After considering it, you may come up with improvements. Feel free to talk about these with your TAs or instructor. Improvements are always welcome!


Before starting:

[[[ Download PC zip ]]]

Zip includes:

All labs will include a main file named by the lab number, such as Lab2.java. The labs will also include a Startup.java file, which serves as a driver: a piece of code that calls the methods you write, to exercise your code and demonstrate your work.

Recall that the following lines should appear at the top of any files that use the terminal or canvas classes.

import terminal.*;
import canvas.*;


  1. Problem Statement

    A set of cartoon characters has been trying for some time to purchase clothing. However, when they shop, the merchant doesn't take them seriously for the following reasons.

    Fortunately, a new shop has opened up on the Internet: Toons 'R Us.com. This store allows the characters to order clothing discreetly and electronically.

    One concern these customers have is the changes their garments suffer when laundered. Even two-dimensional clothing suffers expansion when washed and shrinkage when dried. The customers would like to simulate the wash and dry cycles and see the resulting effects on their garments.

    Our problem is to provide a means for

    The above must be accomplished for
    Shirts
    which have
    • A trunk
    • Two sleeves
    • A cuff (trim) on each sleeve
    Your solution must display a garment. When the user clicks on the screen, the garment undergoes a wash or dry cycle, alternately.
  2. Design

    1. We will use a CS101Canvas to display the garment and interact with the user.
    2. Based on the problem statement, we can decompose the problem by observing the following "has-a" relationships.
      • A Shirt has a
        • trunk, which is a Rectangle
        • right Sleeve
        • left Sleeve
      • A Sleeve has
        • a Rectangle for its main part
        • some Trim at the outer part of the sleeve
      • A Trim is a rectangle.
      The italicized words are the class names for our design.
    3. Decomposition yielded the class structure. After examining what these methods need to maintain and draw the components of their garments, we develop the following abstraction of a rectangle, in the Rect class, for which we have the following API.
      Constructor(s)
      Rect(CS101Canvas canvas)
      Defines a rectangle, initially of zero area. The rectangle is defined by its two corners as follows.
      The corner designated internally as (c1x, c1y) is considered its origin. The other corner, (c2x, c2y) is diagonally opposite the origin corner.
        (c1x, c1y)
                 |----------------------------|
                 |                            |
                 |                            |
                 |                            |
                 |                            |
                 |----------------------------|
                                              (c2x, c2y)
      
      Accessors
      • int getC1X() returns the x coordinate of the rectangle's current origin.
      • int getC1Y() returns the y coordinate of the rectangle's current origin.
      • int getC2X() returns the x coordinate of the rectangle's other corner.
      • int getC2Y() returns the y coordinate of the rectangle's other corner.
      • int getWidth() returns the current width of the rectangle.
      • int getHeight() returns the current height of the rectangle.
      Mutators
      void relocateTo(int x, int y)
      Moves the origin of the rectangle, internally maintained as (c1x, c1y), to the specified parameters. The rectangle maintains its appearance with respect to width and height.
      void scale(double sx, double sy)
      Changes the size of the rectangle. The rectangle's origin is unchanged, but its width and height are scaled by sx and sy, respectively.
      void setWidth(int width)
      Leaves the origin unchanged, but changes the width to the specified paramter.
      void setHeight(int height)
      Leaves the origin unchanged, but changes the height to the specified paramter.
      void remove()
      Makes the rectangle (seem to) disappear.
      Other methods
      private void update()
      is called internally, the image of the rectangle on the canvas is to be updated, based on the internally maintained values of c1x, c1y, c2x, and c2y.
      int area()
      returns the area of the rectangle
      int circumference()
      returns the circumference of the rectangle
    4. For Shirt, we have the following API.
      Constructor(s)
      Shirt(CS101Canvas canvas, int trunkWidth, int trunkHeight, double sleeveFrac, double trimFrac, double washScale, double dryScale)
      This defines what we need for a shirt.
      • trunkWidth and trunkHeight are the width and height of the trunk of the shirt, prior to any laundering.
      • sleeveFrac is the scale of the sleeve with respect to the shirt. For example, a sleeveFrac of 0.5 would create a sleeve whose width and height are one-half the size of the trunk.
      • trimFrac is the scale of trim with respect to the size of the object it trims. However, the scale only applies to the appropriate dimension. For a shirt sleeve, the trim is the same height as the sleeve, but the trim's width is affected by trimFrac.
      • washScale and dryScale specify the factors by which the garment scales when washed and dried, respectively.
      Accessors
      None
      Mutators
      Only the following two:
      • void wash() affects the garment by one wash cycle.
      • void dry() affects the garment by one dry cycle.
    5. For Sleeve, we have the following API.
      Constructor(s)
      Sleeve(CS101Canvas canvas, double frac, double trimFrac)
      • frac is the fraction of the trunk for this sleeve. This is fixed at construction time, even though no sleeve is drawn yet.
      • trimFrac is the fraction of this sleeve that the trim should occupy, as described for Shirt, above.
      Accessors
      None
      Mutators
      The following method is called whenever this object's position and size should be updated.
      • void become(int x, int y, int trunkWidth, int trunkHeight) update's the sleeves origin to (x,y). The trunk's current width and height are provided, so that the sleeve can compute its current dimensions.
    6. For Trim, we have the following API.
      Constructor(s)
      Trim(CS101Canvas canvas, double frac)
      • frac is the fraction of the garment's component for this trim. This is fixed at construction time, even though no sleeve is drawn yet.
      Accessors
      None
      Mutators
      The following method is called whenever this object's position and size should be updated.
      • void become(int x, int y, int sleeveWidth, int sleeveHeight) update's the trim's origin to (x,y). The sleeve's current width and height are provided, so that the trim can compute its current dimensions.


  3. Implementation

    You must implement the classes described in the design. For this lab, you are constrained to following this design. Specific implementation steps are given at the end of this document; the files you download repeat those suggestions.


What to turn in:

  1. Complete a code cover sheet.
  2. Provide a transcript from your self-tests.
  3. Provide a printout of any files you have modified (see approach below).

Suggested implementation approach:

(The files contain some more specific instructions.)

  1. Implement Rect.java according to the instructions contained in that file.
  2. Test Rect.java using the testRect code provided in Startup.java.
  3. Complete the code for Shirt.java, but just to the point of getting the trunk of the shirt drawn.
  4. Test your emerging Shirt.java using testShirt code provided in Startup.java.
  5. Complete the code for Sleeve.java and continue testing.
  6. Complete the code for Trim.java and continue testing.


Last modified 15:33:13 CST 07 February 2000 by Ron K. Cytron