Lab 9: Generics and Exception Handling

due Monday, June 15 at 11:59pm on Canvas

Pair Programming Required (or No Credit)
  • Both partners fill in, sign, date, and submit the pair programming contract
  • Upload the document along with your Lab code to Canvas.
  • Only ONE partner submits the Lab code on Canvas.
  • BOTH partners submit the contract.
  • Please make sure both your names are on all the files.
  • If you need help finding a partner, please contact me as soon as possible.


Writing Our Own ArrayList

  • This lab will give you a peek under the hood of the ArrayList class, by asking you to write your own generic ArrayList class
  • Although our class is generic, recall arrays of generic types are not allowed in Java. 
    • For more information, see Lesson notes on Generics.
  • Therefore, we will need to store an array of Objects and cast to the generic type E when needed.
  • For example, here should be your default constructor:
      public ArrayList() {
        array = (
E[]) new Object[10]; //declare a new array of Objects, but cast to type E[]
        numElements = 0;
       
    }

  • Note: Generic code can generate warnings when casting to a generic type (as in the above method).
    • There is no way for the compiler to verify that the types are compatible, when the type is unknown
    • We will suppress the warnings, using @SuppressWarnings("unchecked"), as we are confident that no runtime errors will occur
    • I have added the tags for you in the starter code below in any case that they are required
  • Copy and paste the following starter code into a new file called ArrayList.java:
/**
 * ArrayList.java
 * @author
 * @author
 * CIS 36B, Lab 9
 */

public class ArrayList<E> {
    private E[] array;
    private int numElements;
   
   
    /**
     * Default constructor for ArrayList
     * Creates an empty array of length 10
     * Sets numElements to 0
     */
    @SuppressWarnings("unchecked")
    public ArrayList() {
        array = (E[]) new Object[10];
        numElements = 0;
    }
   
    /**
     * Constructor for ArrayList
     * Creates an empty array of length size
     * Sets numElements to 0
     * @param size the initial size of the
     * ArrayList
     */
    @SuppressWarnings("unchecked")
    public ArrayList(int size) {
       
    }
   
    /**
     * Copy constructor for ArrayList
     * Creates a new list array of the
     * same size as the ArrayList passed
     * in as a parameter, and copies the
     * parameter's data into the new
     * ArrayList using a for loop
     * Also sets the numElements to be
     * the same as the parameter's
     * numElements
     * @param la the ArrayList to copy
     */
    @SuppressWarnings("unchecked")
    public ArrayList(ArrayList<E> la) {
       
    }
   
    /**
     * Returns whether the ArrayList is
     * currently empty
     * @return whether the ArrayList is
     * empty
     */
    public boolean isEmpty() {
        return false;
    }
   
    /**
     * Returns the current number of
     * elements stored in the ArrayList
     * @return the number of elements
     */
    public int size() {
        return -1;
    }
   
    /**
     * Returns the element at the specified
     * index. Throws an exception if index is
     * larger than or equal to numElements
     * @param index the index of the element
     * to access
     * @return the element at index
     */
    public E get(int index) throws IndexOutOfBoundsException{
        return null;
    }
   
    /**
     * Uses the linearSearch algorithm to
     * locate an element in the ArrayList
     * @param element the element to locate
     * @return whether or not the element
     * is in the ArrayList
     */
    public boolean contains(E element) {
        return false;
    }
   
    /**
     * Uses the linearSearch algorithm to
     * locate an element in the ArrayList
     * @param element the element to locate
     * @return the location of the element
     * or -1 if the element is not in the
     * ArrayList
     */
    public int indexOf(E element) {
        return -1;
    }
   
    /**
     * Determines whether the ArrayList
     * is currently full
     * @return whether the ArrayList
     * is at maximum capacity
     * Should be called by the add methods
     */
    private boolean atCapacity() {
        return false;
    }
   
   
    /**
     * Resizes the ArrayList by making a new
     * array that has a length (capacity)
     * 10 larger than the current array's
     * length (capacity)
     */
    @SuppressWarnings("unchecked")
    private void reSize() {
       
    }
   
    /**
     * Inserts a new element to the end
     * of the list array.
     * Resizes the ArrayList if it is
     * at capacity before inserting
     * @param element the element to insert
     */
    public void add(E element) {
       
    }
   
