Вы находитесь на странице: 1из 3

Lab Exercise 7

CSci-161 Computer Science II: Sec 1 & 2


Fall 2010
(20 Points)
Due: Friday, Nov 19th, 2010
Objectives:

 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

Process (data objects stored in the list)

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

Your application needs the following 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;

The Test Harness Class

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 class Driver


{
public Driver()
{

…………
}
public static void main(String[] args)
{
MyLinkedList link = new MyLinkedList();

link.buildList();
link.displayList();
link.sortList();
link.displayList();
}
}

The MyLinkedList Class

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()

(The Import Statements goes here)

public class MyLinkedList


{
private LinkedList list = new LinkedList();
public MyLinkedList()
{

}
public void buildList()
{
// Your code goes here
}

public void displayList()


{
// Your code goes here
}

public void sortList()


{
// 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:

Output from 1st run:

a. Original unsorted list


b. Sorted list

Output from 2nd run:

c. Original unsorted list


d. Sorted list

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.

What you need to submit

1. In your personal storage, create a zipped file called “Lab7_lastName1_ lastName2


“(all in one zipped file) from the folder (include all files i.e. source code files &
output files).

2. In Blackboard, in the Lab Exercise 7 assignment, submit your zipped file.

If you have any question, feel free to ask any time.

GOOD LUCK

Вам также может понравиться