Question

In: Computer Science

java code Add the following methods to the LinkedQueue class, and create a test driver for...

java code

    1. Add the following methods to the LinkedQueue class, and create a test driver for each to show that they work correctly. In order to practice your linked list cod- ing skills, code each of these methods by accessing the internal variables of the LinkedQueue, not by calling the previously de?ined public methods of the class.
    1. String toString() creates and returns a string that correctly represents the current queue. Such a method could prove useful for testing and debugging the class and for testing and debugging applications that use the class. Assume each queued element already provides its own reasonable toString method.
    2. voidremove(intcount)removesthefrontcountelementsfromthe queue; throws QueueUnderflowException if less than count elements are in the queue.
    3. boolean swapStart() returns false if less than two elements are in the queue, otherwise reverses the order of the front two elements in the queue and returns true.
    4. booleanswapEnds()returnsfalseiftherearelessthantwoelementsin the queue, otherwise swaps the ?irst and last elements of the queue and returns true.

Solutions

Expert Solution


public class LinkedQueue {
  
   class DoubleLinkedNode {

       public DoubleLinkedNode next;
       public DoubleLinkedNode prev;
       public int element;
      
       public DoubleLinkedNode(int num){
           next = null;
           prev = null;
           element = num;
       }

   }
  
DoubleLinkedNode front, rear;
  
  

public String toString()
{
   DoubleLinkedNode curr=front;
   String s= "";
   while(curr.next!=null)
   {
       s = s + String.valueOf(curr.element)+" ";
       curr=curr.next;
   }
   s = s + String.valueOf(curr.element);
   return s;
}

public void remove(int count)
{
   count--;
   if(front==null)
       new Error("QueueUnderflowException");
   if(count==0) {
       front = front.next;
          return ;
   }
   DoubleLinkedNode curr=front;
   int i=0;
   while(curr!=null && i!=count-1)
   {
       curr = curr.next;
       i++;
   }
     
   if(i!=count-1)
       new Error("QueueUnderflowException");
   if(curr.next.next==null)
       rear=rear.prev;
         
     
   curr.next = curr.next.next;
   if(curr.next!=null)
       curr.next.prev=curr;
}

public boolean swapStart() {
   if(front==null || front.next==null)
       return false;
     
   DoubleLinkedNode curr=front;
   front = front.next;
   front.prev=null;
   if(front.next!=null)
       front.next.prev=curr;
   curr.next=front.next;
   curr.prev=front;
   front.next=curr;
   if(front.next.next==null)
       rear = front.next;
   return true;
     
}

public boolean swapEnds() {
   if(front==null || front.next==null)
       return false;
   if(front.next.next==null)
       return swapStart(); //when Queue contain two element
   rear.next=front.next;
   rear.prev.next=front;
   front.prev=rear.prev;
   front.next.prev=rear;
   front.next=null;
   rear.prev=null;
   DoubleLinkedNode curr;
   curr=front;
   front=rear;
   rear=curr;
   return true;
     
}
  

}


Related Solutions

