Question

Project Requirements In both cases use the queue classes we have created in class, not the Java API queue options. Be sure to2.) Create a simulation of an intersection using queues. Assume that there is a single lane going irn each direction, that thSome hints . Ultimately, with four variables, that means you will have four loops nested within each other in order to try alE Cars: the number of cars in the east bound lane waiting to go at the conclusion of the experiment W Cars: the number of car

------------------------------------------------------------------------------------------------------------ CODE ALREADY HAVE BELOW---------------------------------------------------------------------------------------


public class LinkedQueue<T> implements QueueADT<T>
{

   private int count;
   private LinearNode<T> head;
   private LinearNode<T> tail;
          
   public LinkedQueue()
   {
       count = 0;
       head = null;
       tail = null;
   }
   @Override
   public void enqueue(T element)
   {
       LinearNode<T> node = new LinearNode<T> (element);
       if(isEmpty())
           head = node;
       else
           tail.setNext(node);
       tail = node;
       ++count;
      
   }

   @Override
   public T dequeue() throws EmptyCollectionException
   {
       if (isEmpty())
       {
           throw new EmptyCollectionException();
       }
          
           T result = head.getElement();
           head = head.getNext();
           --count;
           if(isEmpty())
               tail = null;
           return result;
       }
      
  
  

   @Override
   public T first() throws EmptyCollectionException
   {
       if(isEmpty())
           throw new EmptyCollectionException();
       return head.getElement();
   }

   @Override
   public int size()
   {
       return count;
   }

   @Override
   public boolean isEmpty()
   {
       return count<=0;
   }
  
   public String toString()
   {
       String output = "LinkedQueue -> [ ";
       LinearNode<T> current = head;
       while(current != null)
       {
           output+= current.getElement().toString() + " ";
           current = current.getNext();
       }
       output+= "]";
       return output;
   }

}


public class LinearNode<T>
{
   private LinearNode<T> next;
   private T           element;
  
   public LinearNode()
   {
       next = null;
       element = null;
   }
  
   public LinearNode(T elem)
   {
       next = null;
       element = elem;
   }
  
   public LinearNode<T> getNext()
   {
       return next;
   }
  
  
   public void setNext (LinearNode<T> node)
   {
       next = node;
   }
   public T getElement()
   {
       return element;
   }
  
   public void setElement(T elem)
   {
       element = elem;
   }
}


public interface QueueADT<T>
{
       public void enqueue(T element);
      
       public T dequeue() throws EmptyCollectionException;
      
       public T first() throws EmptyCollectionException;
      
       public int size();
      
       public boolean isEmpty();
}


public class EmptyCollectionException extends Exception
{
  
   private static final long serialVersionUID = 358083002087971606L;

   public EmptyCollectionException()
   {
       super();
   }
  
   public EmptyCollectionException(String msg)
   {
       super(msg);
   }
}

// P 121 Chap 5
public class CircularArrayQueue<T> implements QueueADT<T>
{
   private final static int DEFAULT_CAPACITY = 100; //Static b/c it's only 1 version of it
   private int front, rear, count;
   private T[] queue;
  
   @SuppressWarnings("unchecked")
   public CircularArrayQueue(int initialCapacity)
   {
       front = rear = count = 0;
       queue = (T[]) new Object [ initialCapacity];
   }
     
   public CircularArrayQueue()
   {
       this(DEFAULT_CAPACITY);
   }
  
   public void enqueue(T element)
   {
       if (size() == queue.length)
           expandCapacity();
      
       queue [rear] = element;
       rear = (rear + 1) % queue.length;
       ++count;
   }
  
  

   public T dequeue() throws EmptyCollectionException
   {
       if(isEmpty())
      
           throw new EmptyCollectionException();
      
       T result = queue[front];
       queue [front] = null;
       front = (front + 1) % queue.length;
       --count;
      
       return result;
   }
  
   public T first() throws EmptyCollectionException
   {
       if(isEmpty())
           throw new EmptyCollectionException();
      
       return queue[front];
   }
  
   public int size()
   {
       return count;
   }

   public boolean isEmpty()
   {
       return count <=0;
   }
  
