On this page:
3.1 Instructions
Practice Problems
Problem 1: Understanding the String class
Problem 2: Drawing trees
What to submit
Using the javalib library
7.7

Assignment 3: Designing methods for Complex Data, Practice with Lists and Trees, Drawing

Goals: Practice working with lists. Learn to design methods and practice designing with accumulators. Learn to use the image library.

3.1 Instructions

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 handin enforces. 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.

There will be a separate submission for each problem - it makes it easier to grade each problem, and to provide you with the feedback for each problem you work on.

Practice Problems

Work out these problems on your own. Save them in an electronic portfolio, so you can show them to your instructor, review them before the exam, use them as a reference when working on the homework assignments.

Problem 1: Understanding the String class

Related files:
  Strings.java  

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.

Note: The following method defined for the class String may be useful:

// does this String come before the given String lexicographically? // produce value < 0 --- if this String comes before that String // produce value zero --- if this String is the same as that String // produce value > 0 --- if this String comes after that String int compareTo(String that)

Submit your work in the completed Strings.java file.

WARNING: Be extra careful not to name your file "String.java" – or else Java will think that you’re trying to redefine the built-in String class, and everything will break in creatively bizarre ways.

Problem 2: Drawing trees

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

import java.awt.Color;
 
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.)

Hints: To design many of the methods for this problem, it helps to refresh some basic trigonometry.

For each method, it will help to visualize or draw on paper what you are trying to achieve before you attempt to design and write code.

What to submit

Please submit your solution in a file named Tree.java, and name your examples class ExamplesTree.

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.