Assignment 3: Designing methods for Complex Data, Practice with Lists and Accumulators
Goals: Practice working with lists. Learn to design methods and practice designing with accumulators.
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.
As always, you may only use techniques that have been covered in lectures so far in your solutions.
You will submit this assignment in two parts:
Part 1: Submit your data definitions, examples and tests for the methods. You should include stubs for the methods themselves (remember not to have your stubs return nulls). Although we would not expect the tests to pass yet, your code should compile. This will be partly graded on the completeness of your test cases. You should add examples for the cases you want to test. Please add a comment for each test case describing what you are testing.
After part 1 of Assignment 3, there is a self-eval assignment. This will give you a chance to review your own work. This will also help the TAs with grading because it will ask you to tag lines of code that we will be looking for (in this case, tests).
The self-eval will show up in handins after the deadline for part 1 has passed: at 10pm Monday night. Then you will have 24 hours to complete it.
Important: you will no longer be able to submit part 1 after clicking on the self-eval. Make sure you have submitted your final version of the assignment before doing the self-eval!
Part 2: Submit everything from Part 1 but this time include complete implementations for the methods (including any helpers needed).
Part 1: Monday, January 27th, 9:00 pm (with self-eval due Tues by 10pm)
Part 2: Thursday, January 30th, 9:00 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: Working with lists
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)
In a future assignment, we will be creating a game that deals with lists of words. A word can be active or inactive in the game so we come up with the following definitions for a list of words:
In this assignment, we will implement some methods for lists of words. Some of these may be relevant to the later game assignment, others are for practice.
All of the following methods should be defined in ILoWord. You may need to design helpers for some of the methods as well.
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, with words sorted in alphabetical order, treating all Strings as if they were given in all lowercase. You may use insertion sort as we did in lecture.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 of IWords has words 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 IWords and a given list of IWords, 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 merges this sorted list of IWords and a given sorted list of IWords, and produces a sorted list of IWords 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 checkAndReduce which takes in a String of length 1 and produces an ILoWord where any active words in this ILoWord are reduced by removing the first letter only if the given string matches the first letter.
Design the method addToEnd that takes in an IWord and produces an ILoWord that is like this ILoWord but with the given IWord added at the end.
Design the method filterOutEmpties that produces an ILoWord with any IWords that have an empty string are filtered out.
Design the method draw that takes in a WorldScene and draws all of the words in this ILoWord onto the given WorldScene. Review The Image Library to learn about making a TextImage to display text and the placeImageXY method to place an image on a WorldScene. Choose colors for the background and the words. Active words and inactive words should be different colors. Choose sizes for the background and for the font for words.