    /**
     * Inserts a new element at the specified
     * index in the ArrayList.
     * Resizes the ArrayList if it is
     * at capacity before inserting
     * @param index the index at which to insert
     * @param element the element to insert
     */
    public void add(int index, E element) throws IndexOutOfBoundsException {
       
    }
   
    /**
     * Assigns a new value to the ArrayList
     * at a specified index
     * @param index the index at which to update
     * @param element the new element
     */
    public void set(int index, E element) throws IndexOutOfBoundsException {
       
    }
   
    /**
     * Removes an element at a specified index in
     * the ArrayList
     * @param index the index at which to remove
     * @return the element that was removed
     */
    @SuppressWarnings("unchecked")
    public E remove(int index) throws IndexOutOfBoundsException{
        return null;
       
    }
   
    /**
     * Removes the first instance of the specified
     * element in the list array
     * @param str the element to remove
     * @return whether the element was successfully
     * removed
     */
    public boolean remove(E element) {
        return false; 
    }
   
    /**
     * Returns whether two ArrayLists are
     * the same length and store
     * the same data in the same order
     */
    @SuppressWarnings("unchecked")
    @Override public boolean equals(Object o) {
        return false;
    }
   
    /**
     * Creates a String of all elements,
     * with [] around the elements,
     * each element separated from the next
     * with a comma
     */
    @Override public String toString() {
        String result = "[";
        return result + "]";
    }
   
}


  • Please do not import ArrayList in either your ArrayList.java or ArrayListTest.java or you will receive a 0 for this lab.
  • Next, write each method according to the description in the comment.
  • As you write each method, test it using the test file.
  • Make sure that one method is working before you move on to write the next <--good coding practice!


ArrayListTest.java File
  • Note that when I run your code with the below test file, the output should be identical to the sample output shown below - including the error messages - to receive full credit

/**
 * ArrayListTest.java
 * @author
 * @author
 * CIS 36B, Lab 9
 */

