On this page:
1 Instructions
Problem 1:   Road Trip!
Problem 2:   Drawing trees
Using the javalib library
Problem 3:   Abstracting over Data Definitions
8.13

Assignment 3: Designing methods for complex data; Practice with accumulators🔗

Goals: Learn to use the String class, practice working with lists. Learn to design methods for complex class hierarchies.

1 Instructions🔗

This assignment is long. Start early.

Be very, very careful with naming! Again, the solution files expect your submissions to be named a certain way, so that they can define their own Examples class with which to test your code. Therefore, whenever the assignment specifies:
  • the names of classes,

  • the names and types of the fields within classes,

  • the names, types and order of the arguments to the constructor,

  • the names, types and order of arguments to methods, or

  • filenames,

...be sure that your submission uses exactly those names.

Make sure you follow the style guidelines that we gave you. For now the most important ones are: using spaces instead of tabs, indenting by 2 characters, following the naming conventions (data type names start with a capital letter, names of fields and methods start with a lower case letter), and having spaces before curly braces.

You will submit this assignment by the deadline using the handin submission system. You may submit as many times as you wish. Be aware of the fact that close to the deadline the system may have a long queue of submissions which means it takes longer for your code to be submitted - so try to finish early.

The submission will be organized as follows:

Due Dates:
  • Problem 1 – Thursday, September 26th, 10:00pm

  • Problem 2 – Thursday, September 26th, 10:00pm

  • Problem 3 – Thursday, September 26th, 10:00pm

Problem 1: Road Trip!🔗

For all questions in this problem, be sure to follow the design recipe carefully:
  • Give sufficient examples of data, and sufficient tests, to test your methods thoroughly.

  • If you find yourself wanting to use a field-of-field, stop. Fill out the template for each method, and figure out another design.

  • Think carefully about how to use dynamic dispatch, and where to define methods, to keep your code as simple and clean as possible.

In this problem, you’re going to help two drivers on a RoadTrip. Every road trip has two drivers, driver1 and driver2, as well as directions, which is a list of Direction. A Direction has a description, and a number of miles. For example, if a direction had the description "make a U-turn" and the number of miles was 20, it would mean that in 20 miles, the driver should make a U-turn. Also, for simplicity’s sake, miles will always be measured with whole numbers in this assignment.

The issue our drivers are facing is that the road trip might be very long, so they want to split up the road trip by changing who is in the driver’s seat every 10 (or 15, or 20, or whatever the given number is) miles. The number of miles is always guaranteed to be positive and a whole number, but those are the only assumptions that can be made about the number. Also, note that driver1 should always be the first driver on the road trip.

Design a method splitUpTrip that transforms a road trip into a list of road trip chunks, based on the given number of miles the drivers want to switch-off. A RoadTripChunk has a driver as well as directions, which is a list of Direction.

For example, imagine that drivers Hazel and Henry are following these directions (with the number of miles given after the direction’s description):
  • Make a left at Alberquerque, 13

  • Make a right at the fork, 2

  • Make a left at the next fork, 3

  • Take the overpass, 45

  • Destination on left, 12

If they wanted to switch who was driving every 15 miles, the split up instructions would look like this, with Hazel as the first driver:
  • Make a left at Alberquerque, 13

  • Make a right at the fork, 2

  • Switch with Henry, 0

Then, with Henry driving:
  • Make a left at the next fork, 3

  • Switch with Hazel, 12

Then, with Hazel driving:
  • Switch with Henry, 15

Then, with Henry driving:
  • Switch with Hazel, 15

Then, with Hazel driving:
  • Take the overpass, 3

  • Destination on left, 12

Note that the drivers have to be informed how long to drive until they need to switch, so those instructions need to become new directions in the list of road trip chunks where appropriate.

Hint: you will certainly need to use at least one accumulator parameter in at least one helper method. There are several valid designs for this problem; work through a wish-list process to figure out what helpers you might want.

Hint: When it comes to naming interfaces and classes for lists, be sure to follow our naming convention, or our tests won’t be able to run properly.

Hint: If you find yourself needing to reverse the order of lists (not all implementations will), it is best to reverse everything at the end once so you don’t need to keep track of whether or not directions and/or road trip chunks are in order as you go.

Hint: Make sure you understand the entire given example. Work through it on paper or a whiteboard until each step of the output is clear.

Hint: When a direction is too long for the current driver to finish, it effectively becomes two directions: the direction to switch drivers when their driving is up, and the original direction minus the distance already travelled. When writing your methods, think carefully about how these "new" directions play into the recursive structure of your code, and what list of directions you want to recur on.

Problem 2: Drawing trees🔗

In this assignment, you will be drawing and working with trees.

interface ITree { /* see methods below */ }
 
class Leaf implements ITree {
int size; // represents the radius of the leaf Color color; // the color to draw it }
 
class Stem implements ITree {
// How long this stick is int length;
// The angle (in degrees) of this stem, relative to the +x axis double theta;
// The rest of the tree ITree tree;
}
 
class Branch implements ITree {
// How long the left and right branches are int leftLength;
int rightLength;
// The angle (in degrees) of the two branches, relative to the +x axis, double leftTheta;
double rightTheta;
// The remaining parts of the tree ITree left;
ITree right;
}

A Stem at an angle of 90 degrees is growing straight up; a Branch with a left angle of 135 degrees and a right angle of 45 degrees points on both upward diagonals. (Leaves don’t need angles, since we can just approximate them with circles.)

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. See The Image Library for more information.

To use the library, download the javalib 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 (Color.RED, Color.GRAY, etc.)

You can test images using check-expect just as you can check other values:
boolean testImages(Tester t) {
return t.checkExpect(new RectangleImage(30, 20, OutlineMode.SOLID, Color.GRAY),
new RectangleImage(30, 20, OutlineMode.SOLID, Color.GRAY));
}

But note that you must construct the images in the same manner: for example, the following test will fail:
boolean testFailure(Tester t) {
return t.checkExpect(
new ScaleImageXY(new RectangleImage(60, 40, OutlineMode.SOLID, Color.GRAY), 0.5, 0.25),
new RectangleImage(30, 15, OutlineMode.SOLID, Color.GRAY));
}

Finally, you can display your images so that you can see whether you’re on the right track, as follows:

boolean testDrawTree(Tester t) {
WorldCanvas c = new WorldCanvas(500, 500);
WorldScene s = new WorldScene(500, 500);
return c.drawScene(s.placeImageXY(myTree.draw(), 250, 250))
&& c.show();
}

See The Image Library for more information.

Problem 3: Abstracting over Data Definitions🔗

Related files:
  Media.java  

In this problem, our data will represent different kinds of media. Each piece of media has a title and list of available languages for captions. Different kinds of media can also contain additional information as shown in the supplied Media.java file.

Note: none of these methods are properly implemented. As given in the file, they are all stubs that currently return a dummy value, so the code will compile but not yet work.

Warmup: Download the file and work out the following problems:

Once you have finished these methods and are confident that they work properly, save the work you have done to a separate file. Do not submit the code as written so far. The problems below are the main point of this exercise, and it will be helpful for you to preserve the code written so far as a reference against which to compare your revised code below. Again, submit only the work below.