Homework Help Question & Answers

Queues, Deques and Priority Queues. Enter the necessary code where indicated (using: MyQues.java - below and...

Queues, Deques and Priority Queues.

Enter the necessary code where indicated (using: MyQues.java - below and questions)

How would you use a stack to verify whether a given string is a palindrome or not?

How would you check if a string is a palindrome or not, using queues and deques?

import java.util.*;

/*

* From useQueue, print out

* [Strawberries, Whipped Cream, Blueberries, Whipped Cream,

Strawberries, Whipped Crean, Powdered Choclolate]

*

* From useDeque, print out

* [Pineapple Cake, Whipped Cream, Strawberries, Whipped Cream, Powdered

Choclolate]

*

* From usePque, print out

* [Bananas, Vanilla Wafers, Whipped Cream]

*

* Do not remove or modify the code below.

* You can add code where indicated.

*/

public class MyQues{

public static void main(String[] args){

System.out.println();

useQueue();

System.out.println();

useDeque();

System.out.println();

usePque();

System.out.println();

}

public static void useQueue(){

Queue<String> myQueue = new LinkedList<>();

myQueue.offer("Blueberries");

myQueue.offer("Strawberries");

myQueue.offer("Whipped Cream");

myQueue.poll();

myQueue.poll();

myQueue.offer("Whipped Cream");

myQueue.offer("Strawberries");

myQueue.offer("Whipped Cream");

myQueue.offer("Powdered Choclolate");

// Add code below

// Add code above

System.out.print(myQueue);

}

public static void useDeque(){

Deque<String> myQueue = new LinkedList<>();

myQueue.offer("Blueberries");

myQueue.offer("Strawberries");

myQueue.offer("Whipped Cream");

myQueue.poll();

myQueue.poll();

myQueue.offer("Whipped Cream");

myQueue.offer("Strawberries");

myQueue.offer("Whipped Cream");

myQueue.offer("Powdered Choclolate");

// Add code below

// Add code above

System.out.print(myQueue);

}

public static void usePque(){

PriorityQueue<String> myQueue = new PriorityQueue<>();

myQueue.offer("Blueberries");

myQueue.offer("Strawberries");

myQueue.offer("Whipped Cream");

myQueue.poll();

myQueue.poll();

myQueue.offer("Whipped Cream");

myQueue.offer("Strawberries");

myQueue.offer("Whipped Cream");

myQueue.offer("Powdered Choclolate");

// Add code below

// Add code above

System.out.print(myQueue);

}

}

0 0
Add a comment
Answer #1

Ans:

/*

The required changes that can be done on the above given program so that the expected output is obtained from it is as follows:

*/

import java.util.*;

/*

* From useQueue, print out

* [Strawberries, Whipped Cream, Blueberries, Whipped Cream,

Strawberries, Whipped Crean, Powdered Choclolate]

*

* From useDeque, print out

* [Pineapple Cake, Whipped Cream, Strawberries, Whipped Cream, Powdered

Choclolate]

*

* From usePque, print out

* [Bananas, Vanilla Wafers, Whipped Cream]

*

* Do not remove or modify the code below.

* You can add code where indicated.

*/

public class MyQues{

public static void main(String[] args){

System.out.println();

useQueue();

System.out.println();

useDeque();

System.out.println();

usePque();

System.out.println();

}

public static void useQueue(){

Queue<String> myQueue = new LinkedList<>();

myQueue.offer("Blueberries");

myQueue.offer("Strawberries");

myQueue.offer("Whipped Cream");

myQueue.poll();

myQueue.poll();

myQueue.offer("Whipped Cream");

myQueue.offer("Strawberries");

myQueue.offer("Whipped Cream");

myQueue.offer("Powdered Choclolate");

// Add code below

myQueue.poll();

myQueue.poll();

myQueue.poll();

myQueue.poll();

myQueue.poll();

myQueue.add("Strawberries");

myQueue.add("Whipped Cream");

myQueue.add("Blueberries");

myQueue.add("Whipped Cream");

myQueue.add("Strawberries");

myQueue.add("Whipped Cream");

myQueue.add("Powdered Choclolate");

// Add code above

System.out.print(myQueue);

}

public static void useDeque(){

Deque<String> myQueue = new LinkedList<>();

myQueue.offer("Blueberries");

myQueue.offer("Strawberries");

myQueue.offer("Whipped Cream");

myQueue.poll();

myQueue.poll();

myQueue.offer("Whipped Cream");

myQueue.offer("Strawberries");

myQueue.offer("Whipped Cream");

myQueue.offer("Powdered Choclolate");

// Add code below

myQueue.poll();

myQueue.addFirst("Pineapple Cake");

// Add code above

System.out.print(myQueue);

}

public static void usePque(){

PriorityQueue<String> myQueue = new PriorityQueue<>();

myQueue.offer("Blueberries");

myQueue.offer("Strawberries");

myQueue.offer("Whipped Cream");

myQueue.poll();

myQueue.poll();

myQueue.offer("Whipped Cream");

myQueue.offer("Strawberries");

myQueue.offer("Whipped Cream");

myQueue.offer("Powdered Choclolate");

// Add code below

myQueue.poll();

myQueue.poll();

myQueue.poll();

myQueue.poll();

myQueue.poll();

myQueue.add("Bananas");

myQueue.add("Vanilla Wafers");

myQueue.add("Whipped Cream");

// Add code above

System.out.print(myQueue);

}

}

// OUTPUT:

[Strawberries, whipped cream, Blueberries, whipped cream, Strawberries, whipped cream, Powdered Choclolate] [Pineapple Cake,

// Stack Based Palindrome checking program:

import java.util.*; import java.io.*; public class Main { public static void main(String[] args) { Scanner sc = new Scanner(S

// Program:

import java.util.*;
import java.io.*;

public class Main
{
   public static void main(String[] args) {
      
       Scanner sc = new Scanner(System.in);
       Stack<Character> stack = new Stack<Character>();
       System.out.print("Enter a string: ");
       String s = sc.nextLine();
      
       for(char ch : s.toCharArray()){
       stack.push(ch);
       }
      
       Boolean flag = true;
       for(char ch : s.toCharArray()){
       if(ch != stack.pop()){
       flag = false;
       System.out.println("Entered String "+s+" is not a palindrome!");
       break;
       }
       }
      
       if(flag){
       System.out.println("Entered String "+s+" is a palindrome!");
       }
   }
}

// OUTPUT :

Enter a string: madam Entered String madam is a palindrome! ... Program finished with exit code 0 Press ENTER to exit console

Enter a string: sir Entered String sir is not a palindrome! ... Program finished with exit code 0 Press ENTER to exit console

// Queue Based Palindrome Checking Program:

8 9 Main.java 1 import java.util.*; 2 import java.io.*; 3 4 public class Main 5-{ 6 public static void main(String[] args) {

// Program:

import java.util.*;
import java.io.*;

public class Main
{
   public static void main(String[] args) {
      
       Scanner sc = new Scanner(System.in);
       Queue<Character> queue = new LinkedList<>();
       System.out.print("Enter a string: ");
       String s = sc.nextLine();
      
       for(char ch : s.toCharArray()){
       queue.offer(ch);
       }
      
       Boolean flag = true;
       for(int i = s.length()-1; i >= 0; i--){
       if(s.charAt(i) != queue.poll()){
       flag = false;
       System.out.println("Entered String "+s+" is not a palindrome!");
       break;
       }
       }
      
       if(flag){
       System.out.println("Entered String "+s+" is a palindrome!");
       }
   }
}

// OUTPUT:

Enter a string: racecar Entered String racecar is a palindrome! ... Program finished with exit code 0 Press ENTER to exit con

// Dequeue Based palindrome checking program:

1 9 Main.java import java.util.*; 2 import java.io.*; 3 4 public class Main 5- 6 public static void main(String[] args) { 7 8

// Program:

import java.util.*;
import java.io.*;

public class Main
{
   public static void main(String[] args) {
      
       Scanner sc = new Scanner(System.in);
       Deque<Character> dequeue = new LinkedList<>();
       System.out.print("Enter a string: ");
       String s = sc.nextLine();
      
       for(char ch : s.toCharArray()){
       dequeue.offerLast(ch);
       }
      
       Boolean flag = true;
       for(char ch : s.toCharArray()){
       if(ch != dequeue.pollLast()){
       flag = false;
       System.out.println("Entered String "+s+" is not a palindrome!");
       break;
       }
       }
      
       if(flag){
       System.out.println("Entered String "+s+" is a palindrome!");
       }
   }
}

// OUTPUT:

Enter a string: vacuum Entered String vacuum is not a palindrome! ... Program finished with exit code 0 Press ENTER to exit c

// THANK YOU SO MUCH IN ADVANCE!

// PLEASE DO LIKE AND UPVOTE IF THIS WAS HELPFUL!

Add a comment
Know the answer?
Add Answer to:
Queues, Deques and Priority Queues. Enter the necessary code where indicated (using: MyQues.java - below and...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coin

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
  • Activity 2. Complete the code inside the Java file below, ListUtil.java, such that this class supplies...

    Activity 2. Complete the code inside the Java file below, ListUtil.java, such that this class supplies a utility method to reverse the entries in a linked list. Then, test your code using the tester class, Reverse Tester.java, given below. ListUtil.java import java.util.LinkedList; /** This class supplies a utility method to reverse the entries in a linked list. */ public class ListUtil { /** Reverses the elements in a linked list @param strings the linked list to reverse public static void...

  • What will the code shown below print to the console? public class Cascade public static void...

    What will the code shown below print to the console? public class Cascade public static void print(int arg){ for(int i=0; i<arg; i++){ System.out.print(""); } System.out.println(""); if(arg > 1){ print(arg-1); } } public static void main(String[] args) { print(4); } } E B IV AA- IES XX, SE V GT 12pt Paragraph -

  • BELOW IS THE CODE I ALREADY HAVE Linked List - Delete Modify Lab 5 and include...

    BELOW IS THE CODE I ALREADY HAVE Linked List - Delete Modify Lab 5 and include the method to delete a node from the Linked List. In summary: 1) Add the method Delete 2) Method call to delete, with a number that IS in the list 3) Method call to delete, with a number that is NOT in the list - Be sure to include comments - Use meaningful identifier names (constants where appropriate) import java.io.*; 1/ Java program to...

  • I need to make this code access the main method I suppose, but I don't know...

    I need to make this code access the main method I suppose, but I don't know how to make that happen... Help please! QUESTION: Write a method called switchEmUp that accepts two integer arrays as parameters and switches the contents of the arrays. Make sure that you have code which considers if the two arrays are different sizes. Instructor comment: This will work since you never go back to the main method, but if you did, it wouldn't print out...

  • 10. What prints when the following code is executed? public static void main (String args) "Cattywampus"; for (...

    10. What prints when the following code is executed? public static void main (String args) "Cattywampus"; for (int i-s.length )-1 i> 0 i-2) if (s.charAt (i)a') System.out.print(""); ] else if (s.charAt (i)'t') System.out.print (s.charAt (i-2)) i+ti else System. out. print (s . charAt (İ) ) ; if (i<2) System.out.print ("y"); System.out.println () 10. What prints when the following code is executed? public static void main (String args) "Cattywampus"; for (int i-s.length )-1 i> 0 i-2) if (s.charAt (i)a') System.out.print(""); ]...

  • Priority Queue Demo import java.util.*; public class PriorityQueueDemo {    public static void main(String[] args) {...

    Priority Queue Demo import java.util.*; public class PriorityQueueDemo {    public static void main(String[] args) {        //TODO 1: Create a priority queue of Strings and assign it to variable queue1               //TODO 2: Add Oklahoma, Indiana, Georgia, Texas to queue1                      System.out.println("Priority queue using Comparable:");        //TODO 3: remove from queue1 all the strings one by one        // with the "smaller" strings (higher priority) ahead of "bigger"...

  • (a)How many times does the code snippet given below display "Hello"? int x = 1; while...

    (a)How many times does the code snippet given below display "Hello"? int x = 1; while (x != 15) {    System.out.println ("Hello");    x++; } (b)What is the output of the following code fragment? int i = 1; int sum = 0; while (i <= 5) {    sum = sum + i;    i++; } System.out.println("The value of sum is " + sum); Quie 2 What is the output of the following snipped code? public class Test {...

  • need code to copy and output //Exercise07_02 // Exercise07_02.java: Enter 10 integers and // display the...

    need code to copy and output //Exercise07_02 // Exercise07_02.java: Enter 10 integers and // display the numbers in reverse order public class Exercise07_02 { public static void main (String[] args) { java.util.Scanner input = new java.util.Scanner(System.in); int[] num = new int[10]; for (int i = 0; i <10; i++) { // Read a number System.out.print( "Read a number: "); num[i] = input.nextInt(); } // Display the array reversely System.out.print("The reversal of the input is "); for (int i = 9;...

  • The JCF provides numerous classes that implement the List interface, including LinkedList, ArrayList, and Vector. Write...

    The JCF provides numerous classes that implement the List interface, including LinkedList, ArrayList, and Vector. Write code to show how the JCF class ArrayList and LinkedList are used to maintain a grocery list. Verify List methods such as "add()", get(), "remove()", "isEmpty()" and "size()" by implementing a test program. import java.util.ArrayList; import java.util.Iterator; public class GroceryList { static public void main(String[] args){ ArrayList<String> groceryList = new ArrayList<String>(); Iterator<String> iter;    groceryList.add("apples"); groceryList.add("bread"); groceryList.add("juice"); groceryList.add("carrots"); groceryList.add("ice cream");    System.out.println("Number of items...

  • java programming how would i modify the code below to add a GUI (with a main menu with graphics and buttons, etc...) and include an option to print a list of all students added in a grid format and sh...

    java programming how would i modify the code below to add a GUI (with a main menu with graphics and buttons, etc...) and include an option to print a list of all students added in a grid format and short by name, course, instructor, and location. ///main public static void main(String[] args) { Scanner in = new Scanner(System.in); ArrayList<Student>students = new ArrayList<>(); int choice; while(true) { displayMenu(); choice = in.nextInt(); switch(choice) { case 1: in.nextLine(); System.out.println("Enter Student Name"); String name...

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.