Computer Science 210
Data Structures

Fall 2018, Siena College

Problem Set 4
Due: 4:00 PM, Monday, October 29, 2018

You may work alone or in a group of size 2 or 3 on this assignment. However, in order to make sure you learn the material and are well-prepared for the exams, you should work through non-coding problems on your own before discussing them with your partner, should you choose to work with someone. Also, be sure every programming task is a team effort. In particular, the "you do these and I'll do these" approach is sure to leave you unprepared for the exams.

All GitHub repositories must be created with all group members having write access and all group member names specified in the file by 4:00 PM, Monday, October 22, 2018. This applies to those who choose to work alone as well!

There is a significant amount of work to be done here, and you are sure to have questions. It will be difficult if not impossible to complete the assignment if you wait until the last minute. A slow and steady approach will be much more effective.

Getting Set Up

You will receive an email with the link to follow to set up your GitHub repository for this problem set (ps4-yourgitname). One member of the group should follow the link to set up the repository on GitHub, then that person should email the instructor with the other group members' GitHub usernames so they can be granted access. This will allow all members of the group to clone the repository and commit and push changes to the origin on GitHub.

Please answer the questions later in this assignment in the file in your repository. Use Markdown to format neatly. Extensive formatting is not necessary.

More Recursion

Your first task is to write one more recursive method along the lines of those you wrote for Lab 5. This one will compute the sum of k consecutive values of an array, starting at a given index. We'll call this a "subarray sum".

The method stub for subArraySum and a comment describing in more detail what it should do can be found in SubArraySum.

Question 1: Describe specifically, in English, how you are forming the subproblem to solve, and what you need to do to take the solution to the subproblem and use it to get a solution to the whole problem. (4 points)

Question 2: What is the base case/stopping condition of your recursion? (2 points)

Practice Program: Complete the subArraySum recursive method. You may test by writing a main method or by calling the methods within BlueJ and providing appropriate parameters. No loops! (8 points)

Command-line Parameters

We can provide information to a Java application through the args parameter to its main method. As you can see from the main method signature we have been using all semester, it is an array of Strings. These String values come from what the program's user types after its name on the command line when running the program. Different Java IDEs provide different mechanisms for specifying these Strings, known as command-line parameters. In BlueJ, these are specified in a dialog box that we have been ignoring each time we run a Java application - the one that comes up after you choose main from the right-click menu to run your application. To specify the command-line parameters, we specify them as an array of String with the same syntax as a statically initialized array in Java. For example, we could specify an array of 3 String values:

  { "Java", "is", "fun" }

The Java run-time system will construct and initialize an array with these values, and we can access "Java" as args[0], "is" as args[1], and "fun" as args[2] in our program's main method.

Practice Program: Write a Java application in a program that takes any number of integers as command-line parameters (though they will come to you as String values) and prints their sum. (6 points)

Note: you can convert a String whose contents can be interpreted as an integer value to an int by passing it to the Integer.parseInt method.

Sorting with Comparables

The programming for this part of the problem set will be graded as a programming project. Pay attention to style and documentation!

In your starter repository, you will find an iterative implementation of selection sort. Like the recursive version in the SortingComparisons example, it operates on an array of objects which implement the Comparable interface (which you used in lab last week).

Its method header looks like this:

    public static <T extends Comparable> void sort(T[] array)

This complicated syntax is Java's way of introducing a type parameter T, much like the type parameters we have seen in generic classes, but differing in two ways: (i) the type parameter is specified only for the method, not the entire class, so it is meaningful only within the implementation of this method, and (ii) the type parameter has a restriction that it can only match types that can be treated as Comparable objects. It is this second difference that allows us to assume, within the sort method, that a compareTo method exists for objects of type T. This further allows us to write a single general-purpose sort method that works on any kind of objects that are Comparable.

Question 3: Suppose we have two objects, a and b, of some type T that we know are Comparable, and we call the method a.compareTo(b) and it returns a positive number. What does that tell us about the order in which a and b should appear relative to each other in the final sorted array? (2 points)

Programming Assignment: Expand the tests in the main method of SelectionSort to include another builtin Java class that implements Comparable (String or Double are some possibilities) and the version of the Ratio class that you made Comparable in lab. Note that you should replace the Ratio class provided in the repository with the one you developed in lab. (6 points)

