Assignment 2: Arrays of Doom
- Out on: September 12, 2016
- Due by: September 18, 2016 before 10:00 pm
- Collaboration: None
- Grading: Packaging 10%, Style 10% (where applicable), Performance 10% (where applicable), Functionality 70% (where applicable)
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
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
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.)
- Reading numbers from standard input can be accomplished using a
java.util.Scannerobject that has been wrapped around
System.inwhich is Java’s name for the standard input stream.
- Make sure you hit return one last time at the end of your input and only then signal end-of-file with the appropriate key-combination for your operating system (this restriction doesn’t apply when you use I/O redirection to give input to the program, a highly recommended practice for testing).
- You will have to process an unbounded number of inputs, which requires that you keep track of how “full” the array is. When nothing fits into the array anymore, you’ll have to “grow” it somehow. The best approach is to double the size of the array when you are out of space. (We’ll talk about the reasons for this in lecture next week.)
- Do not try to change everything at once, there are too many “moving parts” to get things right that way. Instead, choose one thing to change, for example just the way input is given to the program, finish that, test it, and only then move on to the next thing. Remember: Baby steps!
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.
FlexibleArrayfor which the lower bound equals the upper bound has exactly one slot.
FlexibleArrayis not the
ArrayADT we did in lecture; it doesn’t have to support the exact same set of operations.
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
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:
- Start with an empty list (instead of the complete list we built in the constructor of
put, check if the relevant position has been modified before (meaning a
Nodeobject exists for that position); if not, add a
Nodeto the list for the position and its new value; otherwise update the correct
Nodeto the new value.
get, check if the relevant position has been modified before; if not, return the default value; otherwise, return the value found in the relevant
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.)
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:
- the code in
testNewGetonly covers the first two axioms of the specification
- the code in
testNewLengthWrongonly covers the first precondition of the specification
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.
- Your iterator for
SparseArraydoesn’t have to be particularly efficient; indeed, it’s rather tricky to make it work fast; don’t try. (Or rather: Only try if you’re bored!)
- Think about this: Someone creates a
SparseArray<Integer>with an initial value of 1. Then they put a few numbers different from 1 into the array. For example, the value of slot 4 might be 18. What should happen for
- Testing a precondition means testing that the correct exception is thrown when a bad parameter is provided.
- Note that the
PolyArraywe gave you doesn’t test iterators at all; it probably should since all array implementations (the ones we gave you and the one you wrote yourself) support iterators…
Even More Hints
- Ensure that the version of your code you hand in does not produce any extraneous debugging output anymore!
- Pay attention to edge cases in the input your classes and programs are expected to handle! For example, make sure that you handle the empty input in a reasonable way for Problem 1.
Please turn in a gzip-compressed tarball of your assignment; the filename should be
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)!
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
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
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).