Question
Objectives: Implementing ListADT and OrderedListADT interfaces with doubly linked nodes Implementing Comparable interface Implementing Iterable interface Implementing Iterator interface with links Provided Files: ListADT OrderedListADT
Objectives:
Implementing ListADT and OrderedListADT interfaces with doubly linked nodes Implementing Comparable interface Implementing Iterable interface Implementing Iterator interface with links
Provided Files:
ListADT
OrderedListADT
DoubleNode
OrderedListTest.java
OrderedListTest2.java
Dog1.java
Lab Assignment:
In this lab, you are going to store String and Dog objects in two separate ordered lists. Objects in the string list will be sorted alphabetically. Objects in the dog list will be sorted by age.
1. Write DoubleLinkedList class that implements the provided ListADT interface and java.lang.Iterable interface. Use the provided DoubleNode class to implement DoubleLinkedList class.
public class DoubleLinkedList
protected DoubleNode
} In the DoubleLinkedList class write a private inner class ListIterator that implements the Iterator interface.
2. Write OrderedDoubleLinkedList class that extends DoubleLinkedList class and implements the provided OrderedListADT.
public class DoubleOrderedLinkedList
/** * Creates an empty list using the default capacity. */ public DoubleOrderedList() { super(); } public void add(T element) { //complete method add } }
3. Dog1 class implements interface Comparable. Complete method compareTo in class Dog1. Method compareTo compares two dogs by their age in an increasing order. Method compareTo will be used in the add method in the OrderedDoubleLinkedList class. Method add in the OrderedDoubleLinkedList adds dog objects to the list in the order specified by the method compareTo in the Dog1 class.
/** * Compares two dogs age attribute. * @param t, t Dog object * @return zero when the object before the dot operator has the same * age as the parameter object. Returns -1 when this object's age is smaller * then the age of the parameter object, otherwise it returns 1 */ public int compareTo(Dog1 t) { //use an if else if statement to to compare dog's ages //COMPLETE THIS METHOD }
4. Override method equals in Dog1 class. Method equals is required in method find and method remove. In method equals compare for equality instance variables of the current object (object before the dot operator) with instance variables of the parameter object. public boolean equals(Object t) { Dog1 d = (Dog1) t; //COMPLETE THIS METHOD }
5.Use the provided OrderedListTest.java to create a small ordered list of Strings. Use OrderedListTest2.java to create a small ordered list containing dog objects. Expand the the provided application to test all methods. You must use an iterator to iterate over one or both of the lists.
Provided files
package jsjf; import java.util.Iterator; import jsjf.exceptions.*; /** * ListADT defines the interface to a general list collection. Specific * types of lists will extend this interface to complete the * set of necessary operations. * * @author Lewis and Chase * @version 4.0 */ public interface ListADTextends Iterable { /** * Removes and returns the first element from this list. * * @return the first element from this list */ public T removeFirst() throws EmptyCollectionException; /** * Removes and returns the last element from this list. * * @return the last element from this list */ public T removeLast() throws EmptyCollectionException; /** * Removes and returns the specified element from this list. * * @param element the element to be removed from the list */ public T remove(T element) throws EmptyCollectionException, ElementNotFoundException; /** * Returns a reference to the first element in this list. * * @return a reference to the first element in this list */ public T first() throws EmptyCollectionException; /** * Returns a reference to the last element in this list. * * @return a reference to the last element in this list */ public T last() throws EmptyCollectionException; /** * Returns true if this list contains the specified target element. * * @param target the target that is being sought in the list * @return true if the list contains this element */ public boolean contains(T target) throws EmptyCollectionException; /** * Returns true if this list contains no elements. * * @return true if this list contains no elements */ public boolean isEmpty(); /** * Returns the number of elements in this list. * * @return the integer representation of number of elements in this list */ public int size(); /** * Returns an iterator for the elements in this list. * * @return an iterator over the elements in this list */ public Iterator iterator(); /** * Returns a string representation of this list. * * @return a string representation of this list */ public String toString(); }
________________________________________________________________________________________________________________________________________________________
package jsjf; /** * OrderedListADT defines the interface to an ordered list collection. Only * Comparable elements are stored, kept in the order determined by * the inherent relationship among the elements. * * @author Lewis and Chase * @version 4.0 */ public interface OrderedListADTextends ListADT { /** * Adds the specified element to this list at the proper location * * @param element the element to be added to this list */ public void add(T element); }
________________________________________________________________________________________________________________________________________________________
/** */ package collections; class DoubleNode{ private DoubleNode next; private T element; private DoubleNode previous; /** * * Creates an empty node. */ public DoubleNode() { next = null; element = null; previous = null; } /** * * Creates a node storing the specified element. * * * @param elem the element to be stored into the new node */ public DoubleNode(T elem) { next = null; element = elem; previous = null; } /** * * Returns the node that follows this one. * * * @return the node that follows the current one */ public DoubleNode getNext() { return next; } /** * * Returns the node that precedes this one. * * * @return the node that precedes the current one */ public DoubleNode getPrevious() { return previous; } /** * * Sets the node that follows this one. * * * @param dnode the node to be set as the one to follow the current one */ public void setNext(DoubleNode dnode) { next = dnode; } /** * * Sets the node that precedes this one. * * * @param dnode the node to be set as the one to precede the current one */ public void setPrevious(DoubleNode dnode) { previous = dnode; } /** * * Returns the element stored in this node. * * * @return the element stored in this node */ public T getElement() { return element; } /** * * Sets the element stored in this node. * * * @param elem the element to be stored in this node */ public void setElement(T elem) { element = elem; } }
________________________________________________________________________________________________________________________________________________________
package application; import collections.DoubleOrderedList; import exceptions.*; public class OrderedListTest { /** * @param args the command line arguments */ public static void main(String[] args) { DoubleOrderedListlist = new DoubleOrderedList(); list.add("sam"); list.add("tom"); list.add("bob"); list.add("ali"); System.out.println(list.first()); System.out.println(list.toString()); try { list.remove("ali"); } catch (ElementNotFoundException | EmptyCollectionException e) { System.out.println(e.getMessage() + ""); } list.removeFirst(); System.out.println(list.toString()); //test the iterator. use a while loop ////test remaining methods } }
________________________________________________________________________________________________________________________________________________________
package application; import collections.DoubleOrderedList; import exceptions.*; import housing.*; import java.util.Iterator; public class OrderedListTest2 { public static void main(String[] args) { DoubleOrderedListlist = new DoubleOrderedList(); list.add(new Dog("Fido", 14)); list.add(new Dog("Fido", 7)); list.add(new Dog("Fido", 4)); list.add(new Dog("Fido", 1)); Dog d = list.first(); System.out.println(list.toString()); System.out.println(list.contains(d)); try { Dog test = new Dog("Fido", 4); list.remove(test); } catch (ElementNotFoundException | EmptyCollectionException e) { System.out.println(e.getMessage() + ""); } System.out.println(list.toString()); Iterator i = list.iterator(); //test the iterator and the other methods } }
________________________________________________________________________________________________________________________________________________________
package application; public class Dog1 implements Comparable{ private String name; private int age; /** * Constructs Dog object * * @param name, the name of the dog * @param age, the age of the dog */ public Dog1(String name, int age) { this.name = name; this.age = age; } /** * returns dogs name * * @returns name, the dog's name */ public String getName() { return name; } /** * Returns dog's age * * @return age, the dog's age */ public int getAge() { return age; } /** * Returns a string containing dog's attributes * * @return a string containing dog's attributes */ @Override public String toString() { return "name: " + name + " age: " + age; } /** * Compares two dogs attributes. * @param t, t Dog object * @return zero when the object before the dot operator has the same * age as the parameter object. Returns -1 when this object's age is smaller * then the age in the parameter object, otherwise it returns 1 */ @Override public int compareTo(Dog1 t) { return 0; //COMPLETE THIS METHOD } /** * Compares two dogs for equality * @param t, t Dog object * @return true when the attributes of the object before the dot operator * and the attributes of the parameter objects are the same */ @Override public boolean equals(Object t) { Dog1 d = (Dog1) t; return false; //COMPLETE THIS METHOD } }
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started