In: Computer Science
4 Implement a Java program that meets the following requirements
• You can use the Java standard sequence data structure API types for sets, lists, stack,queue and priority queue as needed. All are available in the java.util package, which you will want to import in your program. 1. Argue in code comments which data structure, stack or queue, you will use to implement this method. Implement a method which creates some String objects as food orders for a small restaurant, and add them to your data structure. Using your data structure standard operations, Process the orders in first come, first served order, printing out the order and a message about fulfilling it.
2. Argue in code comments which data structure, array-based list or linked list based list, you will use to implement this method, for best effectiveness and performance. Implement a method which simulates a short line (between 3 and 8) of people waiting for a movie using a data structure. Represent each guest with a String, have at least one guest cut to the front of the line, at least one guest leave the line, and all guests eventually processed by printing a message about it, using your data structure standard operations.
3. Implement a main method to call the above methods, demonstrating them working with printed output
package lists;
import java.util.*;
public class MainDemo {
public static void main(String args[])
{
Demo d=new Demo();
d.restaurant();
d.movie();
}
}
class Demo
{
Scanner s=new Scanner(System.in);
public void restaurant()
{
Queue q=new LinkedList();// queue
follows the mechanism of FIFO(first in first out).
// for implementing orders queue is
the best choice.
System.out.println("Enter how many
orders");
int n=s.nextInt();
System.out.println("Enter list of
orders:");
for(int i=0;i<n;i++)
{
String
order=s.next();
q.add(order);
}
System.out.println("The Processing
of orders is:");
for(int i=0;i<n;i++)
{
System.out.println(q.remove());
}
}
public void movie()
{
LinkedList list = new
LinkedList();// linked list is best suitable to implement the list
rather than array.
// array require more adjustments
than array.
System.out.println("Enter how many
members(3-8)");
int n=s.nextInt();
System.out.println("Enter names of
quests:");
for(int i=0;i<n;i++)
{
String
str=s.next();
list.add(str);
// adding element to list
}
System.out.println("The Processing
order of quests is:");
for(int i=0;i<n;i++)
{
System.out.println(list.get(i)); // processing
order
}
list.removeFirst(); // removing
first element from list
System.out.println("The Processing
order of quests after removing first is:");
for(int
i=0;i<list.size();i++)
{
System.out.println(list.get(i));
}
System.out.println("Enter the
position to remove a guest:");
int pos=s.nextInt();
list.remove(pos);// removing
element form required position.
System.out.println("The Processing
order of quests from the given position is :");
for(int
i=0;i<list.size();i++)
{
System.out.println(list.get(i));
}
}
}
output:
Enter how many orders
4
Enter list of orders:
colby
calas
gumbo
bergenost
The Processing of orders is:
colby
calas
gumbo
bergenost
Enter how many members(3-8)
5
Enter names of quests:
vicky
sam
peter
johm
gosling
The Processing order of quests is:
vicky
sam
peter
johm
gosling
The Processing order of quests after removing first is:
sam
peter
johm
gosling
Enter the position to remove a guest:
2
The Processing order of quests from the given position is :
sam
peter
gosling