Skip to content

Programming by Design

If you're not prepared to be wrong, you'll never come up with anything original. – Sir Ken Robinson

  • About
  • Java-PbD
  • C-PbD
  • ASM-PbD
  • Algorithms
  • Other

CISS-111 Extra Credit Lists

Posted on March 27, 2013August 13, 2024 By William Jojo
CISS-111-Project

In this project, you will take the code from Example 7 in the Generic Methods and Classes topic and make some modifications.

Change the class to:

  • Maintain a tail node to reference the last node in the chain.
  • Maintain a size as an int that represents the number of nodes in the chain.
  • Make the original insert() method add to the end of the chain instead of the beginning.
  • Alter the constructor as needed to satisfy the above.

To this code, you will also create and add the following new methods:

public int indexOf(Object o)
Return the index of the first occurrence of the object o or -1 if not found.
public int size()
Return the number of items in the list.
public Object[] toArray()
Return an array of Object that contains the items in the list.
public void delete(int index)
Delete the item at the index or throws IndexOutOfBoundsException.
public void insert(int i, T item)
Inserts item at position i or throws IndexOutOfBoundsException.
public T elementAt(int index)
Returns the element at position index or throws IndexOutOfBoundsException.
public GenList(T[] array)
Optional EXTRA CREDIT 3 Points: Takes an array of T type objects to populate the list.

Table 1: Methods to augment the existing GenList.

This is a simple augmentation exercise. You will take something that already exists in a generic form and extend its usefulness. It will give you further exposure to generic classes and methods and some direct experience in testing with them.

Important Note!
You will throw an IndexOutOfBoundsException whenever an index argument would go out of bounds.

Here is a test program to exercise your new GenList:

TestGenListString.java
public class TestGenListString {
 
  public static void main(String[] args) {
 
    GenList<String> ll = new GenList<String>();
 
    System.out.println("\nAdding Blue, Red, Orange and Green to the list");
    ll.insert("Blue");
    ll.insert("Red");
    ll.insert("Orange");
    ll.insert(3,"Green"); // adds directly to end using index.
    System.out.println(ll);
    System.out.println("size is now " + ll.size());
    System.out.println("The index of Orange is " + ll.indexOf("Orange"));
    
    System.out.println("\nAdding Yellow to index 2.");
    ll.insert(2, "Yellow");
    System.out.println(ll);
    System.out.println("size is now " + ll.size());
    System.out.println("The index of Yellow is " + ll.indexOf("Yellow"));
    
    System.out.println("\nTesting toArray()");
    Object[] o = ll.toArray();
    System.out.println("Object array:");
    for (int x = 0; x < o.length; x++)
      System.out.println("o[" + x + "] = " + o[x]);
 
    System.out.println("\nRemoving Green from end of list");
    ll.delete("Green"); // beginning
    System.out.println(ll);
    System.out.println("size is now " + ll.size());
 
    System.out.println("\nThe element at index 1 is " + ll.elementAt(1));

    System.out.println("\nTesting IndexOutOfBoundsException for elementAt()");
    try {
      System.out.println("THIS IS OUT OF BOUNDS " + ll.elementAt(10));
    }
    catch (IndexOutOfBoundsException e) {
      System.out.println("Hurray! Caught the bad one!");
    }

    System.out.println("\nRemoving Red (index 1) from middle of list");
    ll.delete(1); // Red at index 1.
    System.out.println(ll);
    System.out.println("size is now " + ll.size());
 
    System.out.println("\nRemoving Blue (index 0) from front of list");
    ll.delete(0); // front
    System.out.println(ll);
    System.out.println("size is now " + ll.size());
    
    System.out.println("\nRemoving Orange (index 1) from end of list");
    ll.delete(1); // Orange at index 1.
    System.out.println(ll);
    System.out.println("size is now " + ll.size());
  }
}

The output should be very close to:

Adding Blue, Red, Orange and Green to the list
[Blue, Red, Orange, Green]
size is now 4

The index of Orange is 2

Adding Yellow to index 2.
[Blue Red Yellow Orange Green]
size is now 5

The index of Yellow is 2

Testing toArray()
Object array:
o[0] = Blue
o[1] = Red
o[2] = Yellow
o[3] = Orange
o[4] = Green

Removing Green from end of list
[Blue Red Yellow Orange]
size is now 4

The element at index 1 is Red

Testing IndexOutOfBoundsException for elementAt()
Hurray! Caught the bad one!

Removing Red (index 1) from middle of list
[Blue Yellow Orange]
size is now 3

Removing Blue (index 0) from front of list
[Yellow Orange]
size is now 2

Removing Orange (index 1) from end of list
[Yellow]
size is now 1

Submit the project to the Learning Management System as GenList.java.

Post navigation

❮ Previous Post: CISS-111 Project 6
Next Post: CISS-111 Project 7 ❯

Creative Commons License
This work is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License.

Copyright © 2018 – 2025 Programming by Design.