On this page:
3.1 Submission
Problem 1: Working with Accumulators
3.1 Draw at the Wimbledon Tournament
Problem 2: Mobiles
3.1 Using the javalib library
3.1.1 Basic Images
3.1.2 Combining Images
3.1.3 Pinholes
6.10

Lab 3: Problem Solving and Accumulators

Goals: The goals of this lab are to write methods in union data, and specifically to practice writing accumulator-style methods.

For this lab, there are no starter files. For each problem, start a new project and build the files from scratch.

3.1 Submission

Submit your solutions for the Lab 3 Exercises on the handin server by 9/26 1:00am Eastern.

Problem 1: Working with Accumulators

3.1 Draw at the Wimbledon Tournament

One of the four grand-slam tournaments in tennis is held at Wimbledon, England. It hosts both singles and double games. In this problem, we will focus only on singles games (one player against another).

Specifically we will represent the draw of the gentleman’s singles at Wimbledon 2019. Here is a snapshot of part of this draw:

Specifically, we represent the draw using an interface named IDraw. Two classes implement this interface. The SeedPlayer class represents a single player at the bottom of the draw (where the tournament starts). A seed player has a name. The PlayResult class represents a single match. It consists of the name of the winner, and the two IDraw objects that played that match, named one and two.

For example, here is a representation of the above draw, starting from the quarter-finals:

IDraw djokovic = new SeedPlayer("Djokovic");
IDraw goffin = new SeedPlayer("Goffin");
IDraw pella = new SeedPlayer("Pella");
IDraw agut = new SeedPlayer("Agut");
...
 
IDraw semiFinal1 = new PlayResult("Djokovic",djokovic,goffin);
IDraw semiFinal2 = new PlayResult("Agut",agut,pella);
...
 
IDraw final1 = new PlayResult("Djokovic",semiFinal1,semiFinal2);
IDraw final2 = new PlayResult("Federer",semiFinal4,semiFinal3);
 
IDraw winner = new PlayResult("Djokovic",final1,final2);

Note that the PlayResult objects have been created so that one is always the draw from which the winner comes (e.g. final2 is constructed using semiFinal4 first because that is the draw that Federer came from.)

Define the above interface and classes, and complete the above example.

Design a method ILoString wonAgainst() that computes and returns a list of all player names that this player has won against, in the order in which they played this player. For example, winner.wonAgainst() should return a list "Goffin", "Agut", "Federer", semiFinal2.wonAgainst() should return "Pella", and goffin.wonAgainst() should return an empty list.

Design a method String nemesis(String player) that returns the name of the player who eliminated a player whose name is the argument, in this draw. If this player won the draw then it should return "Won". If there is no player with the given name, it should return "Not Found". For example winner.nemesis("Federer") should return "Djokovic", winner.nemesis("Agut") should return "Djokovic", winner.nemesis("Djokovic") should return "Won", and winner.nemesis("Murray") should return "Not Found".

Write sufficient tests for these and any helper methods you choose to write.

Do Now!

After you are done with each method, think about whether you used accumulators to do it or not. Try the other way.

Do Now!

The restriction that one is always the draw from which the current winner came is not really motivated. Did this restriction help you to solve these problems? Think about how to relax this restriction while still implementing the above methods correctly.

Problem 2: Mobiles

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. Do them now, if you have the time, or finish them for homework. Think about them, but move on to the next problem.

3.1 Using the javalib library

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 javalib.worldcanvas.*; // so we can view our images 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 rest are needed to import and use the definitions from javalib.

Much like the image library for ISL, our image library for Java also comes with extensive documentation.

3.1.1 Basic Images

Note: The purpose of this section of the lab is to get you exposed to our image library. Be sure to actually run the examples given so you get a feel for it before you have to use it on homework!

Here is a snippet of code that will let us draw and look at a circle:

class ExamplesShapes {
 
WorldImage circle = new CircleImage(100, OutlineMode.SOLID, Color.BLUE);
WorldCanvas c = new WorldCanvas(500, 500);
WorldScene s = new WorldScene(500, 500);
 
boolean testDrawMyShapes(Tester t) {
return c.drawScene(s.placeImageXY(circle, 250, 250))
&& c.show();
}
}

And here is a snippet of code that will let us draw and look at two circles:

class ExamplesShapes {
 
WorldImage circle = new CircleImage(100, OutlineMode.SOLID, Color.BLUE);
WorldCanvas c = new WorldCanvas(500, 500);
WorldScene s = new WorldScene(500, 500);
 
boolean testDrawMyShapes(Tester t) {
return c.drawScene(s.placeImageXY(circle, 250, 250))
&& c.drawScene(s.placeImageXY(circle, 100, 300))
&& c.show();
}
}

However, it would be nice to be able to look at separate images separately, and to be able to identify which image is which. It would also be nice to not have to construct new canvases and scenes over and over again by hand. Here is a snippet of code that you can use to look at multiple images and tell them apart with much less work needing to be done for each image.

class ExamplesShapes {
 
// shows image at the center of an equally-sized canvas, // and the text at the top of the frame is given by description boolean showImage(WorldImage image, String description) {
int width = (int) Math.ceil(image.getWidth());
int height = (int) Math.ceil(image.getHeight());
WorldCanvas canvas = new WorldCanvas(width, height, description);
WorldScene scene = new WorldScene(width, height);
return canvas.drawScene(scene.placeImageXY(image, width / 2, height / 2))
&& canvas.show();
}
 
WorldImage circleBlue = new CircleImage(100, OutlineMode.SOLID, Color.BLUE);
WorldImage circleRed = new CircleImage(100, OutlineMode.SOLID, Color.RED);
 
boolean testDrawMyShapes(Tester t) {
return showImage(circleBlue, "Blue circle")
&& showImage(circleRed, "Red circle");
}
}

Being able to look at your images will be helpful for debugging in future assignments as well as this lab. Note that this is not a replacement for proper tests, and that methods that output images can be tested in the tester library just like any other method.

Use the documentation to help you with these exercises:
  • Draw at least two other basic shapes, such as a square and a triangle

  • Give your lab partner a friendly message by drawing some text

  • Draw the empty image

3.1.2 Combining Images
3.1.3 Pinholes