On this page:
Using the javalib library
1 Methods for complex data
6.8

Lab 3: Methods for Complex Data

Goals: The goals of this lab are to practice designing methods for complex data and to practice testing thoroughly.

For this lab, there are no starter files. Start a new project and build the file from scratch.

Using the javalib library

Related files:
  javalib.jar     tester.jar  

The javalib library provides the support for the design of interactive games and creating images composed by combining geometric shapes as well as image files.

To use the library, download the javalib.jar file above and add it to your project the same way you have added the tester library.

At the top of the .java file where the library is used, add the following import statements:

import tester.*; // The tester library import javalib.worldimages.*; // images, like RectangleImage or OverlayImages import javalib.funworld.*; // the abstract World class and the big-bang library import java.awt.Color; // general colors (as triples of red,green,blue values) // and predefined colors (Red, Green, Yellow, Blue, Black, White)

The first one is familiar; the next three are needed to import the definitions from javalib.

1 Methods for complex data

Alexander Calder was an American artist well known for his hanging sculptures called mobiles. Look up some of his work; it is quite pretty.

We would like to help other artists who may want to design mobiles, so they can check ahead of the time whether their mobile will fit in the desired space (height, width, weight), and whether it will be properly balanced.

The following drawing shows two mobiles, one simple and the other more complex:

image

image

Each horizontal bar is a strut (a very thin, very lightweight rod), and each vertical bar is a string. We will restrict the mobile to two items hanging from each strut, and record for any item hanging at the end of the line its weight and its color.

We have decided on the following data representation for mobiles (the length refers to the length of the vertical string, the leftside and rightside refer to the two parts of the strut):

             +---------+
             | IMobile |<---------------+
             +---------+                |
             +---------+                |
                 |                      |
                / \                     |
                ---                     |
                 |                      |
       ---------------------            |
       |                   |            |
+--------------+   +---------------+    |
| Simple       |   | Complex       |    |
+--------------+   +---------------+    |
| int length   |   | int length    |    |
| int weight   |   | int leftside  |    |
| Color color  |   | int rightside |    |
+--------------+   | IMobile left  |----+
                   | IMobile right |----+
                   +---------------+

The remaining two problems are harder: