Assignment 3: Understanding String class; Designing methods for Complex Data
Goals: Learn to use the String class, practice working with lists. Learn to design methods for complex class hierarchies. Design images using javalib library.
1 Instructions
The names of the classes must be exactly the same as specified. The same is the case for the names and types of the fields within the class, as well as the order in which they are defined and listed as the constructor arguments. This allows us to design our own Examples class that tests your program.
Make sure you follow the style guidelines that WebCAT enforces. For now the most important ones are: using spaces instead of tabs, indenting by 4 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 Web-CAT submission system. You may submit as many times as you wish. Be aware of the fact that close to the deadline the WebCAT system may slow down to handle many submissions - so try to finish early.
With each homework you will also submit your log file named pair-user1-user2.txt where you replace user1 and user2 with the usernames of the two partners.
On top of both files you will have five lines of comments as follows:
// assignment 1 |
// partner1-last-name partner1-first-name |
// partner1-username |
// partner2-last-name partner2-first-name |
// partner2-username |
(In the text file you do not need the two slashes)
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 wou work on.
The two submissions will be organized as follows:
Submission HW3P1: The log file and the Strings.java file in one .zip file
Submission HW3P2: The IMobile.java file
Due Date: Wednesday, January 29th, 2014 at 11:59 pm.
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
For this problem start with the file Strings.java that defines a list of Strings.
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 and that String are the same |
// produce value > 0 --- if this String comes after that String |
int compareTo(String that) |
Design the method sort that produces a new list, sorted in alphabetical order, treating all Strings as if they were given in all lower case.
The String class defines the method toLowerCase that produces a String just like the one that invoked the mehtod, but with all upper-case letters converted to lower case.
Design the method isSorted that determines whether the list is sorted in alphabetical order.
Hint: You may need a helper method. You may want remember the accumulator style functions we have seen in Scheme.
Design the method merge that from two sorted lists of Strings and produces a sorted list of Strings that contains all items in both original lists (including duplicates).
Hint: The method is defined for the clases that represent a list of Strings and consumes another list of Strings.
Design the method filter for a list of Strings that consumes a String of length one and produces a list of all Strings in the original list that start with the given String.
The class String defines the method startsWith(String) that determines whether the String that invoked the method starts with the given String.
Problem 2: Mobiles
Finish the problem on mobiles from the Lab 3. For the method maxWidth assume that the mobile is hung in one line against a wall, with left and right mobiles aligned as defined.
In effect, it owuld be the width of the image defined in the next method.
Design the method WorldImage drawMobile(Posn p) that produces the image of this mobile if it were hanging from the nail at the given Posn - and was all shown as a two-dimensional drawing, similar to our examples given in the lab.
Additional information on designing WorldImages will be provided in a separate document.