I308 C307 Data/Information Representation

I308 C307 Lab 2

Due Date: Monday, February 7, 2022.

In this lab, we will create a set of Java classes for manipulating a linked list containing integers.

Ex. 1. The Node Class

In Eclipse or Netbeans, create a project called hw4. In this project, create a class called Node in a package called hw4. In this class, declare attributes for an integer called datum and for a link to another node called next.

Create a constructor for this class with no parameters, and one that takes one integer to be stored in the datum of the new node. Then create a constructor that takes another node as parameter and copies the datum from it, but initializes the next as null.

Ex. 2 The List (container) Class

a. Class, attributes, constructor. Create a second class in the same package called List. Declare some attributes for the head, tail, and size, just like in the slides, and a default constructor together with a function called clear creating an empty list, just like in the slides.

b. Insert and remove. Add three functions to this class, using the lecture slides as a starting point:

c. List Traversal. Add a function called print to the class List that outputs all the nodes in the list.

Add another function called isOrdered that returns true if the list is in ascending order and false if it isn't.

Ex. 3 Interface and Main

Add a class Main to the project containing a main function. We're going to add some tests here for the functions seen so far.

In the main, declare a Scanner object for easier input. For this, at the top of the file, inside the package but before the class, add a statement to import the module java.util.Scanner. Then add a variable called scan of type Scanner and initialize it with new Scanner(System.in).

Declare a variable of type List and initialize it with an empty list.

Add the following menu to this class:

Add the appropriate actions in the main for each of these options. Test the program to see if the code written so far works.

Ex. 4 Iterators

Add a class ListIterator to the project and declare an attribute of type Node called current to this class. Add a default constructor, one with a node as parameter, and one with another iterator.

Add the following methods to this class:

For these functions, you can use the code of the class and of the methods from the lecture slides.

In the class List, add a method called begin that returns an iterator containing a reference to the first node in the list. Also in this class, add an overloaded method called toString() that returns a string containing the information in the list. For this one, use iterators for the traversal.

In the main, add an option to print the list as a simple output, where you use the function toString in the output.

Also in the Main class, add a static traversal function for the list using iterators, that computes the sum of the elements of the list. Add options for the new functions in the interface.

This program will be continued in the homework.

Lab Submission

Upload all the .java files to Canvas - Assignments - Homework 4 after you complete the homework.