28. Add the following methods to the ArrayBoundedStack class, and create a test driver for each...
28. Add the following methods to the ArrayBoundedStack class, and create a test driver for each to show that they work correctly. In order to practice your array related coding skills, code each of these methods by accessing the internal variables of the ArrayBoundedStack, not by calling the previously defined public methods of the class. a. String toString()—creates and returns a string that correctly represents the current stack. Such a method could prove useful for testing and debugging the class...
Add the following method below to the CardDeck class, and create a test driver to show...
Add the following method below to the CardDeck class, and create a test driver to show that they work correctly. int cardsRemaining() //returns a count of the number of undealt cards remaining in the deck. Complete in Java programming language. // Models a deck of cards. Includes shuffling and dealing. //---------------------------------------------------------------------- package Homework4; import java.util.Random; import java.util.Iterator; import javax.swing.ImageIcon; public class CardDeck { public static final int NUMCARDS = 52; protected ABList<Card> deck; protected Iterator<Card> deal; public CardDeck() { deck...
Code in Java Given the LinkedList class that is shown below Add the following methods: add(String...
Code in Java Given the LinkedList class that is shown below Add the following methods: add(String new_word): Adds a linkedlist item at the end of the linkedlist print(): Prints all the words inside of the linkedlist length(): Returns an int with the length of items in the linkedlist remove(int index): removes item at specified index itemAt(int index): returns LinkedList item at the index in the linkedlist public class MyLinkedList { private String name; private MyLinkedList next; public MyLinkedList(String n) {...
//Complete the incomplete methods in the java code //You will need to create a driver to...
//Complete the incomplete methods in the java code //You will need to create a driver to test this. public class ManagedArray { private int[] managedIntegerArray; //this is the array that we are managing private int maximumSize; //this will hold the size of the array private int currentSize = 0; //this will keep track of what positions in the array have been used private final int DEFAULT_SIZE = 10; //the default size of the array public ManagedArray()//default constructor initializes array to...
PLEASE CODE THIS IN JAVA Create a driver class Playground that contains the function, public static...
PLEASE CODE THIS IN JAVA Create a driver class Playground that contains the function, public static void main(String[] args) {}. Create 2 SportsCar and 2 Airplane instances using their constructors. (SPORTSCAR AND AIRPLANE CLASSES LISTED BELOW THIS QUESTION. Add all 4 instances into a single array called, “elements.” Create a loop that examines each element in the array, “elements.” If the elements item is a SportsCar, run the sound method and if the item is an Aeroplane, run it’s ChangeSpeed...
IN JAVA: Repeat Exercise 28, but add the methods to the LinkedStack class. Add the following...
IN JAVA: Repeat Exercise 28, but add the methods to the LinkedStack class. Add the following methods to the LinkedStacked class, and create a test driver for each to show that they work correctly. In order to practice your array related coding skills, code each of these methods by accessing the internal variables of the LinkedStacked, not by calling the previously defined public methods of the class. - String toString()—creates and returns a string that correctly represents the current stack....
Code in Java Create a stack class to store integers and implement following methods: 1) void...
Code in Java Create a stack class to store integers and implement following methods: 1) void push(int num): This method will push an integer to the top of the stack. 2) int pop(): This method will return the value stored in the top of the stack. If the stack is empty this method will return -1. 3) void display(): This method will display all numbers in the stack from top to bottom (First item displayed will be the top value)....
In this class add Comparable interface. In the driver program create a few objects and In...
In this class add Comparable interface. In the driver program create a few objects and In the driver program create a few objects and compare them . then create a list of those objects and sort them .A Quadratic is bigger than another Quadratic if it opens faster package pack2; /** * This is a program for defining a quadratic equation * @author sonik */ public class Quadratic { public int coeffX2 = 0; public int coeffX = 0; public...
java Objective: Create a class. Create objects. Use methods of a class. Create a class BankAccount...
java Objective: Create a class. Create objects. Use methods of a class. Create a class BankAccount to represent a bank account according to the following requirements: A bank account has three attributes: accountnumber, balance and customer name. Add a constructor without parameters. In the initialization of the attributes, set the number and the balance to zero and the customer name to an empty string. Add a constructor with three parameters to initialize all the attributes by specific values. Add a...
Add code (see below for details) to the methods "set" and "get" in the following class,...
Add code (see below for details) to the methods "set" and "get" in the following class, ArrayTen.java, so that these two methods catch the exception java.lang.ArrayIndexOutOfBoundsException if an illegal index is used, and in turn throw java.lang.IndexOutOfBoundsException instead. Modify the "main" method to catch java.lang.IndexOutOfBoundsException and, when such an exception is caught, print the exception as well as the stack trace. public class ArrayTen { private String myData[] = new String[10]; public void set(int index, String value) { myData[index] =...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT