Description
Problem 0. (10 Points) README. Explain in a README.txt file how to compile
(using the javac command) and execute your codes (using the java command), and what
is the expected output of your codes when tested. It should be detailed enough so that
whoever grades your codes can understand what you were doing with your code clearly and
should be able to reproduce your tests following what you wrote in it.
Problem 1. (70 Points) Implement generic interfaces. First study the Cell class
that is given in pages 247–248. It can represent a generic singly linked list. You will modify
the Cell class as below: call it MyNode:
1
public final class MyNode
private E data; // data field must be private
private MyNode
public MyNode (E val, MyNode
// other necessary methods such as getter/setter and iterator() (see below)
}
Task 1. (30 points for the MyNodeIterator
MyNode
java.util.Iterator
https://docs.oracle.com/javase/8/docs/api/java/util/Iterator.html),
with the class header class MyNodeIterator
let it have a private field MyNode
The constructor of class MyNodeIterator
Task 2. (20 points for making MyNode
public final class MyNode
https://docs.oracle.com/javase/8/docs/api/java/lang/Iterable.html. To
do so, you need to define the iterator() method that returns MyNodeIterator
for this MyNode
Task 3. (20 points for the MyNodeTest class – use the skeleton code provided.) Now, if
list is of type MyNode
“for each” for-loop:
for (E e : list) { /* do something with e */ }
Implement the class MyNodeTest that contains the following four static methods:
(a) (5 points) int sum() that accepts a linked list of type MyNode
sums up the values in each node in the linked list, and
(b) (10 points) num sum() accepts a linked list of type MyNode with any element
type that extends Number, and sums up the values in each node in the linked
list into a double value (since double is the largest type within the Number
type). Use a bounded wildcard.
(c) (5 points) print() accepts a linked list of type MyNode with any element type
and prints out the values in each node in the linked list.
(d) The method main() (provided) tests your MyNode
classes. In the main(), an Integer list intlist of type MyNode
a Double list doublelist of type MyNode
invoking the MyNode constructor recursively. And then, we invoke the two
methods print() and int sum() passing intlist as the argument. Also,
2
invoke print() and num sum() with doublelist as the argument. Furthermore, invoke num sum() with intlist as the argument, and notice the power
of bounded wildcards! As seen in the main(), the only collection structure you
are allowed to use in this problem is your own MyNode and nothing else, that
is, you cannot use existing Collection provided by Java such as ArrayList or
LinkedList. Have fun!
Problem 2. (100 Points) Implement a nicer linked list. You may notice in Problem 1 that it is rather inconvenient to build lists with MyNode. Implement another generic
class MyList
have private fields MyNode
To do Tasks 1, 2 and 3, the class header of MyList
public class MyList
Task 1. (5 points) Make MyList
and giving an implementation of iterator().
Task 2. (10 points) Make MyList
and overriding the Object.clone() method. You must give your own explicit
implementation for the clone() method using the for-each loop.
Task 3. (10 points) Make MyList
interface and overriding the compareTo() method. The comparison criterion is the
length of the lists.
Task 4. (10+5 = 15 points) Also, override the Object.equals() and Object.hashCode()
methods. The equals() criteria are (i) the length and (ii) the contents of the lists
(but not the order of the values in the list). For example, if l1 = [1,2,3], l2
= [2,1,3], and l3 = [1,1,2,3], then l1.equals(l2) and l2.equals(l1) must
return true but not l1.equals(l3) or l3.equals(l1) (nor for l2 and l3).
[Hint: Since you will first check whether the lengths of the two lists are the same,
your hashCode() can simply return the length of the list. If the lengths are the
same, then check if the elements are all the same using a nested for-each loop.]
Task 5. (5 + 10 = 15 points) Define the two constructors for the MyList
public MyList(); // create an empty list
public MyList(Iterable
The second constructor should copy the elements in iterable to MyNodes of this
list.
3
Task 6. (45 points) Implement these member methods (with their expected meaning, see
below):
public MyList
public String toString(); (10 points)
public void push(E item); (10 points)
public E pop(); (10 points)
public E peek(); (5 points)
Also include public int getLength() { return length; }; (no points)
A call x.reverse() should reverse x, and return the reversed list as the result.
An easy way to implement reverse() is to reconstruct a new list, and swap that
in place of the original. The toString() method should print out the list in the
following form: a list of integers 1, 2, and 3 should be printed as
[(head: 1) -> (2) -> (3)].
The push method works the same way as the cons (:) operator in Haskell, i.e., it
prepends item at the head of the list as the left-most element. The pop method
removes the head (left-most) element from the list and returns its value. The push
and pop method should modify the length of the list appropriately. The peek
method returns the head element’s value without removing the element.
Class MyListTest with a main() that tests those requirements is provided. Feel free to
expand it to test your implementations. Include the MyListTest class in your .zip file.
Have fun!
4