   private void expandCapacity()
   {
       @SuppressWarnings("unchecked")
       T[] larger = (T[]) new Object [queue.length * 2];
       for(int i = 0; i < count; i++)
       {
           larger[i] = queue [front];
           front = (front + 1) % queue.length;
       }
       front = 0;
       rear = count;
      
       queue = larger;
   }
  
   public String toString()
   {
       String output= "CircularArrayQueue -> [ ";
       int pointer = front;
       for(int i =0; i < count; i++)
       {
           output+= queue [pointer] + " ";
           pointer = (pointer + 1) % queue.length;
       }
       output+= "]";
       return output;
   }

  
}

0 0
Add a comment Improve this question Transcribed image text
Answer #1

//Here's the code with comments for both the parts of the question 1:


public class Palindrome{
public static void main (String[] args) throws Exception{
checkPalindrome();
}

public static void checkPalindrome() throws Exception{
Scanner sc = new Scanner(System.in);
System.out.print("Enter a string: ");
// taking the input into the variable str
String str = sc.nextLine();
// checking using the queue
boolean queueCheck = checkForPalindromeUsingQueue(str);
// checking using the stack
boolean stackCheck = checkForPalindromeUsingStack(str);
String isNotStringQueue = queueCheck ? "IS" : "IS NOT";
String isNotStringStack = stackCheck ? "IS" : "IS NOT";
// printing the desired output
System.out.println("According to the queue method that statement " + isNotStringQueue + " a palindrome");
System.out.println("According to the queue method that statement " + isNotStringStack + " a palindrome");
System.out.println("Try another (y/n)?");
String input = sc.nextLine();
if (input.equals("y"))
checkPalindrome();
}

public static boolean checkForPalindromeUsingQueue(String str) throws Exception{
LinkedQueue queue = new LinkedQueue();
// storing the string character by character into the queue in reverse order
// because queue follows FIRST IN FIRST OUT logic
for(int i = str.length()-1; i >= 0; i--)
queue.enqueue(str.charAt(i));
// creating the reverse string using the queue created above
// on dequeuing the queue, it will output a new string in the reverse order of the original string
String reverse = "";
while(!queue.isEmpty()){
reverse += queue.dequeue();
}
// if the reverse string is equal to the original string, then str is a palindrom otherwise no.
// same logic is followed in the implementation using the stack except that the stack data structure is
// used to store the string character by character.
return reverse.equals(str);
}

public static boolean checkForPalindromeUsingStack(String str){
Stack stack = new Stack();
for(int i = 0; i < str.length(); i++)
stack.push(str.charAt(i));

String reverse = "";
while(!stack.isEmpty())
reverse+=stack.pop();
return reverse.equals(str);
}
}