public class ArrayListTest {
    public static void main(String[] args) {
        System.out.println("***Testing Default Constructor and One Parameter Constructor***\n");
        ArrayList<Car> test1 = new ArrayList<>();
      
        System.out.println("After default constructor called:");
        System.out.println("Should print size of 0: "
                + test1.size());
        System.out.println("Should print is empty - true: "
                + test1.isEmpty());
        System.out.println("Should print []: " + test1);
      
        ArrayList<Car> test2 = new ArrayList<>(15);
        System.out.println("\nAfter one parameter constructor called:");
        System.out.println("\nShould print size of 0: "
                + test2.size());
        System.out.println("Should print is empty - true: "
                + test2.isEmpty());
        System.out.println("Should print []: \n" + test2);
      
        System.out.println("\n***Testing Add Methods***\n");
        ArrayList<Car> big = new ArrayList<>();
        for(int i = 0; i < 25; i++) {
            big.add(new Car());
        }
       
        System.out.println("Should print 25 empty Cars:\n");
        System.out.println(big);
       
        test1.add(new Car("Tesla", "Model 3", 14000, true));
        System.out.println("Should print size of 1: "
                + test1.size());
        System.out.println("Should print is empty - false: "
                + test1.isEmpty());
        System.out.println("Should print Tesla Model 3 at index 0: \n"
                + test1.get(0));
        System.out.println("Should print [Tesla]: " + test1);
      
        test1.add(new Car("Nissan", "Leaf", 500, false));
        System.out.println("\nAdding Nissan Leaf: ");
        System.out.println("Should print size of 2: "
                + test1.size());
        System.out.println("Should print is empty - false: "
                + test1.isEmpty());
        System.out.println("Should print Tesla Model 3 at index 0: \n"
                + test1.get(0));
        System.out.println("Should print Nissan Leaf at index 1: \n"
                + test1.get(1));
        System.out.println("Should print [Tesla, Leaf]: "
                + test1);
        try {
            System.out.println("\nShould print error message. Cannot add at index 10:");
            test1.add(10, new Car("Chevrolet", "Bolt", 60000, true));
        } catch(IndexOutOfBoundsException e) {
            System.out.println(e.getMessage());
        }
  
        test1.add(0, new Car("Chevrolet", "Bolt", 60000, true));
        System.out.println("\nAdding Chevy Bolt at index 0: \n");
        System.out.println("Should print size of 3: "
                + test1.size());
        System.out.println("Should print is empty - false: "
                + test1.isEmpty());
        System.out.println("Should print Chevy Bolt at index 0: \n"
                + test1.get(0));
        System.out.println("Should print Tesla at index 1: \n"
                + test1.get(1));
        System.out.println("Should print [Chevy, Tesla, Nissan]: " + test1);
      
        test1.add(2, new Car("Ford", "Fusion", 100, false));
        System.out.println("\nAdding Ford at index 2: ");
        System.out.println("Should print size of 4: "
                + test1.size());
        System.out.println("Should print is empty - false: "
                + test1.isEmpty());
        System.out.println("Should print Chevy at index 0: \n"
                + test1.get(0));
        System.out.println("Should print Ford at index 2: \n"
                + test1.get(2));
        System.out.println("Should print [Chevy, Tesla, Ford, Nissan]: \n" + test1);
      
        System.out.println("\nAdding 1-20 in ArrayList of size 15. Should resize to 20:");
        for(int i = 1; i <= 20; i++) {
            test2.add(new Car());
        }
        System.out.println("Should print size of 20: "
                + test2.size());
        System.out.println("Should print is empty - false: "
                + test2.isEmpty());
        System.out.println("Should print empty Car at index 0: \n"
                + test2.get(0));
        System.out.println("Should print empty Car at index 19: \n"
                + test2.get(19));
      
        System.out.println("\n***Testing Copy Constructor***\n");
        ArrayList<Car> test3 = new ArrayList<>(test1);
        System.out.println("Should print size of 4: "
                + test1.size());
        System.out.println("Should print is empty - false: "
                + test3.isEmpty());
        System.out.println("Should print Chevy at index 0: \n"
                + test3.get(0));
        System.out.println("Should print Ford at index 2: \n"
                + test3.get(2));
        System.out.println("Should print [Chevy, Tesla, Ford, Nissan]: \n" + test3);
      
        System.out.println("\nTesting for deep copy: ");
        test3.add(new Car("Hyundai",  "Kona", 15000, true));
        System.out.println("Original ArrayList size should be 4: "
                + test1.size());
        System.out.println("Copy ArrayList size should be 5: "
                + test3.size());
        System.out.println("Printing original [Chevy, Tesla, Ford, Nissan]: " + test1);
        System.out.println("Printing new [Chevy, Tesla, Ford, Nissan, Hyundai]: " + test3);
      
        System.out.println("\n***Testing Set Method***\n");
        try {
            System.out.println("Should print error message when setting index 6: ");
            test1.set(6, new Car());
        } catch (IndexOutOfBoundsException e) {
            System.out.println(e.getMessage());
        }
      
        test1.set(2, new Car("Tesla", "Model S", 150, false));
        System.out.println("\nShould print size of 4: "
                + test1.size());
        System.out.println("Should print the list array is empty - false: "
                + test1.isEmpty());
        System.out.println("Should print Chevy at index 0: "
                + test1.get(0));
        System.out.println("Should print Tesla Model S at index 2: "
                + test1.get(2));
        System.out.println("Should print [Chevy, Tesla 3, Tesla S, Nissan]: " + test1);
      
      
        System.out.println("\n***Testing Remove Methods***\n");
      
        System.out.println("Remove index 3, Nissan: "
                + test1.remove(3));
        System.out.println("Should print size of 3: "
                + test1.size());
        System.out.println("Should print is empty - false: "
                + test1.isEmpty());
        System.out.println("Should print Chevy at index 0: "
                + test1.get(0));
        try {
            System.out.println("Should print error message - cannot get index 3: "
                    + test1.get(3));
        } catch(IndexOutOfBoundsException e) {
            System.out.println(e.getMessage());
        }
        System.out.println("Should print [Chevy, Tesla 3, Tesla S]: " + test1);
      
       System.out.println("\nRemove index 0, Chevy: "
                + test1.remove(0));
        System.out.println("Should print size of 2: "
                + test1.size());
        System.out.println("Should print is empty - false: "
                + test1.isEmpty());
        System.out.println("Should print Tesla 3 at index 0: "
                + test1.get(0));
        System.out.println("Should print Tesla S at index 1: "
                + test1.get(1));
        System.out.println("Should print [Tesla 3, Tesla S]: " + test1);
        try {
            System.out.println("\nRemove index 10, should print error message: ");
            test1.remove(10);
        } catch (IndexOutOfBoundsException e) {
            System.out.println(e.getMessage());
        }
        System.out.println("Should print size of 2: "
                + test1.size());
        System.out.println("Removing Tesla S - true: " + test1.remove(new Car("Tesla", "Model S", 150, false)));
       
        System.out.println("Should print is empty - false: "
                + test1.isEmpty());
        System.out.println("Should print Tesla 3 at index 0: "
                + test1.get(0));
       
        System.out.println("Should print size of 1: " + test1.size());
       
        System.out.println("Should remove Ford - false: " + test1.remove(new Car("Ford", "Fusion", 1234, false)));
      
        System.out.println("Should print is empty - false: "
                + test1.isEmpty());
        System.out.println("Should print Tesla 3 at index 0: "
                + test1.get(0));
       
        System.out.println("Should print size of 1: " + test1.size());
      
        System.out.println("\n***Testing Size Method***\n");
        System.out.println("Should print size of 20: "
                + test2.size());
        System.out.println("Should print size of 0: "
                + new ArrayList<Car>().size());
        System.out.println("Should print size of 5: "
                + test3.size());
      
        System.out.println("\n***Testing IsEmpty Method***\n");
        System.out.println("Should print true: "
                + new ArrayList<Car>().isEmpty());
        System.out.println("Should print false: "
                + test2.isEmpty());
      
        System.out.println("\n***Testing Contains Method***\n");
        System.out.println("Contains Tesla 3, should print true: "
                + test3.contains(new Car("Tesla", "Model 3", 14000, true)));
        System.out.println("Contains Honda, should print false: "
                + test3.contains(new Car("Honda", "Sonata", 12345, false)));
       
      
        System.out.println("\n***Testing IndexOf Method***\n");
        System.out.println("Should print index of 0: "
                + test2.indexOf(new Car()));
        System.out.println("Index of A, should print index of -1: "
                + test1.indexOf(new Car()));
      
        System.out.println("\n***Testing Get Method***\n");
        System.out.println("Element at 0, should print Tesla Model 3: "
                + test1.get(0));
        System.out.println("Element at 3, should print Tesla Model 3: "
                + test3.get(1));
        System.out.println("Element at 6, should print error message: ");
        try {
            test1.get(6);
        } catch(IndexOutOfBoundsException e) {
            System.out.println(e.getMessage());
        }
      
        System.out.println("\n***Testing Equals Method***\n");
        ArrayList<Integer> test4 = new ArrayList<Integer>();
        test4.add(4);
        test4.add(2);
        test4.add(3);
        ArrayList<Integer> test5 = new ArrayList<Integer>();
        test5.add(4);
        test5.add(2);
        test5.add(3);
        System.out.println("Should print ArrayLists are equal (true): " + test4.equals(test5));
        test5.set(2, 4);
        System.out.println("Should print ArrayLists are not equal (false): " + test4.equals(test5));
        test5.remove(2);
        System.out.println("Should print ArrayLists are not equal (false): " + test4.equals(test5));
        System.out.println("Should print false (passing in a null): " + test4.equals(null));
        System.out.println("Should print false (passing in a Car): " + test4.equals(new Car()));
      
    }
}


