We have seen how ArrayLists are a convenient class for storing data, which remove many of the hassles that come with using arrays. In order to better appreciate that convenience, we will see exactly how much work goes into the implementation of an ArrayList, by making one ourselves! This will illustrate the benefit of Abstract Data Types (ADTs), which are classes that provide a useful service, while hiding the nitty-gritty details under the surface. You interact with an ADT through a set of well-defined methods, and more importantly you do not need to know how it is implemented in order to use the class. As an example, think of your DVD player. You don't know exactly how your DVD player works inside, but you still know how to use it because it has a simple interface - the buttons - and you know abstractly what effect each of them should have on the state of the DVD player. This is a common pattern in programming, and we will design our first ADT in this assignment.
This homework requires skills with arrays and dividing work among subroutines. You will need to write the following classes:
You must put a comment at the top of your code that specifies your:
real name, login name, email address, and PID. You must also include a
two-to-four sentence description of the program. Use a multi-line
comment (/* */). YOU MUST DO THIS FOR EVERY JAVA
FILE!
Comments Comments in the style detailed in the book ARE required. Your code must be properly commented to receive full credit.
Comments and good style are worth (2pt).
This class is the bulk of the assignment, and will contain several
methods. Since this is a two-part assignment, we explicitly divide
this class into two parts. Throughout this assignment, there are many
opportunities for reusing code by having one method of
SmartArray call another. For each method that could benefit
by calling another method, we have put this picture (
) next to it as a hint.
You can compute this power of 2 with a loop, by starting with 1 and going to the next power of 2, so long as you haven't exceeded the input value.
Input value New capacity 10 16 20 32 8 8
get(int index) - This method fetches the item that
lives at the given index in the SmartArray and returns
it. Before returning the item, you must check to make sure that the
given index is valid. If it is not valid, you should throw an
IndexOutOfBoundsException with the message ("Invalid
index: " + theInvalidIndex).
(1pt)
set(int index, Object obj) - This method will reset
the element at index to be equal to
obj. If index is valid, then the
element at that index should be reset to equal obj. If
index is not valid, then this method should throw an
IndexOutOfBoundsException with the message ("Invalid
index: " + theInvalidIndex). This method should NOT change the
size of the SmartArray. This method has
no return value.
(1pt)
add(Object obj) - This method adds the
obj to the end of the SmartArray. This will
involve increasing the size of the SmartArray by 1. Once you
have ensured that the SmartArray has sufficient capacity for
another new element, you can simply put the new element in the correct
location in the array, and increase the size. This method has no
return value.
(1pt)
add(int index, Object obj) - This method will insert a
new element into this SmartArray at the given index. If the
index is valid, then each element whose location is greater than or
equal to index will get shifted up by 1. Then the new
object will get stored at index. If the index is not
valid, then this method should throw an
IndexOutOfBoundsException with the message ("Invalid
index: " + theInvalidIndex). If there is no exception, this
method should increase the size of the SmartArray by 1. This
method has no return value.
addAll(SmartArray other) - This method will add all of
the elements from the other SmartArray to this
SmartArray. The elements from other should be added
to the end of this SmartArray, in the same order they
appeared in other. The end result of this method is that this
SmartArray should increase its size by other.size(),
and other should be left completely unchanged. This method
has no return value.
remove(int index) - This method will remove the
object at the given index from the SmartArray. If the index
is valid, then the value at that index should be removed and all the
values at higher indexes should be shifted down by 1. This will cause
the size of the SmartArray to decrease by 1. If the index is
not valid, then this method should throw an
IndexOutOfBoundsException with the message ("Invalid
index: " + theInvalidIndex). This method should return the
Object that was removed. Here is an example: (1pt)Returns "is"
SmartArray before calling remove:
0 1 2 3 "My" "name" "is" "Mike" SmartArray after calling remove(2):
0 1 2 "My" "name" "Mike"
remove(Object obj) - This method is similar to the
other remove method, but it instead searches the SmartArray
for the given object and if it is found, it is removed. This method
compares obj against each element of the SmartArray
using the .equals method. If it finds obj, then it
will remove it and shift all the objects at higher indexes down by
1. If obj is in the SmartArray in multiple
locations, then only the lowest-index one is removed. If anything
is removed, then this method will
return true. If nothing is removed, this method will return
false.
(1pt)
contains(Object obj) - This method will search for
the given object and return true if it lives anywhere inside
this SmartArray. The search will compare obj against
the elements in the SmartArray by using the .equals
method. If the object is not found in the SmartArray, then
this method should return false.
(1/2pt)
containsAll(SmartArray other) - This method will
check to see if ALL of the elements of the given SmartArray
are contained inside this SmartArray. If there are any
elements in the given SmartArray that are not contained
inside this SmartArray, then this method should return
false. Otherwise, this method should return true.
(1pt)
subList(int startIndex, int endIndex) - This
method will return a sublist of this SmartArray, which
consists of all the elements from startIndex up to but not
including endIndex. Before making the sublist, you must make
sure that the input indexes are valid. The indexes are considered
valid if all three of the following are true:
(2pt EXTRA CREDIT)
Original SmartArray:
0 1 2 3 "My" "name" "is" "Mike" Result of calling subList(1,3):
0 1 "name" "is"
Here are the sample solutions for SensorReader.java and DataProcessor.java and SensorData.java. (2pt)
(NOTE: you will not need to edit or turnin SensorData because
it did not use ArrayLists)
We have testing programs for both Part A and Part B of this assignment. You can run these testing programs by typing the following commands on ieng6:
testPSA3A SmartArray.classThese will test your code in several ways and tell you which of your methods are not working properly.
or
testPSA3B SmartArray.class
NOTE: The tester for part B will test the extra credit method (subList), so if you chose not to do the extra credit then your code will always fail that particular test.
Since there are 2 parts to this assignment, there will be 2 separate turnins with two separate bundle scripts.
On ieng6 somewhere under your home directory you need to make a folder called PSA3. All of your files for this assignment must go in this folder.