Home CPSC 340

Lab 2: DynamicList Methods

 

Due: September 4


 

Objective

To work with the ArrayList implementation we developed in class.


 

Task

For this lab you will be adding a couple of methods to the DynamicList class we developed.


 

Details


 

Testing

You can test your code with the following main class:


public class DynamicListTest {
    public static void main(String args[]) {
        DynamicList<String> names = new DynamicList<String>(30);
        names.add("Alice");
        names.add("Bob");
        names.add("Claire");
        names.add("Dominic");
        names.add("Estelle");
        names.add("Frank");
        names.add("Gwen");
        names.add("Hugo");
        names.add("Irene");
        names.add("Claire");
        names.add("Jack");

        // test the set method
        names.set(1, "Billy");
        System.out.println(names.get(1));
        try {
            names.set(100, "Slartibartfast");
        } catch (IndexOutOfBoundsException e) {
            System.out.println("Exception thrown correctly");
        }

        // test the last index method
        int index = names.lastIndexOf("Claire");
        System.out.println("Last index of Claire is " + index);

        // call the trimToSize method -- should ensure that works in method
        names.trimToSize();

        // print the final list
        System.out.println("List contents:");
        for (int i = 0; i < names.size(); i++) {
            System.out.println("\t" + names.get(i));
        }
    }
}

This code should then produce the following output:

Billy
Exception thrown correctly
Last index of Claire is 9
Size = 11
Capacity = 11
List contents:
    Alice
    Billy
    Claire
    Dominic
    Estelle
    Frank
    Gwen
    Hugo
    Irene
    Claire
    Jack

 

Submitting

When you are finished, please upload your code to the Canvas page for this assignment. Please send the .java file(s) only.

Copyright © 2020 Ian Finlayson | Licensed under a Creative Commons Attribution 4.0 International License.