Required Output:

***Testing Default Constructor and One Parameter Constructor***

After default constructor called:
Should print size of 0: 0
Should print is empty - true: true
Should print []: []

After one parameter constructor called:

Should print size of 0: 0
Should print is empty - true: true
Should print []:
[]

***Testing Add Methods***

Should print 25 empty Cars:

[
Make: No make
Model: No model
Mileage: -1.0
Is Used: false
,
Make: No make
Model: No model
Mileage: -1.0
Is Used: false
,
Make: No make
Model: No model
Mileage: -1.0
Is Used: false
,
Make: No make
Model: No model
Mileage: -1.0
Is Used: false
,
Make: No make
Model: No model
Mileage: -1.0
Is Used: false
,
Make: No make
Model: No model
Mileage: -1.0
Is Used: false
,
Make: No make
Model: No model
Mileage: -1.0
Is Used: false
,
Make: No make
Model: No model
Mileage: -1.0
Is Used: false
,
Make: No make
Model: No model
Mileage: -1.0
Is Used: false
,
Make: No make
Model: No model
Mileage: -1.0
Is Used: false
,
Make: No make
Model: No model
Mileage: -1.0
Is Used: false
,
Make: No make
Model: No model
Mileage: -1.0
Is Used: false
,
Make: No make
Model: No model
Mileage: -1.0
Is Used: false
,
Make: No make
Model: No model
Mileage: -1.0
Is Used: false
,
Make: No make
Model: No model
Mileage: -1.0
Is Used: false
,
Make: No make
Model: No model
Mileage: -1.0
Is Used: false
,
Make: No make
Model: No model
Mileage: -1.0
Is Used: false
,
Make: No make
Model: No model
Mileage: -1.0
Is Used: false
,
Make: No make
Model: No model
Mileage: -1.0
Is Used: false
,
Make: No make
Model: No model
Mileage: -1.0
Is Used: false
,
Make: No make
Model: No model
Mileage: -1.0
Is Used: false
,
Make: No make
Model: No model
Mileage: -1.0
Is Used: false
,
Make: No make
Model: No model
Mileage: -1.0
Is Used: false
,
Make: No make
Model: No model
Mileage: -1.0
Is Used: false
,
Make: No make
Model: No model
Mileage: -1.0
Is Used: false
]
Should print size of 1: 1
Should print is empty - false: false
Should print Tesla Model 3 at index 0:

Make: Tesla
Model: Model 3
Mileage: 14000.0
Is Used: true

Should print [Tesla]: [
Make: Tesla
Model: Model 3
Mileage: 14000.0
Is Used: true
]

Adding Nissan Leaf:
Should print size of 2: 2
Should print is empty - false: false
Should print Tesla Model 3 at index 0:

Make: Tesla
Model: Model 3
Mileage: 14000.0
Is Used: true

Should print Nissan Leaf at index 1:

Make: Nissan
Model: Leaf
Mileage: 500.0
Is Used: false

Should print [Tesla, Leaf]: [
Make: Tesla
Model: Model 3
Mileage: 14000.0
Is Used: true
,
Make: Nissan
Model: Leaf
Mileage: 500.0
Is Used: false
]

Should print error message. Cannot add at index 10:
Error: Cannot add  at index 10.
Index is outside the bounds of the ArrayList.
Index: 10, Size: 2

Adding Chevy Bolt at index 0:

Should print size of 3: 3
Should print is empty - false: false
Should print Chevy Bolt at index 0:

Make: Chevrolet
Model: Bolt
Mileage: 60000.0
Is Used: true

Should print Tesla at index 1:

Make: Tesla
Model: Model 3
Mileage: 14000.0
Is Used: true

Should print [Chevy, Tesla, Nissan]: [
Make: Chevrolet
Model: Bolt
Mileage: 60000.0
Is Used: true
,
Make: Tesla
Model: Model 3
Mileage: 14000.0
Is Used: true
,
Make: Nissan
Model: Leaf
Mileage: 500.0
Is Used: false
]

Adding Ford at index 2:
Should print size of 4: 4
Should print is empty - false: false
Should print Chevy at index 0:

Make: Chevrolet
Model: Bolt
Mileage: 60000.0
Is Used: true

Should print Ford at index 2:

Make: Ford
Model: Fusion
Mileage: 100.0
Is Used: false

Should print [Chevy, Tesla, Ford, Nissan]:
[
Make: Chevrolet
Model: Bolt
Mileage: 60000.0
Is Used: true
,
Make: Tesla
Model: Model 3
Mileage: 14000.0
Is Used: true
,
Make: Ford
Model: Fusion
Mileage: 100.0
Is Used: false
,
Make: Nissan
Model: Leaf
Mileage: 500.0
Is Used: false
]

Adding 1-20 in ArrayList of size 15. Should resize to 20:
Should print size of 20: 20
Should print is empty - false: false
Should print empty Car at index 0:

Make: No make
Model: No model
Mileage: -1.0
Is Used: false

Should print empty Car at index 19:

Make: No make
Model: No model
Mileage: -1.0
Is Used: false


***Testing Copy Constructor***

Should print size of 4: 4
Should print is empty - false: false
Should print Chevy at index 0:

Make: Chevrolet
Model: Bolt
Mileage: 60000.0
Is Used: true

Should print Ford at index 2:

Make: Ford
Model: Fusion
Mileage: 100.0
Is Used: false

Should print [Chevy, Tesla, Ford, Nissan]:
[
Make: Chevrolet
Model: Bolt
Mileage: 60000.0
Is Used: true
,
Make: Tesla
Model: Model 3
Mileage: 14000.0
Is Used: true
,
Make: Ford
Model: Fusion
Mileage: 100.0
Is Used: false
,
Make: Nissan
Model: Leaf
Mileage: 500.0
Is Used: false
]

Testing for deep copy:
Original ArrayList size should be 4: 4
Copy ArrayList size should be 5: 5
Printing original [Chevy, Tesla, Ford, Nissan]: [
Make: Chevrolet
Model: Bolt
Mileage: 60000.0
Is Used: true
,
Make: Tesla
Model: Model 3
Mileage: 14000.0
Is Used: true
,
Make: Ford
Model: Fusion
Mileage: 100.0
Is Used: false
,
Make: Nissan
Model: Leaf
Mileage: 500.0
Is Used: false
]
Printing new [Chevy, Tesla, Ford, Nissan, Hyundai]: [
Make: Chevrolet
Model: Bolt
Mileage: 60000.0
Is Used: true
,
Make: Tesla
Model: Model 3
Mileage: 14000.0
Is Used: true
,
Make: Ford
Model: Fusion
Mileage: 100.0
Is Used: false
,
Make: Nissan
Model: Leaf
Mileage: 500.0
Is Used: false
,
Make: Hyundai
Model: Kona
Mileage: 15000.0
Is Used: true
]

***Testing Set Method***

Should print error message when setting index 6:
Error: Cannot set at index outside bounds of list array.
Index: 6 , Size: 4

Should print size of 4: 4
Should print the list array is empty - false: false
Should print Chevy at index 0:
Make: Chevrolet
Model: Bolt
Mileage: 60000.0
Is Used: true

Should print Tesla Model S at index 2:
Make: Tesla
Model: Model S
Mileage: 150.0
Is Used: false

Should print [Chevy, Tesla 3, Tesla S, Nissan]: [
Make: Chevrolet
Model: Bolt
Mileage: 60000.0
Is Used: true
,
Make: Tesla
Model: Model 3
Mileage: 14000.0
Is Used: true
,
Make: Tesla
Model: Model S
Mileage: 150.0
Is Used: false
,
Make: Nissan
Model: Leaf
Mileage: 500.0
Is Used: false
]

***Testing Remove Methods***

Remove index 3, Nissan:
Make: Nissan
Model: Leaf
Mileage: 500.0
Is Used: false

Should print size of 3: 3
Should print is empty - false: false
Should print Chevy at index 0:
Make: Chevrolet
Model: Bolt
Mileage: 60000.0
Is Used: true

Error: Cannot get element at index 3.
Outside bounds of the ArrayList.
Index: 3, Size: 3

Should print [Chevy, Tesla 3, Tesla S]: [
Make: Chevrolet
Model: Bolt
Mileage: 60000.0
Is Used: true
,
Make: Tesla
Model: Model 3
Mileage: 14000.0
Is Used: true
,
Make: Tesla
Model: Model S
Mileage: 150.0
Is Used: false
]

Remove index 0, Chevy:
Make: Chevrolet
Model: Bolt
Mileage: 60000.0
Is Used: true

Should print size of 2: 2
Should print is empty - false: false
Should print Tesla 3 at index 0:
Make: Tesla
Model: Model 3
Mileage: 14000.0
Is Used: true

Should print Tesla S at index 1:
Make: Tesla
Model: Model S
Mileage: 150.0
Is Used: false

Should print [Tesla 3, Tesla S]: [
Make: Tesla
Model: Model 3
Mileage: 14000.0
Is Used: true
,
Make: Tesla
Model: Model S
Mileage: 150.0
Is Used: false
]

Remove index 10, should print error message:
Error: Cannot remove element at index 10. Outside bounds of arraylist.

Should print size of 2: 2
Removing Tesla S - true: true
Should print is empty - false: false
Should print Tesla 3 at index 0:
Make: Tesla
Model: Model 3
Mileage: 14000.0
Is Used: true

Should print size of 1: 1
Should remove Ford - false: false
Should print is empty - false: false
Should print Tesla 3 at index 0:
Make: Tesla
Model: Model 3
Mileage: 14000.0
Is Used: true

Should print size of 1: 1

***Testing Size Method***

Should print size of 20: 20
Should print size of 0: 0
Should print size of 5: 5

***Testing IsEmpty Method***

Should print true: true
Should print false: false

***Testing Contains Method***

Contains Tesla 3, should print true: true
Contains Honda, should print false: false

***Testing IndexOf Method***

Should print index of 0: 0
Index of A, should print index of -1: -1

***Testing Get Method***

Element at 0, should print Tesla Model 3:
Make: Tesla
Model: Model 3
Mileage: 14000.0
Is Used: true

