Created
June 5, 2014 04:02
-
-
Save cdduarte/710c28b5a7e7d347556c to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Driver - for Java generic MyLinkedList<E> class | |
* | |
* @author christopher duarte | |
* @course cis551 | |
* @assignment HW6 | |
* @param <E> | |
*/ | |
public class Driver { | |
/** | |
* @param args | |
*/ | |
public static void main(String[] args) | |
{ | |
System.out.println( "Testing Started" ); | |
MyLinkedList<Integer> a = new MyLinkedList<Integer>(); // default ctor | |
// show initial size MyLinkedList a | |
System.out.printf( "Size of MyLinkedList a: %d%n", a.size() ); | |
// add 10 items to MyLinkedList a (add at the end of the list) | |
for ( int i = 0; i < 10; ++i ) { | |
a.add(i, i); | |
System.out.printf( "Added %d, size of MyLinkedList a: %d%n", i, a.size() ); | |
} | |
// remove an item from MyLinkedList a | |
Integer k = a.remove(5); | |
System.out.printf( "Removed item at index %d, size of MyLinkedList a: %d%n", k, a.size() ); | |
for ( int i = 0; i < a.size(); ++i ) | |
System.out.printf( "Item at index %d = %d%n", i, a.get(i) ); | |
// insert an item at a particular position in MyLinkedList a | |
a.add(7, 999); | |
System.out.printf( "Added %d at index %d, size of MyLinkedList a: %d%n", 999, 7, a.size() ); | |
for ( int i = 0; i < a.size(); ++i ) | |
System.out.printf( "Item at index %d = %d%n", i, a.get(i) ); | |
// set value of item at particular index in MyLinkedList a | |
a.set(6,888); | |
System.out.printf( "Set item at index %d to %d, size of MyLinkedList a: %d%n", 6, 888, a.size() ); | |
for ( int i = 0; i < a.size(); ++i ) | |
System.out.printf( "Item at index %d = %d%n", i, a.get(i) ); | |
a.set(3,8); | |
System.out.printf( "Set additional item at index %d to %d, size of MyLinkedList a: %d%n", 13, 8, a.size() ); | |
int f = a.indexOf(8); | |
System.out.println("The first index of element 8 is at index " + f); | |
System.out.println( "Removing element 8." ); | |
a.remove(8); | |
if ( a.contains(8) ) | |
System.out.println( "8 is contained in MyLinkedList a" ); | |
else | |
System.out.println( "8 is not contained in MyLinkedList a" ); | |
for ( int i = 0; i < a.size(); ++i ) | |
System.out.printf( "Item at index %d = %d%n", i, a.get(i) ); | |
System.out.println("Head of list a:" + a.element()); | |
if (a.add(11) == true) | |
System.out.println( "11 is added to MyLinkedList a" ); | |
for ( int i = 0; i < a.size(); ++i ) | |
System.out.printf( "Item at index %d = %d%n", i, a.get(i) ); | |
a.remove(); | |
System.out.println("Removed current head, head now contains " + a.get(0)); | |
System.out.println("List a is a list of type Integer can we remove String 7 as opposed to Integer 7? " + a.remove("7")); | |
// clear MyLinkedList a | |
a.clear(); | |
System.out.printf( "Cleared MyLinkedList a, size of MyLinkedList a: %d%n", a.size() ); | |
// Exception testing | |
try { | |
@SuppressWarnings("unused") | |
MyLinkedList<Integer> c = new MyLinkedList<Integer>(); // invalid argument | |
} | |
catch (Exception ex) { | |
System.out.println( "Problem attempting construct MyLinkedList c with -1 elements: " + ex ); | |
} | |
try { | |
a.add(999, 7); // try to insert 7 at out-of-bounds index | |
} | |
catch (Exception ex) { | |
System.out.println( "Problem attempting to add 7 at index 999 in MyLinkedList a: " + ex ); | |
} | |
try { | |
a.get(999); // try to retrieve item from out-of-bounds index | |
} | |
catch (Exception ex) { | |
System.out.println( "Problem attempting to get item at index 999 in MyLinkedList a: " + ex ); | |
} | |
try { | |
a.remove(999); // try to remove item at out-of-bounds index | |
} | |
catch (Exception ex) { | |
System.out.println( "Problem attempting to remove item at index 999 in MyLinkedList a: " + ex ); | |
} | |
try { | |
a.set(999, 7); // try to set at out-of-bounds index | |
} | |
catch (Exception ex) { | |
System.out.println( "Problem attempting to set item at index 999 in MyLinkedList a: " + ex ); | |
} | |
System.out.println( "Testing Completed" ); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment