600.226: Data Structures (Fall 2016)

Assignment 2: Arrays of Doom

Overview

The second assignment is mostly about arrays, notably our own array specifications and implementations, not just the built-in Java arrays. Of course we also once again snuck a small ADT problem in there…

Note: The grading criteria now include 10% for programming style. Make sure you use Checkstyle with the correct configuration file from Piazza!

Problem 1: Revenge of Unique (40%)

You wrote a small Java program called Unique for Assignment 1. The program accepted any number of command line arguments (each of which was supposed to be an integer) and printed each unique integer it received back out once, eliminating duplicates in the process.

For this problem, you are going to start from the example solution we discussed in class and posted on Piazza for you. You will make two major changes to Unique: First, you’re going to modify the program to use our Array interface and our SimpleArray implementation from lecture (also available on Piazza for you) instead of basic Java arrays. Second, you’re going to modify the program to read the integers from standard input instead of processing the command line. So the resulting Unique program is invoked as

java Unique

with no further arguments. The program then waits for input from the user, who could for example type

1 9 2
3
1 4
9 5 3
6 0

and then hit the end-of-file key (that’s CTRL-D on Unix and CTRL-Z on Windows). At this point the program should output the unique numbers that were present in the input just like before:

1
9
2
3
4
5
6
0

Again, the files Unique.java, Array.java, and SimpleArray.java (as well as all necessary exception classes) are available from Piazza. Please use those official files, not your own versions of them. (Obviously Unique.java is not covered by the “do not modify provided files” rule, you must modify it in order to solve this problem.)

Hints

Problem 2: Flexible Arrays (20%)

Develop an algebraic specification for the abstract data type FlexibleArray which works like the existing Array ADT for the most part except that both its lower and its upper index bound are set when the array is created. Lower as well as upper bound can be any integer, provided the lower bound is less than or equal the upper bound. Write up the specification for FlexibleArray in the format we used in lecture and comment on the design decisions you had to make. Also, tell us what kind of array you prefer and why.

Note: Some programming languages, notably Pascal and Ada, support arrays of this kind instead of the zero-based arrays that are dominant in C and Java.

Hints

Problem 3: Sparse Arrays (40%)

A sparse array is an array in which relatively few positions have values that differ from the initial value set when the array was created. For sparse arrays, it is wasteful to store the value of all positions explicitly since most of them never change. Instead, we want to store values only for the positions that have actually been changed.

For this problem, you’re supposed to write a class SparseArray that implements the Array interface we developed in lecture (the same interface you used for Problem 1 above). Do not modify the Array interface in any way! Instead of using a plain Java array like we did for SimpleArray, your SparseArray should use a linked list of Node objects to store values, similar to the ListArray from lecture. However, your nodes no longer store just the data at a certain position, they also store the position itself! Here’s a rough outline of how your implementation could work:

Important: Your Node class must be nested inside your SparseArray class with private visibility! Clients should not be able to “touch” Node objects in any way!

(Don’t forget to add proper javadoc comments for your SparseArray class; in particular, include advice for clients trying to decide between the regular SimpleArray implementation and your new sparse implementation.)

Testing

As part of Assignment 1, we gave you a program called PolyCount that could be used to test the basic operation of a number of different Counter implementations. For this assignment, we’re giving you a similar program called PolyArray that can be used to test array implementations (some of the details are a bit more complex for technical reasons, but you don’t need to understand those for now). However, PolyArray is far from complete regarding test cases:

You need to modify PolyArray.java to add more test cases so that all axioms and preconditions will be tested. Of course, you should also use PolyArray to test your new SparseArray code: Once you’re done with PolyArray all three implementations should be fully tested when the program is run.

Hints

Even More Hints

Deliverables

Please turn in a gzip-compressed tarball of your assignment; the filename should be cs226-assignment-number-name.tar.gz with number replaced by the number of this assignment (see above) and name replaced by the first part of the email address you used to register on Piazza. (For example, Peter would use cs226-assignment-3-phf.tar.gz for his submission of Assignment 3.) The tarball should contain no derived files whatsoever (i.e. no .class files, no .html files, etc.), but should allow building all derived files. Include a README file that briefly explains what your programs do and contains any other notes you want us to check out before grading; your answers to written problems should be in this file as well. Finally, make sure to include your name and email address in every file you turn in (well, in every file for which it makes sense to do so anyway)!

Grading

For reference, here is a short explanation of the grading criteria; some of the criteria don't apply to all problems, and not all of the criteria are used on all assignments.

Packaging refers to the proper organization of the stuff you hand in, following both the guidelines for Deliverables above as well as the general submission instructions for assignments.

Style refers to Java programming style, including things like consistent indentation, appropriate identifier names, useful comments, suitable javadoc documentation, etc. Many aspects of this are enforced automatically by Checkstyle when run with the configuration file available on Piazza. Style also includes proper modularization of your code (into interfaces, classes, methods, using public, protected, and private appropriately, etc.). Simple, clean, readable code is what you should be aiming for.

Testing refers to proper unit tests for all of the data structure classes you developed for this assignment, using the JUnit 4 framework as introduced in lecture. Make sure you test all (implied) axioms that you can think of and all exception conditions that are relevant.

Performance refers to how fast/with how little memory your program can produce the required results compared to other submissions.

Functionality refers to your programs being able to do what they should according to the specification given above; if the specification is ambiguous and you had to make a certain choice, defend that choice in your README file.

If your programs cannot be built you will get no points whatsoever. If your programs cannot be built without warnings using javac -Xlint:all we will take off 10% (except if you document a very good reason; no, you cannot use the @SuppressWarnings annotation either). If your programs fail miserably even once, i.e. terminate with an exception of any kind, we will take off 10% (however we'll also take those 10% off if you're trying to be "excessively smart" by wrapping your whole program into a universal try-catch).