Element at 3, should print Tesla Model 3:
Make: Tesla
Model: Model 3
Mileage: 14000.0
Is Used: true

Element at 6, should print error message:
Error: Cannot get element at index 6.
Outside bounds of the ArrayList.
Index: 6, Size: 1


***Testing Equals Method***

Should print ArrayLists are equal (true): true
Should print ArrayLists are not equal (false): false
Should print ArrayLists are not equal (false): false
Should print false (passing in a null): false
Should print false (passing in a Car): false


Car Class and Vehicle Interface:

  • Notice that the test file tests ArrayLists of data storing type Car and type Integer.
  • For this assignment (to give you more practice for the final exam), you will need to complete a Car class as follows:
/**
 * Car.java
 * @author
 * @author
 * CIS 36B, Lab 9
 */
public class Car implements Vehicular, Comparable<Car>{
    private String make;
    private String model;
    private double mileage;
    private boolean isUsed;
    private static int numVehicles = 0;
   
    /**
     * Default Car constructor
     * Calls the 4-argument constructor
     * Sets make to "No make"
     * Sets model to "No model"
     * Sets mileage to -1
     * Sets isUsed to false
     */
    public Car() {
       
    }
   
    /**
     * 4-argument Car constructor
     * @param make the Car's make
     * @param model the Car's model
     * @param mileage the Car's current mileage
     * @param isUsed whether the Car is used
     * Also increments numVehicles
     */
    public Car(String make, String model, double mileage, boolean isUsed) {
       
    }
   
    /**
     * Updates the make of the Car
     * @param make the new make of Car
     */
    public void setMake(String make) {
       
    }
   
    /**
     * Updates the model of Car
     * @param model the new model
     */
    public void setModel(String model) {
       
    }
   
    /**
     * Updates whether the Car isUsed
     * Sets isUsed to true
     */
    public void updatesIsUsed() {
       
    }
   
    /**
     * Returns the Car's make
     * @return the make of the Car
     */
    public String getMake() {
        return "";
    }
   
    /**
     * Returns the Car's model
     * @return the model of the Car
     */
    public String getModel() {
        return "";
    }
   
    /**
     * Returns whether the Car is used
     * @return the value of isUsed
     */
    public boolean getIsUsed() {
        return false;
    }
   
    /**
     * Returns the total number of Car
     * Objects that have been constructed
     * @return the value of numVehicles
     */
    public static int getNumVehicles() {
        return -1;
    }
   
    /**
     * Returns a String containing the following:
     * A new line character
     * Make:  <make>
     * Model:  <model>
     * Mileage: <mileage>
     * Is Used: <isUsed>
     * A new line character
     */
    @Override public String toString() {
        return "\n";
    }
   
    /**
     * Determines whether this is equal to another Object
     * @param o another Object
     * @return whether the two Objects are equal
     */
    @Override public boolean equals(Object o) {
        return false;
    }
   
    /**
     * Compares this to another Car
     * according to 1. make, 2. model,
     * 3. mileage, 4. isUsed
     * @param c another Car
     * @return an integer comparison of the
     * two Cars
     */
    @Override public int compareTo(Car c) {
        return -1;
    }

}

  • This Class inherits from the below Vehicular interface:
/**
 * Vehicular.java
 * @author jenniferparrish
 * CIS 36B, Lab 9
 */
public interface Vehicular {
   
    /**
     * Updates a vehicle's mileage
     * @param mileage the updated mileage
     */
    void setMileage(double mileage);

   
    /**
     * Returns a vehicle's mileage
     * @return the mileage of the vehicle
     */
    double getMileage();
}


What to Submit:

  • Submit your completed ArrayList.java file and Car.java file (make sure your name(s) are on both files) to Canvas.
  • Submit the pair programming contract to Canvas along with your code
    • One partner submits ArrayList.java, Car.java, and the pair programming contract.
    • The other other partner only submits the pair programming contract.

How You Will Be Graded:

  • You will receive 5 points for each correctly written method (15 methods) which, when tested, produces identical output in the test file to the sample output shown.
  • You will receive 25 points for a correct Car.java class.
  • -5 for any package statements - please remove package statements before submitting
  • -5 for missing pair programming contract
  • -5 for incorrect indentation. Please go to Source->Format to format your code before submitting.
  • -5 if your names are not on the top of ArrayList.java and Car.java
  • No credit if you add any import statements to the top of ArrayList.java