Question

In: Computer Science

You and your closest friends have decided to go to see a private screening of a...

You and your closest friends have decided to go to see a private screening of a movie at a local movie theater where you can be socially distant. Friends will be added to the queue after their names are input by the user. Because of current restrictions, the queue (line) for tickets can only have a capacity of 7 people.

Write a Java program using a Queue (of size 7) that will display a menu:

1) add person to line

2) remove person from line

When (1) is chosen the user will be prompted to input the friend's name. Note that you must not add another person to the queue until there is an available space for them (you will need to check if the queue is full before adding people.) If there are already 7 people in the queue the program will output that "the line is full please try again later."

When (2) is chosen the person at the head/front of the queue is removed.

After each person is added or removed from the queue, output the contents of the queue and the name of the person at the head of the queue. The program will end after the removal of a person leaves the queue empty.

MUST BE JAVA. please include menu for queue operations.

Solutions

Expert Solution

Here is the completed code for this problem. Comments are included, go through it, learn how things work and let me know if you have any doubts or if you need anything to change. If you are satisfied with the solution, please rate the answer. If not, PLEASE let me know before you rate, I’ll help you fix whatever issues. Thanks

//MovieQueue.java

import java.util.Scanner;

public class MovieQueue {

      // a simple class to represent a single node in the queue

      private class Node {

            String name;

            Node next;

      }

      // attributes to first and last nodes

      private Node front, tail;

      // current size

      private int size;

      // constructor

      public MovieQueue() {

            front = tail = null;

            size = 0;

      }

      // method to add a name to the queue

      public void add(String name) {

            // creating a node and setting name

            Node n = new Node();

            n.name = name;

            // if front is null, adding n as both front and tail, else appending to

            // tail and appending tail

            if (front == null) {

                  front = tail = n;

            } else {

                  tail.next = n;

                  tail = n;

            }

            // updating size

            size++;

      }

      // method to remove and return person at front

      public String remove() {

            // returning null if queue is empty

            if (size == 0) {

                  return null;

            } else {

                  // otherwise removing front node, updating tail if needed, updating

                  // size, returning removed value

                  String name = front.name;

                  front = front.next;

                  if (front == null) {

                        tail = null;

                  }

                  size--;

                  return name;

            }

      }

      // method to print queue contents

      public void print() {

            System.out.print("Queue: |");

            // displaying queue elements from front to tail

            for (Node n = front; n != null; n = n.next) {

                  System.out.print(n.name);

                  // appending a comma and space if there are more people

                  if (n.next != null) {

                        System.out.print(", ");

                  }

            }

            // displaying the name of person at the front, or 'No one' if queue is

            // empty

            System.out.println("|\nPerson at the front: "

                        + ((front == null) ? "No one" : front.name));

      }

      // returns the size

      public int getSize() {

            return size;

      }

      // main method

      public static void main(String[] args) {

            // creating a queue

            MovieQueue queue = new MovieQueue();

            int CAPACITY = 7; // to indicate queue capacity

            Scanner sc = new Scanner(System.in);

            boolean done = false;

            // looping until done is true

            while (!done) {

                  // displaying menu and reading choice

                  System.out.println("\n1. add person to line");

                  System.out.println("2. remove person from line");

                  System.out.print("Your choice: ");

                  int ch = Integer.parseInt(sc.nextLine());

                  // finding choice

                  if (ch == 1) {

                        // if queue is full, displayng error

                        if (queue.getSize() == CAPACITY) {

                              System.out

                                          .println("the line is full please try again later.");

                        } else {

                              // otherwise reading name of person, adding to queue

                              System.out.print("Enter name of person: ");

                              String name = sc.nextLine();

                              queue.add(name);

                              // printing queue

                              queue.print();

                        }

                  } else if (ch == 2) {

                        // displaying error if no one is added to the queue yet

                        if (queue.getSize() == 0) {

                              System.out.println("Queue is empty! Add some people first");

                        } else {

                              // else removing and displaying person at front

                              System.out.println(queue.remove()

                                          + " is removed from the line");

                              // displaying queue

                              queue.print();

                              // if queue is now empty, setting done to true

                              if (queue.getSize() == 0) {

                                    System.out.println("Program is ending...");

                                    done = true;

                              }

                        }

                  }

            }

      }

}

/*OUTPUT*/

1. add person to line

2. remove person from line

Your choice: 1

Enter name of person: Alice

Queue: |Alice|

Person at the front: Alice

1. add person to line

2. remove person from line

Your choice: 1

Enter name of person: Bob

Queue: |Alice, Bob|

Person at the front: Alice

1. add person to line

2. remove person from line

Your choice: 1

Enter name of person: Cait

Queue: |Alice, Bob, Cait|

Person at the front: Alice

1. add person to line

2. remove person from line

Your choice: 2

Alice is removed from the line

Queue: |Bob, Cait|

Person at the front: Bob

1. add person to line

2. remove person from line

Your choice: 1

Enter name of person: David

Queue: |Bob, Cait, David|

Person at the front: Bob

1. add person to line

2. remove person from line

Your choice: 1

Enter name of person: Erik

Queue: |Bob, Cait, David, Erik|

Person at the front: Bob

1. add person to line

2. remove person from line

Your choice: 1

Enter name of person: Ford

Queue: |Bob, Cait, David, Erik, Ford|

Person at the front: Bob

1. add person to line

2. remove person from line

Your choice: 1

Enter name of person: Gary

Queue: |Bob, Cait, David, Erik, Ford, Gary|

Person at the front: Bob

1. add person to line

2. remove person from line

Your choice: 1

Enter name of person: Harold

Queue: |Bob, Cait, David, Erik, Ford, Gary, Harold|

Person at the front: Bob

1. add person to line

2. remove person from line

Your choice: 1

the line is full please try again later.

1. add person to line

2. remove person from line

Your choice: 2

Bob is removed from the line

Queue: |Cait, David, Erik, Ford, Gary, Harold|

Person at the front: Cait

1. add person to line

2. remove person from line

Your choice: 2

Cait is removed from the line

Queue: |David, Erik, Ford, Gary, Harold|

Person at the front: David

1. add person to line

2. remove person from line

Your choice: 2

David is removed from the line

Queue: |Erik, Ford, Gary, Harold|

Person at the front: Erik

1. add person to line

2. remove person from line

Your choice: 2

Erik is removed from the line

Queue: |Ford, Gary, Harold|

Person at the front: Ford

1. add person to line

2. remove person from line

Your choice: 2

Ford is removed from the line

Queue: |Gary, Harold|

Person at the front: Gary

1. add person to line

2. remove person from line

Your choice: 2

Gary is removed from the line

Queue: |Harold|

Person at the front: Harold

1. add person to line

2. remove person from line

Your choice: 2

Harold is removed from the line

Queue: ||

Person at the front: No one

Program is ending...


Related Solutions

You have decided to go into the business of selling Motorcycles. You have incorporated your business...
You have decided to go into the business of selling Motorcycles. You have incorporated your business under the name of Miller Corporation On January 1, 20X1, you begin by depositing $50,000 cash in the new corporate bank account ? $30,000 of the money is yours and $20,000 is borrowed from your Uncle Mike. For the $30,000, which is yours, you (as Miller Corporation) issue yourself 300 shares of common stock. For the $20,000 borrowed from your uncle, you sign a...
You and your friends have decided to take a trip after the semester ends to celebrate...
You and your friends have decided to take a trip after the semester ends to celebrate getting through your statistics class. Pick a departure point from a location that is close to your hometown (Denver, CO). You will find a list of ten different destinations for your trip below. ). So, you should have data for 12 different destination points. You need to collect some data online to find the distance between cities as well as the cost of airfare....
You and your roommates have decided to go to a fraternity party on Saturday night. You...
You and your roommates have decided to go to a fraternity party on Saturday night. You arrive at the fraternity house and see another classmate, Jenn, from your psychology class. You go over to say hello and see what she thought of the first exam, but Jenn seems to be acting very strangely. She starts describing fantastic colors made by the lights and how the dance floor is a beautiful sea of diamonds. You take a look around and realize...
You have decided to go into the goat feed business. You’ve been mixing it for your...
You have decided to go into the goat feed business. You’ve been mixing it for your own goats and now neighbors and friends want the same mix. So you need to sit down and do a little economics to figure out whether this is a profit opportunity or not. The production function for your special mix of feed is:                                     Y = X11/2 X21/4 Where Y = lbs of feed mix X1 = lbs. of oats X2 = lbs. of...
You have decided to go into business with your peer and start a business. Which method...
You have decided to go into business with your peer and start a business. Which method of costing would be appropriate for the business? What factors would need to be considered for such a venture? How could the wrong costing method create inaccurate results?
What duties do you owe to the people closest to you, such as family, friends, and...
What duties do you owe to the people closest to you, such as family, friends, and neighbors? What duties do you owe to people in general, any time and any place? Dear Expert, the course is BUS 3305: BUSINESS LAW AND ETHICS UNIT 3: Tort Law Learning Journal Unit 3 Please provide references
Suppose you are considering whether to go to Paris with a group of friends during your...
Suppose you are considering whether to go to Paris with a group of friends during your Christmas holiday. The round trip airfare from Hong Kong to Paris is $12,000, but you have an option to pay this airfare with a frequent- flyer coupon. All other relevant costs of the vacation in Paris add up to $15,000. The most you will be willing to pay for the Paris trip is $25,000, which indicates the benefit you can gain from this trip....
1. Maria’s sisters have died, as have most of her closest friends. At age 78, Maria...
1. Maria’s sisters have died, as have most of her closest friends. At age 78, Maria has left her house, her neighborhood, and her church activities and moved to another state in order to live close to her oldest son and his family. Describe some of the effects this move might have on Maria’s personal relationships. What are some of the risks that will be involved if Maria does not have the ability or the opportunity to make new friends?...
When you decide to go and have a dinner with your friends in a world class hotel such as the Langham hotel or Coronado Beach
  When you decide to go and have a dinner with your friends in a world class hotel such as the Langham hotel or Coronado Beach, perhaps you would be horrified by the high price you would have to pay for a bottle of soft drink such as Coca Cola or Pepsi Cola or wine or even bottled water. Perhaps you begin to ponder why the same commodity that you can get at a supermarket at one tenth the hotel...
When you decide to go and have a dinner with your friends in a world class hotel such as the Langham hotel or Coronado Beach
  When you decide to go and have a dinner with your friends in a world class hotel such as the Langham hotel or Coronado Beach, perhaps you would be horrified by the high price you would have to pay for a bottle of soft drink such as Coca Cola or Pepsi Cola or wine or even bottled water. Perhaps you begin to ponder why the same commodity that you can get at a supermarket at one tenth the hotel...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT