Assignment 3: Designing methods for Complex Data, Practice with Lists, Trees and Drawing
This is a partner assignment. You should work with the partner that was assigned to you in Lab 3. Both partners must work on every part of the assignment together.
Goals: Practice working with lists. Learn to design methods and practice designing with accumulators. Learn to use the image library.
3.1 Instructions
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,
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.
Thursday, February 2nd, 9:00pm.
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.
Problems 18.1 - 18.4 on page 225
Problem 18.5 on page 229
Problem 18.6 on page 234
Problem 19.4 on page 263
Problem 1: Understanding the String class
Give sufficient examples of data, and sufficient tests, to test your methods thoroughly.
If you find yourself wanting to use a field-of-field or getters, 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)
When referring to strings, this is known as a “case-insensitive” sort, since when it examines two strings, it converts everything to lowercase (or uppercase —
would it make any difference?) before comparing them, so the comparison can’t use— or be sensitive to— differences in casing. Design the method sort that produces a new list, sorted in alphabetical order, treating all Strings as if they were given in all lowercase.Note: The String class defines the method toLowerCase that produces a String just like the one that invoked the method, but with all uppercase letters converted to lowercase.
Design the method isSorted that determines whether this list is sorted in alphabetical order, in a case-insensitive way.
Hint: You will likely need a helper method. You may want to review the accumulator style functions we have seen in DrRacket.
Design the method interleave that takes this list of Strings and a given list of Strings, and produces a list where the first, third, fifth... elements are from this list, and the second, fourth, sixth... elements are from the given list. Any “leftover” elements (when one list is longer than the other) should just be left at the end.
Design the method merge that takes this sorted list of Strings and a given sorted list of Strings, and produces a sorted list of Strings that contains all items in both original lists, including duplicates, treating all Strings as if they were given in all lowercase. You should not use sort for this. (This is not the same computation as for interleave, but the two methods are similar. Can you construct an example of two lists such that interleaving them and merging them produce different results? Can you construct another example where the two results are the same?)
Design the method reverse that produces a new list of Strings containing the same elements as this list of Strings, but in reverse order.
Hint: The cleanest solution to this problem uses a helper method, in a style seen already in this problem.
Design the method isDoubledList that determines if this list contains pairs of identical strings, that is, the first and second strings are the same, the third and fourth are the same, the fifth and sixth are the same, etc.
Hint: Think carefully about how to test this method.
This isn’t the same as the typical definition of palindromes, which asks whether a single string contains the same letters when read in either order.
Design the method isPalindromeList that determines whether this list contains the same words reading the list in either order.Hint: Several of the methods defined above will be helpful in your solution.
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: Mobiles
Reminder: your examples class from lab should be named ExamplesMobiles, and your three examples should be exampleSimple, exampleComplex and example3.
Finish the problem on mobiles from Lab 3: Problem Solving and Accumulators: methods totalWeight, totalHeight, isBalanced, buildMobile and curWidth.
For the method curWidth: assume that the mobile is hung flat against a wall, with left and right mobiles aligned as defined. (In essence, it computes the width of the image defined in the next method.)
For the method buildMobile: to keep things simple, you can assume the given strut width can be divided properly between the sides and still be an integer.
Design the method WorldImage drawMobile() that produces the image of this mobile if it were hanging from the nail at some Posn, completely flat and shown as a two-dimensional drawing, similar to the examples given in the lab. (You don’t have to reproduce the tick marks in the example images, just the struts and rods and weights.)
Hint: you will probably want to look into image "pinholes".
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 (Red, Green, Yellow, Blue, Black, White)
boolean testImages(Tester t) { return t.checkExpect(new RectangleImage(30, 20, OutlineMode.SOLID, Color.GRAY), new RectangleImage(30, 20, OutlineMode.SOLID, Color.GRAY)); }
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 testDrawMobile(Tester t) { WorldCanvas c = new WorldCanvas(500, 500); WorldScene s = new WorldScene(500, 500); return c.drawScene(s.placeImageXY(ex.drawMobile(), 250, 250)) && c.show(); }
See The Image Library for more information.
Submit your work in a file called Mobiles.java.