Goals: Learn to use the String class, practice working with lists. Learn to design methods and practice designing with accumulators.
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
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.
The submission will be organized as follows:
Homework 3 Problem 1: The 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.
Friday, May 15th, 10:00pm EST
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
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)
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. (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.