Question

In: Computer Science

Implement these methods (adjust DoubleList.java only for errors if you think needed): getNode – take in...

Implement these methods (adjust DoubleList.java only for errors if you think needed):

getNode – take in one int parameter indicating the index of the node to retrieve (index 0 is the front). If that index is out of the bounds of the list, throw a DoubleListException with an appropriate message. Otherwise, determine which half of the list the index is in, and traverse to it using the shortest traversal to get there, by calling either traverseForwards or traverseBackwards with the number of steps to get to the index from the corresponding end (if it's the very middle, you can decide which way to go). For example, consider a list with 5 nodes. Calling getNode(1) should retrieve the node immediately after front using traverseForwards. Calling getNode(3) should retrieve the node immediately after rear using traverseBackwards. Return the found node.

setElement – take in two input parameters: index (int) and element (generic type). Call the getNode method described below to find the node to be updated, and then called setElement on that node with the given element

getElement – take in one input parameter: index (int). Call the getNode method with the given index and return the data element of the node at that position.

toString – returns the string representing the list from the front to the rear with a space between each node. If the list is empty, then return the string "Empty list".

DoubleNode.java

public class DoubleNode{

private DoubleNode next;

private DoubleNode previous;

private T element;

/**

* Constructor with no input parameters.

*/

public DoubleNode(){

next = null;

previous = null;

element = null;

}

/**

* Constructor with one input parameter representing the node's data element.

* @param elem

*/

public DoubleNode (T elem){

next = null;

previous = null;

element = elem;

}

/**

* Get the next node.

* @return next node

*/

public DoubleNode getNext(){

return next;

}

/**

* Get the previous node.

* @return previous node

*/

public DoubleNode getPrevious(){

return previous;

}

/**

* Set the next node.

* @param node

*/

public void setNext (DoubleNode node){

next = node;

}

/**

* Set the previous node.

* @param node

*/

public void setPrevious (DoubleNode node){

previous = node;

}

/**

* Get the data element.

* @return data element.

*/

public T getElement(){

return element;

}

/**

* Set the data element.

* @param elem

*/

public void setElement (T elem){

element = elem;

}

/**

* Return the node's data element for printing purposes.

* @return string of node's data element

*/

public String toString () {

return element.toString();

}

}

DoubleList.java

public class DoubleList{

DoubleNode front,rear;

private T count;

public DoubleList () {

front = null;

rear = null;

count = 0;

}

public void addToRear(T elem){

DoubleNode new_node = new DoubleNode(elem);

if (front.getElement() == null){

front.setElement(new_node);

rear.setElement(new_node);

} else if (front.getElement() != null){

new_node.setPrevious(rear);

rear.setNext(new_node);

rear = new_node;

}

count = count + 1;

}

public void traverseForwards(T elem){

DoubleNode numNode = new DoubleNode(elem);

curNode = front.getElement();

for (i = 0; i = numNode; ++i){

System.out.println(curNode);

curNode = curNode.getNext();

} if (front.getElement == null){

System.out.println(null);

}

}

public void traverseBackwards(T elem){

DoubleNode numNode = new DoubleNode(elem);

curNode = rear.getElement();

for (i = 0; i = numNode; ++i){

System.out.println(curNode);

curNode = curNode.getPrevious();

} if (rear.getElement == null){

System.out.println(null);

}

}

Solutions

Expert Solution

Program

//DoubleNode.java

public class DoubleNode<T>{
   private DoubleNode<T> next;
   private DoubleNode<T> previous;
   private T element;

   /**
   * Constructor with no input parameters.
   */
  
   public DoubleNode(){
       next = null;
       previous = null;
       element = null;
   }
  
   /**
   * Constructor with one input parameter representing the node's data element.
   * @param elem
   */
   public DoubleNode (T elem){
       next = null;
       previous = null;
       element = elem;
   }
  
   /**
   * Get the next node.
   * @return next node
   */
   public DoubleNode<T> getNext(){
       return next;
   }
  
   /**
   * Get the previous node.
   * @return previous node
   */
   public DoubleNode<T> getPrevious(){
       return previous;
   }
  
   /**
   * Set the next node.
   * @param node
   */
  
   public void setNext (DoubleNode<T> node){
       next = node;
   }
  
   /**
   * Set the previous node.
   * @param node
   */
   public void setPrevious (DoubleNode<T> node){
       previous = node;
   }
  
   /**
   * Get the data element.
   * @return data element.
   */
   public T getElement(){
       return element;
   }
  
   /**
   * Set the data element.
   * @param elem
   */
   public void setElement (T elem){
       element = elem;
   }
  
   /**
   * Return the node's data element for printing purposes.
   * @return string of node's data element
   */
   public String toString () {
       return element.toString();
   }
}


//DoubleList.java

//DoubleListException class

class DoubleListException extends Exception
{
   DoubleListException(String s)
   {
       super(s);
   }
}

public class DoubleList<T>{
   DoubleNode<T> front,rear;
   private int count;

   public DoubleList () {
       front = null;
       rear = null;
       count = 0;
   }
  
   public void addToRear(T elem){
  
       DoubleNode<T> new_node = new DoubleNode<T>(elem);
      
       if (front == null){      
           front = new_node;      
           rear = new_node;      
       }
       else {  
           new_node.setPrevious(rear);      
           rear.setNext(new_node);      
           rear = new_node;      
       }      
       count = count + 1;  
   }
  
   public DoubleNode<T> traverseForwards(int pos){
      
       if (front == null){
           return null;  
       }
  
       DoubleNode<T> curNode = front;
      
       for (int i = 0; i < pos; ++i){
           curNode = curNode.getNext();
       }
       return curNode;
   }
  
   public DoubleNode<T> traverseBackwards(int pos){
  
       if (rear == null){
           return null;
       }
      
       DoubleNode<T> curNode = rear;
      
       for (int i = 0; i < pos; ++i){                  
           curNode = curNode.getPrevious();
       }
       return curNode;
   }
  
   /**
   * Get the node at index.
   * @param index represents the position of the list
   * @return the node at index
   */
   public DoubleNode<T> getNode(int index) throws DoubleListException
   {
       if (index < 0 || index >= count) {
           throw new DoubleListException("Invalid index");
       }
      
       if (index < count / 2)
           return traverseForwards(index);
      
       return traverseBackwards(index);
   }
  
   /**
   * Set the data element.
   * @param index represents the position of the list
   * @param elem represents element (generic type) to be set at index
   */
   public void setElement (int index, T elem) throws DoubleListException {
       DoubleNode<T> curNode = getNode(index);
      
       curNode.setElement(elem);
   }
  
   /**
   * Get the data element.
   * @param index represent the position of the list.
   * @return the data element of the node at index position.
   */
   public T getElement(int index) throws DoubleListException{
       DoubleNode<T> curNode = getNode(index);
       return curNode.getElement();
   }
   /**
   * @return the string representing the list from the front to the rear
   * with a space between each node. If the list is empty, then return the string "Empty list".
   */
   public String toString()
   {
       if (rear == null)
           return "Empty list";
      
       DoubleNode<T> curNode = front;
      
       String s = "";
       for (int i = 0; i < count; ++i){
           s = s + curNode.getElement() + " ";                  
           curNode = curNode.getNext();
       }
       return s;
   }
}

//Driver class for testing
class Driver
{
   public static void main (String[] args) throws DoubleListException
   {
       DoubleList<Integer> list = new DoubleList<Integer>();
      
       list.addToRear(10);
       list.addToRear(20);
       list.addToRear(30);
       list.addToRear(40);
       list.addToRear(50);
      
       System.out.println (list);
      
       System.out.println (list.getElement(2));
   }
}

Output:

10 20 30 40 50
30


Related Solutions

Please fill in the code where it says to implement Methods needed to implement include: insert,...
Please fill in the code where it says to implement Methods needed to implement include: insert, find, remove, and toIndex // // STRINGTABLE.JAVA // A hash table mapping Strings to their positions in the the pattern sequence // You get to fill in the methods for this part. // public class StringTable {    private LinkedList<Record>[] buckets; private int nBuckets; // // number of records currently stored in table -- // must be maintained by all operations // public int...
How do you think an organization determines that it's time to implement new strategies and methods...
How do you think an organization determines that it's time to implement new strategies and methods such as Six Sigma?
1. Why are adjustments needed on the worksheet? 2. Why do you adjust for supplies used?...
1. Why are adjustments needed on the worksheet? 2. Why do you adjust for supplies used? 3. What is depreciation? Why does it need to be adjusted? 4. Why is a worksheet useful in preparing the financial statements? 5. Why do you create a journal for adjusting entries? 6.What do you think happens to financial statements if this entry is not completed. 7. Do you think companies could get into trouble by not recognizing prepaid expenses? Answer each question in...
You shall implement six static methods in a class named BasicBioinformatics. Each of the methods will...
You shall implement six static methods in a class named BasicBioinformatics. Each of the methods will perform some analysis of data considered to be DNA. DNA shall be represented arrays of chars containing only the characters A, C, G and T. In addition to the six methods you will implement, six other methods exist in the class, which use Strings instead of char arrays to represent DNA. These other methods simply invoke the methods you are to implement, so all...
In this project you will implement the DataFrame class along with the all the methods that...
In this project you will implement the DataFrame class along with the all the methods that are represented in the class definition. DataFrame is a table with rows and columns – columns and rows have names associated with them also. For this project we will assume that all the values that are stored in the columns and rows are integers. After you have tested your class, you have to execute the main program that is also given below. DataFrame Class...
write a Matlab program to implement the perceptron learning rule to adjust the value of weight...
write a Matlab program to implement the perceptron learning rule to adjust the value of weight w. You can choose your own learning rate and stop criteria. Try different values of the learning rate and stop criteria, what do you find?
Do you think immigration reform is needed? Do you think tightening immigration standards will improve jobs...
Do you think immigration reform is needed? Do you think tightening immigration standards will improve jobs for Americans or reduce them? Do you think immigration reform will prevent terrorism on U.S. Soil? Would reform also make it easier for government entities to tax those that are here illegally? Would you be in favor of granting an amnesty for those people that are here illegally?
Why do you think Odebrecht needed politicians to win contracts?
Why do you think Odebrecht needed politicians to win contracts?
In this assignment, you implement a 2D-matrix as a vector of vectors, and only use at()...
In this assignment, you implement a 2D-matrix as a vector of vectors, and only use at() to access its elements. Write a program that multiplies a 2D matrix with a vector. If you need to see the math, follow this link: https://mathinsight.org/matrix_vector_multiplication (Links to an external site.) For simplicity, our matrix will be of size 3 x 3. Initialize the matrix as shown in to become [1.0, 2.0, 3.0] [4.0 ,5.0 ,6.0] [7.0, 8.0, 9.0] Read the three values of...
In Java For this assignment you will implement two methods, find() and replace() relating to Binary...
In Java For this assignment you will implement two methods, find() and replace() relating to Binary Search Trees. These methods are both to be recursive functions. The signatures for those functions must be: /* This method takes a tree node and a key as an argument and returns the tree node if the key is found and returns null if the key is not found. */ BST find(BST T, char key) /* This method takes a tree node and a...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT