Question

Given main(), complete the program to add people to a queue


4.14 LAB: Ticketing service (Queue)

Given main(), complete the program to add people to a queue. The program should read in a list of people's names including "You" (ending with -1), adding each person to thepeopleInQueue queue. Then, remove each person from the queue until "You" is at the head of the queue. Include print statements as shown in the example below.

Ex. If the input is

Zadie Smith
Tom Sawyer
You
Louisa Alcott

the output is

Welcome to the ticketing service... 
You are number 3 in the queue.
Zadie Smith has purchased a ticket.
You are now number 2
Tom Sawyer has purchased a ticket.
You are now number 1
You can now purchase your ticket!

Code that I have been provided:

import java.util.Scanner;
import java.util.LinkedList;
import java.util.Queue;

public class TicketingService {

public static void main (String[] args) {
Scanner scnr = new Scanner(System.in);
String personName = "";
int counter = 0;
int youPosition;

QueuepeopleInQueue = new LinkedList();
  
personName = scnr.nextLine();
while (!personName.equals("-1")) {
// TODO: Add personName to peopleInQueue and
// determine position of "You" (youPosition)

personName = scnr.nextLine();
}

System.out.println("Welcome to the ticketing service... ");
System.out.println("You are number " + youPosition + " in the queue.");

// TODO: In a loop, remove head person from peopleInQueue,
// output their name and that they have purchased a ticket,   
// then output your position in the queue. When you are at
// the head, output that you can purchase your ticket.


}
}

2 0
Add a comment Improve this question Transcribed image text
Answer #1
import java.util.Scanner;
import java.util.LinkedList;
import java.util.Queue;

public class TicketingService {

    public static void main(String[] args) {
        Scanner scnr = new Scanner(System.in);
        String personName = "";
        int counter = 0;
        int youPosition = 0;

        Queue peopleInQueue = new LinkedList();

        personName = scnr.nextLine();
        while (!personName.equals("-1")) {
            peopleInQueue.add(personName);
            ++counter;
            if (personName.equals("You")) {
                youPosition = counter;
            }
            personName = scnr.nextLine();
        }

        System.out.println("Welcome to the ticketing service... ");
        System.out.println("You are number " + youPosition + " in the queue.");

// TODO: In a loop, remove head person from peopleInQueue,
// output their name and that they have purchased a ticket,   
// then output your position in the queue. When you are at
// the head, output that you can purchase your ticket.
        for (int i = youPosition - 1; i >= 1; i--) {
            System.out.println(peopleInQueue.remove() + " has purchased a ticket.");
            System.out.println("You are now number " + i);
        }
        System.out.println("You can now purchase your ticket!");
    }
}

