Computer Science 210
Data Structures

Fall 2017, Siena College

Lab 3: ArrayList Practice
Due: start of your lab, Wednesday, October 4, 2017

This week's lab is a chance to practice a bit more using custom classes and the ArrayList ADT. While it is due at the usual time, hopefully you can finish up sooner to prepare for the exam.

You will be paired with a partner to complete this lab. Only one submission per group is needed.

Getting Set Up

You will receive an email with the link to follow to set up your GitHub repository arraylists-lab-yourgitname for this Lab. 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. At least one group member should make a clone of the repository to begin work.

In your git bash window, after you clone and cd to the repository, you can type the command

touch package.bluej

which will create the little BlueJ icon for you to click on to launch BlueJ.

Please answer lab questions in the file in your group's GitHub repository.

A double-specific ArrayList class

We talked in class about how great it is to write generic versions of classes (like GenericPair) rather than versions that work only with a specific datatype (like DoublePair). We have a fully generic version of the ArrayList class provided by the Java API, but we are going to think about how ArrayLists are implemented by writing out own class DoubleArrayList that can store only double values.

Implementing the First Few Methods

In this section of the lab you will get to implement the first few methods in the class DoubleArrayList. The following is a list of methods that need to be completed:

(2 Points) public int size() 
(3 Points) public void set (int pos, double element )
(7 Points) public double remove( int pos )
(2 Points) public void clear()

Run the test method testDoubleArrayListA after implementing each method to catch errors early and see your points increase. See each method's documentation for details on what it does.

Writing Test Code

It is a standard programming practice to create test code before you actually complete any method implementations, like we did for you for the previous step in this lab. Now you get the chance to do it for yourself.

Below is the method signature for the DoubleArrayList contains method.

public boolean contains (double element)

It returns true if the list contains the element, and false if it does not.

Create a new public static void method in TestDoubleArrayList called myTestCases. Modeling your method after the other test methods, write code that creates variables score and maxscore and a DoubleArrayList object. It should add various elements to the DoubleArrayList object using the addSimple method, then call the contains method and test the response for correctness. Use variables score and maxscore to count the correct responses and the total possible correct responses. Write 5 tests, so that the maximum score possible is 5. Make sure at least two of your tests check for elements that are not supposed to be in the list, and at least two check for elements that are supposed to be in the list. Also, be sure one of the tests checks for the very last element in the list (which is a case that sometimes causes problems.)

Then, add a dummy implementation of the contains method to DoubleArrayList. Do not implement it yet! This method should consist of the method signature and a body that just returns false. Compile and run your test program. Demo for your instructor. Your score should be low because you haven't yet implemented the contains method. (10 points)

Now implement the contains method. When your implementation passes all your tests, you may continue to the next part. (5 points)

More DoubleArrayList Methods

Now complete the implementations of the remaining methods, as listed below, in DoubleArrayList. Run the test method testDoubleArrayListB after implementing each method to catch errors early and see your points increase.

(1 Point) public int getCapacity()
(7 Points) public void ensureCapacity( int minCapacity )
(4 Points) public void add (double element )
(6 Points) public DoubleArrayList getCopy()
(12 Points) public void add (int pos, double element)
(9 Points) public void addAll( DoubleArrayList addend )
(5 Bonus Points) public void addAll( int pos, DoubleArrayList addpos )

Question 1: What are the advantages and disadvantages of using a DoubleArrayList as you implemented it as compared to using a generic ArrayList to store double values? (5 points)

More Matrix2D

This problem builds on the Matrix2D program from last week. Before you begin, add your non-destructive matrix-matrix multiply method from the earlier lab to the given

Practice Program: In the class Matrix2DList, complete the main method so that it creates an arbitrarily-long list of matrices, then reports their sum and product. Specifics below. (22 points)

Your program should behave as follows:

  1. Prompt for and read the matrix size to be used for all of the matrices. (Done by starter code.)
  2. Prompt for and read the range of values to be used to fill the first matrix with random values. (Done by starter code.)
  3. Create the first matrix and add it to the matrix list (which should be an ArrayList). (Starter code creates the Matrix2D object but does not add it to the list.)
  4. Ask repeatedly if more matrices should be created, and as long as there should be more, prompt again for the next range of values, create a matrix with values in that range, and add it to the matrix list. (Starter code prompts and creates the Matrix2D objects but does not add them to the list.)
  5. Once no more matrices are requested, print the number of matrices that have been created, print each matrix, then compute and print the sum of the matrices and the product of the matrices. (The starter code does none of this.)

A sample run of the program follows:

What size should the matrices be? 4
Enter upper and lower bounds for values of the next matrix: 0 10
More matrices?  (yes or no) yes
Enter upper and lower bounds for values of the next matrix: -5 5
More matrices?  (yes or no) yes
Enter upper and lower bounds for values of the next matrix: 100 200
More matrices?  (yes or no) no
You created 3 matrices.
Matrix 0:
9.68220530639116 7.345769488480678 1.7412544053873813 2.3438316061712605
3.1944624268586654 3.9513050924461313 4.267474531699351 2.583640677971447
4.641348346843794 3.5690041627188753 3.3537004228656233 6.340187059095067
2.098098670498576 7.140456953926631 7.855035765375354 2.5806587268338586

Matrix 1:
4.806713489353562 0.4135288039878944 1.2768161447453794 -4.068342594706581
3.6280471321371586 -2.5156720232308336 4.048496011548281 4.266635696913379
-3.039326003927366 1.1870583652796984 -4.750758409032128 0.5086397570974199
2.3554889820951885 -0.6787271394749981 2.6905416351814733 2.3593801046651217

Matrix 2:
149.83228384235943 130.59594399972167 158.3705270054091 169.4955788825903
110.50901258217934 185.2866551484843 149.6145308945793 120.91463311180807
180.23028479845289 177.20036526697353 151.46037517431358 174.51331981681983
125.26086320907847 112.274872082545 158.7914846584583 172.05826577250718

Sum of your matrices:
164.32120263810415 138.35524229219024 161.38859755554185 167.771067894055
117.33152214117516 186.7222882176996 157.93050143782693 127.7649094866929
181.8323071413693 181.95642779497211 150.06331718814707 181.36214663301232
129.71445086167222 118.73660189699663 169.33706205901512 176.99830460400617

Product of your matrices:
16482.548286913046 13922.983840001521 15352.500605703277 17474.653424398755
5567.009861117915 4553.491999143645 5766.601007853109 6489.223728863248
10682.275123478916 9126.782807692354 10552.788438086425 11877.89036959422
5902.680161976161 4421.1764573063265 6724.206610818697 7652.617790407841


Your submission requires that all required 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 95 points, which are distributed as follows:

> FeatureValueScore
testDoubleArrayListA tests 14
myTestCases method 10
contains method 5
testDoubleArrayListB tests 39
Lab Question 1 5
Matrix2DList practice program 22
testDoubleArrayListB bonus tests 5 (Bonus)
Total 95