Question: Given a completed MyNode.java, a completed MyNodeIterator.java, and a skeleton for MyNodeTest.java: Implement the class MyNodeTest that contains the following four static methods: (a) (5
Given a completed MyNode.java, a completed MyNodeIterator.java, and a skeleton for MyNodeTest.java:
Implement the class MyNodeTest that contains the following four static methods:
(a) (5 points) int sum() that accepts a linked list of type MyNode and sums up the values in each node in the linked list, and
(b) (5 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. 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) (10 points) main() that tests your MyNodeIterator class. In the main(), create the intlist of type MyNode with six integers, and then invoke the two methods print() and int sum() passing intlist as the argument. Also, create the doublelist of type MyNode with six double values, and then invoke print() and num sum() with doublelist as the argument. Also, invoke num sum() with intlist as the argument, and feel the power of bounded wildcards! 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. If you use such existing Collection structure, you will receive zero for this problem.
MyNodeTest.java
import static java.lang.System.out;
public class MyNodeTest { // int_sum method
// num_sum method
// print method
// main method
}
MyNode.java
import java.lang.Iterable;
// Total 20 points // correct class header => 5 points public final class MyNode implements Iterable{ // private fields private final E val; // value field must be private private MyNode next; // next field must also be private
// (5 points) constructor public MyNode (E val, MyNode node) { this.val = val; this.next = node; }
// (5 points) iterator() returns a MyNodeIterator object for this object @Override public MyNodeIterator iterator() { return new MyNodeIterator(this); }
// (5 points) getter and setter methods for the private fields public E getVal() { return val; } public MyNode getNext() { return next; } public void setNext(MyNode node) { next = node; } }
MyNodeIterator.java
import java.util.Iterator;
import java.util.NoSuchElementException;
import java.lang.UnsupportedOperationException;
// Total 25 points
// correct class header => 5 points
class MyNodeIterator implements Iterator{
private MyNode p;
// (5 points) constructor
public MyNodeIterator (MyNode n) {
p = n;
}
// (15 points) methods to implement the Iterator interface
@Override
public boolean hasNext() {
return p != null;
}
@Override
public E next() {
if (!hasNext()) throw new NoSuchElementException();
E val = p.getVal();
p = p.getNext();
return val;
}
}
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