Temporarily remove the implements Comparable<Ratio> from the class header of your Ratio class. Your Ratio class will still compile, but now SelectionSort should not.

Question 4: What compiler error message do you see? Explain briefly what this means and why Java does not accept this code even though your Ratio class still includes an implementation of the compareTo method needed in the implementation of the sort method. (3 points)

Sorting with Comparators

The programming for this part of the problem set will also be graded as a programming project. Pay attention to style and documentation!

Now suppose we want to sort these same kinds of objects, but by different criteria. Perhaps we want to sort Integers in descending order. Perhaps we want to sort Ratio objects by their numerators, ignoring denominators.

We could accomplish this by changing our Comparable objects' compareTo methods. However, this then means we have imposed a new kind of ordering on those objects. Furthermore, we cannot modify the compareTo methods of builtin classes like Integer.

For many types of data, a compareTo method can be used to define its "natural" ordering. But not all data types have an obvious natural ordering, and even for those that do, we might not always want to use that ordering to guide a sort process. There could be many perfectly reasonable criteria on which we might wish to sort a particular kind of data.

Question 5: What is the natural ordering used by the compareTo method for each of the Java classes String and Integer? Give three other meaningful orderings one could choose for each. (5 points)

Question 6: Give two possible meaningful orderings one might use for the DoubleArrayList class we have been working with. (2 points)

Java provides another interface that allows us to define any number of orderings for objects of a given type: the Comparator interface.

Unlike the Comparable, the Comparator interface is typically implemented by a separate class from the one it is designed to compare.

Study and try out the RatioNumeratorComparator in your starter repository. It is able to compare Ratio objects based only on their numerators.

Programming Assignment: Write another Comparator called RatioSumComparator that works with Ratio objects and orders Ratios ascending by the sum of the numerator and denominator. Include tests in a main method similar to those in the RatioNumeratorComparator. (5 points)

We can use Comparators to implement an even more general-purpose sorting method than the one given in SelectionSort. If we pass an additional parameter to a sort method that provides a Comparator that works with the type of objects in the array, we can use that Comparator's compare method any time we need to compare two items during the sort procedure.

The method header should look like this:

  public static <T> void sort(T[] array, Comparator<T> c)

Note that if T is a class that implements Comparable, then the NaturalComparator can be used.

Programming Assignment: Implement a Comparator-based sort method in SelectionSort. Test the method in main by using the two "custom" Comparators for the Ratio class, and the NaturalComparator to sort arrays of two Comparable types. (15 points, 10 for the method 5 for the tests)

Using your Comparator-based sort

Write two applications (three if you are working in a group of three). They should each work on a different data file and each should perform more than one "interesting" sort process. Both/all three will use the same Comparator-based sort method in the SelectionSort class, but each application will call its own class for encapsulating the data objects you are working with, and a number of Comparators to sort the data in different ways. You should have a total of at least five (seven, if working in a group of three) unique Comparators in your submission.

You may choose from the data files I have provided in the starter repository. (see the EXAMPLES-README.txt file there for more information) or you may use some other data that you find interesting. Please include all data files that you use in your submission.

Whatever types of data you choose to use, you will need to implement a custom class that holds the details of each type of data, Comparators that can compare instances of that class by different criteria, and an Java application that uses those Comparators to sort an array that holds instances of your custom class in multiple ways. The examples directory includes a data file movies.dat, whose entries are used to create instances of the provided MovieInfo class, three classes that implement Comparator and compare MovieInfo objects by different criteria, and an application MovieInfoSorter that uses an array with the Comparators to sort the data in multiple ways. Please do not start implementing your own code for custom classes and comparators until you fully understand the ones provided!


Your submission requires that your answers to the questions are in and that all required code and other file deliverables are committed and pushed to the master for your repository's origin on GitHub. That's it! If you see everything you intend to submit when you visit your repository's page on GitHub, you're set.


This assignment is worth 100 points, which are distributed as follows:

> FeatureValueScore
Question 1 4
Question 2 2
subArraySum method correctness 8 correctness 6
Question 3 2
SelectionSort Comparable tests in main 6
Question 4 3
Question 5 5
Question 6 2
RatioSumComparator correctness and tests 5
Comparable-based SelectionSort correctness 10
Comparable-based SelectionSort tests 5
Comparators correctness 15
Sorting applications 10
Design and style 6
Documentation 8
Git commit frequency and message quality 3
Total 100