Add a comment
Know the answer?
Add Answer to:
------------------------------------------------------------------------------------------------------------ CODE ALREADY HAVE BELOW--------------------------------------------------------------------------------------- public class LinkedQueue<T>
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Please help with my car traffic simulator! Code that I already have below, I do not know how to...

    Please help with my car traffic simulator! Code that I already have below, I do not know how to start it off! public class IntersectionSimulation { private final static int EAST_WEST_GREEN_TIME = 30 ; private final static int[] NORTH_SOUTH_GREEN_TIMES = { 20, 24, 30, 42 } ; private final static int[] CAR_INTERSECTION_RATES = { 3, 5, 10 } ; private final static int[] CAR_QUEUEING_RATES = { 5, 10, 30 } ; private final static int[] EXPERIMENT_DURATIONS = { 3*60, 5*60,...

  • There is a data structure called a drop-out stack that behaves like a stack in every...

    There is a data structure called a drop-out stack that behaves like a stack in every respect except that if the stack size is n, then when the n+1element is pushed, the bottom element is lost. Implement a drop-out stack using links, by modifying the LinkedStack code. (size, n, is provided by the constructor. Request: Please create a separate driver class, in a different file, that tests on different types of entries and show result of the tests done on...

  • I just need a java mehod that follows the Javadocs Implented using an arraylist. public class...

    I just need a java mehod that follows the Javadocs Implented using an arraylist. public class WorkAheadQueue<T> implements WorkAheadQueueADT<T> {     private LinearNode<T> front;     private LinearNode<T> back;     private int numNodes;     private ArrayList<LinearNode<T>> frontFive; Removes and returns the element that is at place x in the queue. Precondition: x must be less than 5, x must be less than size * Note: indexing from 0: 0-front element, I =-second element, etc. eparam x the passed in index of...

  • Are based on the following Queue class code segment class QueueFull {/* Empty exception class */};...

    Are based on the following Queue class code segment class QueueFull {/* Empty exception class */}; Class Queue Empty {/* Empty exception class */}; struct Node//Node structure int data;//Holds an integer Node* next;//Pointer to next node in the queue}; Class Queue//Linked node implementation of Queue ADT {Private: Node* front;//Pointer to front node of queue Node* rear;//pointer to last node of queue Public: Queue ()://default constructor initializes queue to be empty -Queue ();//Deallocates all nodes in the queue Void Add (int...

  • In addition to the base files, three additional files are attached: EmptyCollectionException.java, LinearNode.java, and StackADT.java. These...

    In addition to the base files, three additional files are attached: EmptyCollectionException.java, LinearNode.java, and StackADT.java. These files will need to be added to your Java project. They provide data structure functionality that you will build over. It is suggested that you test if these files have been properly added to your project by confirming that Base_A05Q1.java compiles correctly. Complete the implementation of the ArrayStack class. Specifically, complete the implementations of the isEmpty, size, and toString methods. See Base_A05Q1.java for a...

  • Java help: Please help complete the locate method that is in bold.. public class LinkedDoubleEndedList implements...

    Java help: Please help complete the locate method that is in bold.. public class LinkedDoubleEndedList implements DoubleEndedList { private Node front; // first node in list private Node rear; // last node in list private int size; // number of elements in list ////////////////////////////////////////////////// // YOU MUST IMPLEMENT THE LOCATE METHOD BELOW // ////////////////////////////////////////////////// /** * Returns the position of the node containing the given value, where * the front node is at position zero and the rear node is...

  • Complete the implementation of the LinkedStack class presented in Chapter 13. Specifically, complete the implementations of...

    Complete the implementation of the LinkedStack class presented in Chapter 13. Specifically, complete the implementations of the peek, isEmpty, size, and toString methods. See Base_A06Q1.java for a starting place and a description of these methods. Here is the base given: /** * Write a description of the program here. * * @author Lewis et al., (your name) * @version (program version) */ import java.util.Iterator; public class Base_A06Q1 { /** * Program entry point for stack testing. * @param args Argument...

  • Step 3 Develop the following class: Class Name: ImprovedQueue<T> Access Modifier: public Implements: QueueInterface<T> Instance variables...

    Step 3 Develop the following class: Class Name: ImprovedQueue<T> Access Modifier: public Implements: QueueInterface<T> Instance variables Name: front Access modifier: private Data type: QueueNode<T> Constructors: Name: ImprovedQueue Access modifier: public Parameters: none (default constructor) Task: sets the value of front to null Methods Name: isEmpty Access modifier: public Parameters: none Return type: boolean Task: returns true if the front is equal to null; otherwise return false Name: dequeue                           Access modifier: public Parameters: none Return type: T (parameterized type) Task: makes...

  • What is wrong with my code, when I pass in 4 It will not run, without...

    What is wrong with my code, when I pass in 4 It will not run, without the 4 it will run, but throw and error. I am getting the error   required: no arguments found: int reason: actual and formal argument lists differ in length where T is a type-variable: T extends Object declared in class LinkedDropOutStack public class Help { /** * Program entry point for drop-out stack testing. * @param args Argument list. */ public static void main(String[] args)...

  • use intellij idea main java Step 3 Develop the following class: Class Name: ImprovedQueue<T> Access Modifier:...

    use intellij idea main java Step 3 Develop the following class: Class Name: ImprovedQueue<T> Access Modifier: public Implements: QueueInterface<T> Instance variables Name: front Access modifier: private Data type: Queue Node<T> Constructors: Name: ImprovedQueue Access modifier: public Parameters: none (default constructor) Task: sets the value of front to null Methods Name: isEmpty Access modifier: public Parameters: none Return type: boolean Task: returns true if the front is equal to null; otherwise return false Name: dequeue Access modifier: public Parameters: none Return...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT