I just need help with these two classes the other files dont
I just need help with these two classes the other files don\'t have to be modified but i will provide the uml for the array list and the linked list
ListNode<E extends Comparable<E>>
- data : E
- nextNode: ListNode<E>
<<constructor>>ListNode(d : E)
<<constructor>>ListNode(d : E, node : ListNode<E>)
+ setData(d : E)
+getData() : E
+setNext(next : ListNode<E>)
+getNext() : ListNode<E>
Notes on ListNode
ListNode(d : E) sets the nextNode to null, the rest of the implementation of the ListNode class is self-explanatory as discussed in class
UML DIAGRAM FOR AND DISCUSSION FOR LinkedList
LinkedList<E extends Comparable<E>>
- firstNode : ListNode<E>
- lastNode : ListNode<E>
- numElements : int
- name : String
<<constructor>>LinkedList()
<<constructor>>LinkedList(name : String)
+ insertAtFront(item : E)
+ insertAtBack(item : E)
+ removeFromFront() : E throws EmptyListException
+ removeFromBack() : E throws EmptyListException
+ removeItem(index : int) : E throws IndexOutOfBoundsException
+ getItem(index : int) : E throws IndexOutOfBoundsException
+ setItem(index : int, item : E) throws IndexOutOfBoundsException
+ findAndRemove(item : E) : Boolean
+ findItem(item E) : int
+ lengthIs() : int
+ clear()
+ toString()
+ isEmpty() : Boolean
+ sort() throws EmptyListException
ArrayList<E extends Comparable<E>>
- DEFCAP : int final = 50
- origCap : int
- numElements : int
- list : Object[]
<<constructor>>ArrayList()
<<constructor>>ArrayList(size : int) throws InvalidSizeException
+ addItem(item : E) throws MaximumCapacityException
+ getItem(index int) : E throws IndexOutOfBoundsException
+ setItem(index int, item E) throws IndexOutOfBoundsException
+ removeItem(index int) : E throws IndexOutOfBoundsException
+ findItem(item E) : int
+ isEmpty() : Boolean
+ lengthIs() : int
+ clear()
+ toString() : String
+ sort()
- enlarge() throws MaximumCapacityException
Notes on ArrayList
The ArrayList class implements an ArrayList data structure, capable of handling types that implement Comparable<E>. The logic for most of the methods was covered in class, you’ll have to convert these to take advantage of the Generic type that is passed.
Constructors
The constructor that takes no size will set the origCap to DEFCAP and this will be the initial size.
The constructor that accepts a size parameter must throw an InvalidSizeException if the size passed is greater than the DEFCAP or less than 1.
Methods
public void addItem(E item) throws MaximumCapacityException
Attempts to add a new item to the ArrayList. If the array is at capacity it attempts to enlarge, then adds the item to the end.
public E getItem(int index)
This method is to retrieve an item in the list given an index into the ArrayList. If that index does not exist within the ArrayList an IndexOutOfBoundsException is thrown with the message: “Index out of Range”.
public setItem(int index, E item)
Attempts to place the passed item into the given index. If that index does not exist within the ArrayList an IndexOutOfBoundsException is thrown with the message: “Index out of Range”.
public E removeItem(int index)
Attempts to remove the item at the given index from the ArrayList. If that index does not exist within the ArrayList an IndexOutOfBoundsException is thrown with the message: “Index out of Range”.
public int findItem(E item)
Performs a linear search to fine the item passed. Returns the index of the item if found, returns -1 if not found.
public Boolean isEmpty()
Returns a true if the ArrayList is empty, otherwise false.
public int lengthIs()
Returns the current number of elements in the ArrayList.
public void clear()
Clears contents of the ArrayList, sets size of array to the original capacity.
public String toString()
Returns a String containing all elements in the ArrayList, separated by a new line.
public void sort()
Sorts the contents of the ArrayList using the Insertion Sort.
private void enlarge()
Will attempt to enlarge the ArrayList by the value of origCap. If this would put it past its maximum size defined by DEFCAP it will enlarge to the size of DEFCAP. If it is already at size of DEFCAP it will throw a MaximumCapacityException.
| ListNode<E extends Comparable<E>> |
| - data : E - nextNode: ListNode<E> |
| <<constructor>>ListNode(d : E) <<constructor>>ListNode(d : E, node : ListNode<E>) + setData(d : E) +getData() : E +setNext(next : ListNode<E>) +getNext() : ListNode<E> |
Solution
Solution:
I have tried the above project…
ListNode class:
import java.util.Scanner;
class ListNode
{
protected int data;
protected ListNode nextNode;
/* Constructor */
public ListNode()
{
nextNode = null;
data = 0;
}
/* Constructor */
public ListNode(int d,ListNode n)
{
data = d;
nextNode = n;
}
/* Function to set link to next Node */
public void setNext(ListNode n)
{
nextNode = n;
}
/* Function to set data to current Node */
public void setData(int d)
{
data = d;
}
/* Function to get link to next node */
public ListNode getNext()
{
return nextNode;
}
/* Function to get data from current Node */
public int getData()
{
return data;
}
public static void main(String[] args)
{
// TODO Auto-generated method stub
}
}
Class: LinkedList.java
class LinkedList
{
protected int numElements;
protected String name;
protected ListNode firstNode;
protected ListNode lastNode;
/* Constructor */
public LinkedList()
{
firstNode = null;
lastNode = null;
numElements = 0;
name = null;
}
/* Constructor */
public LinkedList(String n)
{
name = n;
}
public void insertAtFront(ListNode n)
{
firstnode = n;
}
public void insertAtBack(ListNode n)
{
lastnode = n;
}
public void removeFromFront()
{
firstnode = null;
}
public void removeFromBack()
{
lastnode = null;
}
/* Function to get link to next node */
public ListNode getItem(int index)
{
return numElement;
}
public Boolean isEmpty()
{
return (numElements ==0);
}
}