Add a comment
Know the answer?
Add Answer to:
Given main(), complete the program to add people to a queue
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
  • LAB: Ticketing service (Queue)

    LAB: Ticketing service (Queue)Given main(), complete the program to add people to a queue. The program should read in a list of people's names including "You" (ending with -1), adding each person to the peopleInQueue queue. Then, remove each person from the queue until "You" is at the head of the queue. Include print statements as shown in the example below.Ex. If the input is:Zadie Smith Tom Sawyer You Louisa Alcott -1the output is:Welcome to the ticketing service...  You are number 3 in the queue. Zadie Smith has purchased a ticket. You are now number 2 Tom Sawyer has purchased a ticket. You are now number 1 You can now purchase your ticket!TicketingService.javaimport java.util.Scanner; import java.util.LinkedList; import java.util.Queue; public class TicketingService {    public static void main (String[] args) {       Scanner scnr = new Scanner(System.in);...

  • Given main(). complete the program to add people to a queue.

     14.11 LAB: Ticketing service (queue) Given main(). complete the program to add people to a queue. The program should read in a list of people's names including "You" (ending with-1), adding each person to the peopleInQueue queue. Then, remove each person from the queue until "You" is at the head of the queue. Include print statements as shown in the example below. Ex. If the input is: Zadie Smith Tom Sawyer You Louisa Alcott -1 the output is: Welcome to the ticketing service... You are number 3 in the queue. Zadie...

  • THE ENTIRE CODE SHOULD BE IN JAVA Playlist (output linked list) Given main(), complete the SongNode...

    THE ENTIRE CODE SHOULD BE IN JAVA Playlist (output linked list) Given main(), complete the SongNode class to include the printSongInfo() method. Then write the Playlist class' printPlaylist() method to print all songs in the playlist. DO NOT print the dummy head node. Ex: If the input is: Stomp! 380 The Brothers Johnson The Dude 337 Quincy Jones You Don't Own Me 151 Lesley Gore -1 the output is: LIST OF SONGS ------------- Title: Stomp! Length: 380 Artist: The Brothers...

  • JAVA PROGRAMMING Given main(), complete the SongNode class to include the printSongInfo() method. Then write the...

    JAVA PROGRAMMING Given main(), complete the SongNode class to include the printSongInfo() method. Then write the Playlist class' printPlaylist() method to print all songs in the playlist. DO NOT print the dummy head node. Ex: If the input is: Stomp! 380 The Brothers Johnson The Dude 337 Quincy Jones You Don't Own Me 151 Lesley Gore -1 the output is: LIST OF SONGS ------------- Title: Stomp! Length: 380 Artist: The Brothers Johnson Title: The Dude Length: 337 Artist: Quincy Jones...

  • This lab will give you a practice with both queue and stack ADTs. In this work,...

    This lab will give you a practice with both queue and stack ADTs. In this work, you are to determine if an input string is a palindrome. A string of characters is a palindrome if and only if it reads the same forward and backward. Examples: eye, abba, civic, radar, and so on. Sample Output: Please enter a string of characters: abba The given string is a palindrome. Want to examine another string? (y/n): y Please enter a string of...

  • You will write a single java program called MadLibs. java.

    You will write a single java program called MadLibs. java. This file will hold and allow access to the values needed to handle the details for a "MadLibs" game. This class will not contain a maino method. It will not ask the user for any input, nor will it display (via System.out.print/In()) information to the user. The job of this class is to manage information, not to interact with the user.I am providing a MadLibsDriver.java e^{*} program that you can...

  • Modify the following given Java program. You are expected to modify the supplied Exercise1.java to print...

    Modify the following given Java program. You are expected to modify the supplied Exercise1.java to print just the last lines of each paragraph in HTML format. Copy and paste the Sample input on the console then print the Expected output. import java.util.Scanner; public class Exercise1 { /** A simple static string for HTML & table header. */ private static final String HTMLHeader = "<!DOCTYPE html>\n" + " <html>\n" + " <head>\n" + " <title>CSE: Exercise 1</title>\n" + " </head>\n" +...

  • Rewrite the following program using the DecimalFormat class so that your output looks like that below....

    Rewrite the following program using the DecimalFormat class so that your output looks like that below. Once again, the example is not calculated as 3/10ths of a percent. Welcome to NIU Investments! Please enter the amount you would like to invest today: 34543.25 Total Investment: $34,543.25 Service Charge: $22.33    ------------- Total Amount Due: $34,565.58 Thank you for your investment! Program: package org.students; import java.util.Scanner; public class NIUInvestments {    //Declaring constant    public static final double SCHARGE=0.0006464;    public...

  • Q1. Write a program to simulate a grocery waiting queue. Your program should ask the user...

    Q1. Write a program to simulate a grocery waiting queue. Your program should ask the user if they want to add a customer to the queue, serve the next customer in the queue, or exit. When a customer is served or added to the queue, the program should print out the name of that customer and the remaining customers in the queue. The store has two queues: one is for normal customers, another is for VIP customers. Normal customers can...

  • (Java with Netbeans) Programming. Please show modified programming code for already given Main class, and programming...

    (Java with Netbeans) Programming. Please show modified programming code for already given Main class, and programming for code for the new GameChanger class. // the code for Main class for step number 6 has already been given, please show modified progrraming for Main class and GameCHanger class, thank you. package .games; import java.util.Random; import java.util.Scanner; public class Main { public static void main(String args[]) { System.out.println("Welcome to the Number Guessing Game"); System.out.println(); Scanner sc = new Scanner(System.in); // Get upper...

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