Question

In: Computer Science

Write a Java program that implements a queue in a hospital. I want your program to...

Write a Java program that implements a queue in a hospital. I want your program to ask the user to enter the number of patients then enter the patient number starting from 110 till the end of the queue then print number of patients waiting in the queue.

Suppose you have a queue D containing the numbers (1,2,3,4,5,6,7,8), in this order. Suppose further that you have an initially empty Stack S. Give a code fragment that uses S, to store the elements in the order (8,7,6,5,4,3,2,1) in D.

Solutions

Expert Solution

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

public class HospitalQueue {
    
    private Queue<Integer> patientQueue;
    int numberOfPatients;

    // Constructor to initialize values and create queue.
    public HospitalQueue(int numberOfPatients){
        this.numberOfPatients = numberOfPatients;
        patientQueue = new LinkedList<>();
    }

    // Reverse queue using stack.
    // Keep popping values from queue and adding to stack.
    // Then keep popping values from stack and adding to queue to reverse the stack.
    public void reverseQueue(Stack<Integer> s){

        while(!patientQueue.isEmpty()){
            int front = patientQueue.poll();
            s.push(front);
        }

        while(!s.empty()){
            int top = s.pop();
            patientQueue.add(top);
        }
    }

    public void add(int patientNumber){
        patientQueue.add(patientNumber);
    }

    public static void main(String[] args){

        Scanner sc = new Scanner(System.in);
        int numberOfPatients;
        System.out.println("Enter number of patients");

        numberOfPatients = sc.nextInt();

        HospitalQueue hospital = new HospitalQueue(numberOfPatients);

        // Entering patients till 110 to end of queue.
        for(int count = 0; count < numberOfPatients; count++){
            hospital.add(110 + count);
        }

        // Printing elements in queue before reversing-
        System.out.println("Before reversing - ");
        for(int patient : hospital.patientQueue){
            System.out.printf("%d ", patient);
        }
        System.out.println("");

        // We can use the reverseQueue function to reverse a queue using a stack.
        Stack<Integer> s = new Stack<>();

        hospital.reverseQueue(s);

        // Printing elements in queue after reversing-
        System.out.println("After reversing - ");
        for(int patient : hospital.patientQueue){
            System.out.printf("%d ", patient);
        }
        System.out.println("");



    }
}

The output -

I have assumed somethings while implementing as you have not specified exactly how to implement. So if you want something done differently please comment and I shall change the code as required.

The code has been commented so you can understand the code better.

I would love to resolve any queries in the comments. Please consider dropping an upvote to help out a struggling college kid :)

Happy Coding !!


Related Solutions

Lab Assignment Write a Java program that implements a queue in a hospital. I want your...
Lab Assignment Write a Java program that implements a queue in a hospital. I want your program to ask the user to enter the number of patients then enter the patient number starting from 110 till the end of the queue then print number of patients waiting in the queue. Suppose you have a queue D containing the numbers (1,2,3,4,5,6,7,8), in this order. Suppose further that you have an initially empty Stack S. Give a code fragment that uses S,...
I want to write this program in java. Write a simple airline ticket reservation program in...
I want to write this program in java. Write a simple airline ticket reservation program in java.The program should display a menu with the following options: reserve a ticket, cancel a reservation, check whether a ticket is reserved for a particular person, and display the passengers. The information is maintained on an alphabetized linked list of names. In a simpler version of the program, assume that tickets are reserved for only one flight. In a fuller version, place no limit...
Program in Java Write an algorithm to transfer the elements from queue Q1 to queue Q2,...
Program in Java Write an algorithm to transfer the elements from queue Q1 to queue Q2, so that the contents in Q2 will be in reverse order as they are in Q1 (e.g. if your queue Q1 has elements A, B, and C from front to rear, your queue Q2 should have C, B, and A from front to rear). Your algorithm must explicitly use an additional stack to solve the problem. Write your algorithm in pseudo code first, and...
Write a JAVA program that implements the following disk-scheduling algorithms: FCFS
Write a JAVA program that implements the following disk-scheduling algorithms: FCFS
I want to write java program to implement the indexing concept so as an example we...
I want to write java program to implement the indexing concept so as an example we have file named Students this file contains information like this 112233445 ahmed 222442211 saeed 112453345 john this program should search for the student name by its number so as an output example: enter the student number 112233445 name found : ahmed also i want to print the index number of where does the student name exists
IN jAVA Language PLEASE Write a JAVA program that implements the following disk-scheduling algorithms: a. FCFS...
IN jAVA Language PLEASE Write a JAVA program that implements the following disk-scheduling algorithms: a. FCFS b. SSTF c. SCAN Your program will service a disk with 5,000 cylinders numbered 0 to 4,999. The program will generate a random series of 50 requests and service them according to each of the algorithms you chose. The program will be passed the initial position of the disk head as a parameter on the command line and report the total amount of head...
Write a Java program (use JDBC to connect to the database) that implements the following function...
Write a Java program (use JDBC to connect to the database) that implements the following function (written in pseudo code): (20 points) CALL RECURSION ( GIVENP# ) ; RECURSION: PROC ( UPPER_P# ) RECURSIVE ; DCL UPPER_P# ... ; DCL LOWER_P# ... INITIAL ( ' ' ) ; EXEC SQL DECLARE C CURSOR FOR SELECT MINOR_P# FROM PART_STRUCTURE WHERE MAJOR_P# = :UPPER_P# AND MINOR_P# > :LOWER_P# ORDER BY MINOR_P# ; print UPPER_P# ; DO "forever" ; EXEC SQL OPEN C...
Write a java application that implements ArrayStack in chapter 16 of your textbook.
Write a java application that implements ArrayStack in chapter 16 of your textbook. Your application should have two files: 1. ArrayStack.java: This file will have the implementation of your stack. 2. TestStack.java: This file will have the main method where you declare your stack object and test the different stack operations. In addition to the ArrayStack methods given in the book, you are to write a toString method. This will allow you to print the Stack object from main. You...
write a java program to Implement a Priority Queue using a linked list. Include a main...
write a java program to Implement a Priority Queue using a linked list. Include a main method demonstrating enqueuing and dequeuing several numbers, printing the list contents for each.
Write a Java program that implements the Depth-First Search (DFS) algorithm. Input format: This is a...
Write a Java program that implements the Depth-First Search (DFS) algorithm. Input format: This is a sample input from a user. 3 2 0 1 1 2 The first line (= 3 in the example) indicates that there are three vertices in the graph. You can assume that the first vertex starts from the number 0. The second line (= 2 in the example) represents the number of edges, and following two lines are the edge information. This is the...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT