Lab Exercise 7 - Lists
Lab Exercise 7 - Lists
This lab assignment demonstrates the notion and use of the Java Linked List.
Lear how to build, sort, and print the contents of a linkedlist.
Problem description:
You are going to create two small Java classes; a driver class that has the static main method and
a regular Java class named MyLinkedList, as follows:
Driver Your test harness class with the static main method
MyLinkedList Your work class for the linked lists
Generate 20 random integers, convert them to objects using the Integer wrapper class and add
them to a linked list. Display the list. Sort the list using the sorting methods from the Collections
class. Display the list again.
Required Methods
1. buildList()
2. displayList()
3. sortList()
Import Statements: (Below are the import statements you will need)
import java.util.Collections;
import java.util.Random;
import java.util.LinkedList;
import java.util.ListIterator;
All this class does is make a call the empty MyLinkedList constructor and to drive all the public
methods. It should be very similar to this:
…………
}
public static void main(String[] args)
{
MyLinkedList link = new MyLinkedList();
link.buildList();
link.displayList();
link.sortList();
link.displayList();
}
}
This class does the heavy lifting and has all of the public methods for this assignment. It has one
empty default constructor. Your job is to create this class and complete the public methods. There
are three methods in this class, as follows:
1. buildList()
2. displayList()
3. sortList()
}
public void buildList()
{
// Your code goes here
}
Programming Hints
Refer to the code examples discussed in the class notes and the book.
The sort is easy. Use the Collections class on your linked list like this:
Collections.sort(list_name);
Your task:
1. Write a Java program to implant and test the linkedlist describe above.
2. Test your program 2 times and save the outputs into 2 separate text files.
3. Output format should have the following:
Important notes:
Add a comment at the beginning of the program that contains team last & name ,
section # , and the lab Exercise #
Add a comment before each method that explains what it does.
Document any unexpected behavior (exception handling) the method might have.
Within your code add comments that document anything unusual or non-intuitive.
Do not write comments that are redundant with in code.
GOOD LUCK