Code in java please import java.util.*; public class QueueLastname //<------ change the name of the class to include your last name { // no code goes here } class Queue { private ArrayList q; public Queue( ) { q = new ArrayList();

C++ Programming: From Problem Analysis to Program Design
8th Edition
ISBN:9781337102087
Author:D. S. Malik
Publisher:D. S. Malik
Chapter18: Stacks And Queues
Section: Chapter Questions
Problem 16PE: The implementation of a queue in an array, as given in this chapter, uses the variable count to...
icon
Related questions
Question

Code in java please


import java.util.*;
public class QueueLastname //<------ change the name of the class to include your
last name
{
// no code goes here
}
class Queue
{
private ArrayList<String> q;
public Queue( )
{
q = new ArrayList<String>();
}
public void enqueue(String num)
{
q.add(num);
}
public String dequeue()
{
return q.remove(0);
}
/*This method goes through the quque and finds the longest name in the q
The queue must be restored in its original state after finding the longest
name*/
public String getLongestName()
{
Stack s = new Stack();
String longest = q.get(0);
// q.remove(0);
boolean b = false;
while(!b)
{
try
{
String front = q.get(0);//get the first element
q.remove(0);//remove it from the q
s.push(front);// push it to the stack
if (front.length() > longest.length() )
longest = front;
}
catch(Exception e)
{
b = true;
}
}
restore(s); // restoring the queue
return longest;

 

 

}
/*
Traverse through the queue and creates a string containing all the names.
must restore the quque to its original state*/
public String toString()
{
Stack c = new Stack();
String s = "";
boolean b = false;
while(!b)
{
try
{
s = s + q.get(0)+ " ";
c.push(q.remove(0));
}
catch(Exception e)
{
b = true;
}
}
restore(c);
return s;
}
/*
This method is called to restore the queue to its original state*/
public void restore(Stack s)
{
boolean b = false;
while(!b)
{
try
{
if (s.isEmpty())
throw new Exception();
String a = (String)s.pop();
q.add(0,a);
}
catch(Exception c)
{
b = true;
}
}
}
/*this method goes through the queue finding the longest name
The queue must be restored to its original state
this method is similar to the longest name*/
public String getShortestName()
{
return "";
}
/*This method reverses the order of the names in the queue */
public void listReversed()
{
}
/*This method finds the verage length of the length of all the names in the q
queue must be restored to its original state*/

 

 

public double getAverageLength()
{
return 0;
}
/*
This method returns true if the q is sorted alphabetically
Queue must be restored to its original state
*/
public boolean ordered()
{
return false;
}
/*this method returns true if the queue is palindrom and returns false otherwise
call the method copy in the palindrome method*/
public boolean isPalindrome()
{
return true;
}
public ArrayList<String> copy()
{
return null;
}
public void preserve(Queue q)
{
boolean b = false;
while(!b)
{
try
{
this.enqueue(q.dequeue());
}
catch (Exception e)
{
b = true;
}
}
}
}
class Driver
{
public static void main(String[] args)
{
// String[] names = {"Alexis", "Zoeehra","Maryam","Jose","Bill","Niksan",
"BB"};
String[] names = {"a", "b", "c", "b","a"};
Queue people = new Queue();
//enqueue the queue using the array called names
for(int i = 0; i < names.length; i++)
{
people.enqueue(names[i]);
}
System.out.println("The q of the people at Bestbuy on black Friday : " +
people);
people.listReversed();

 

 

System.out.println("The q of the people in the reverse order is: "+
people );
people.listReversed();
System.out.println("List is back to its original state: "+ people);
System.out.printf("Average length of all the Strings in the q is = %.2f\n",
people.getAverageLength());
System.out.println("The longest name in the q is = " +
people.getLongestName());
System.out.println(people+"******************");
System.out.println("The shortest name in the q is = " +
people.getShortestName());
System.out.print("is of the list of the people is sorted? " );
if(people.ordered())
System.out.print("Yes");
else
System.out.print("No");
if(people.isPalindrome())
System.out.println("The list is palindrome");
else
System.out.println("The list is not palindrome");
System.out.println(people.isPalindrome());
}
}
/* create your own driver(15 points)
In this driver must use an array q otherwise you will not get any points*/
class YourDriver
{
public static void main (String[] args)
{
//1. declare an arrayq to store some names of your choice
//2. declare a Queue and instantiate it
//3. copy the names from the arrayq to the queue that you declared(for loop
is needed
//4. call all the methods similar to the given driver.
}
}

 

Expert Solution
trending now

Trending now

This is a popular solution!

steps

Step by step

Solved in 3 steps

Blurred answer
Knowledge Booster
Generic Type
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
  • SEE MORE QUESTIONS
Recommended textbooks for you
C++ Programming: From Problem Analysis to Program…
C++ Programming: From Problem Analysis to Program…
Computer Science
ISBN:
9781337102087
Author:
D. S. Malik
Publisher:
Cengage Learning