Question

In: Computer Science

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, to store the elements in the order (8,7,6,5,4,3,2,1) in D.

Q. What values are returned during the following sequence of queue operations, if executed on an initially empty queue?

enqueue(5), enqueue(3), dequeue(),

enqueue(2), enqueue(8), dequeue(), dequeue(), enqueue(9), enqueue(1),

dequeue(), enqueue(7), enqueue(6), dequeue(), dequeue(), enqueue(4),

dequeue(), dequeue().

Solutions

Expert Solution

Code:

import java.util.*;
class QueueClass {
    int[] queueType;
    private int size;
    static int MAX = 10;
    public QueueClass() {
        size = 0;
        queueType = new int[MAX];
    }
    public void enqueue(int v) {
        if (isFull()) return;
        queueType[size] = v;
        size++;
    }
    public int dequeue() {
        if (!isEmpty()) {
            int v = queueType[0];
            for (int i = 0; i < size - 1; i++) {
                queueType[i] = queueType[i + 1];
            }
            size--;
            return v;
        }
        return -1;
    }
    public boolean isEmpty() {
        return size == 0;
    }
    public boolean isFull() {
        return size == MAX;
    }
    public int getSize() {
        return size;
    }
    public void printQueue() {
        for (int i = 0; i < size; i++)
            System.out.print(queueType[i] + " ");
        System.out.println("");
    }
}
class TestQueueClass {
    public static void main(String[] args) {
        // part 1
        QueueClass D = new QueueClass();
        for (int i = 1; i <= 8; i++)
            D.enqueue(i);
        System.out.println("Elements in D before using Stack: ");
        D.printQueue();
        Stack<Integer> S = new Stack<Integer>();
        while (!(D.isEmpty()))
            S.push(D.dequeue());
        while (!(S.empty()))
            D.enqueue(S.pop());
        System.out.println("\nElements in D after using Stack: ");
        D.printQueue();
        //part 2
        QueueClass q = new QueueClass();
        System.out.println("\n\nValue returned after performing operations: ");
        q.enqueue(5); q.enqueue(3);
        System.out.print(q.dequeue() + " ");
        q.enqueue(2); q.enqueue(8); 
        System.out.print(q.dequeue() + " ");
        System.out.print(q.dequeue() + " ");
        q.enqueue(9); q.enqueue(1);
        System.out.print(q.dequeue() + " ");
        q.enqueue(7); q.enqueue(6);
        System.out.print(q.dequeue() + " ");
        System.out.print(q.dequeue() + " ");
        q.enqueue(4);
        System.out.print(q.dequeue() + " ");
        System.out.print(q.dequeue() + " ");
        System.out.println("");
    }
}

Output:


Related Solutions

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...
Assignment Purpose The purpose of this lab is to write a well commented java program that...
Assignment Purpose The purpose of this lab is to write a well commented java program that demonstrates the use of one dimensional arrays and methods.(Need Comment, Write by Java Code) Instructions Write a method rotateArray that is passed to an array, x, of integers (minimum 7 numbers) and an integer rotation count, n. x is an array filled with randomly generated integers between 1 and 100. The method creates a new array with the items of x moved forward by...
Assignment Purpose The purpose of this lab is to write a well commented java program that...
Assignment Purpose The purpose of this lab is to write a well commented java program that demonstrates the use and re-use of methods with input validation. Instructions It is quite interesting that most of us are likely to be able to read and comprehend words, even if the alphabets of these words are scrambled (two of them) given the fact that the first and last alphabets remain the same. For example, “I dn'ot gvie a dman for a man taht...
Assignment Purpose The purpose of this lab is to write a well commented java program that...
Assignment Purpose The purpose of this lab is to write a well commented java program that demonstrates the use of one dimensional arrays and methods. Instructions Write a method rotateArray that is passed to an array, x, of integers (minimum 7 numbers) and an integer rotation count, n. x is an array filled with randomly generated integers between 1 and 100. The method creates a new array with the items of x moved forward by n Elements that are rotated...
Assignment Purpose The purpose of this lab is to write a well commented java program that...
Assignment Purpose The purpose of this lab is to write a well commented java program that demonstrates the use of two dimensional arrays, input validation, and methods. (Write by Java Code, Need Comment) Instructions A theater seating chart is implemented as a two-dimensional array of ticket prices, like this: Seat Ticket Price 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10...
Assignment Purpose The purpose of this lab is to write a well commented java program that...
Assignment Purpose The purpose of this lab is to write a well commented java program that demonstrates the use of one dimensional arrays and methods. Instructions Write a method rotateArray that is passed to an array, x, of integers (minimum 7 numbers) and an integer rotation count, n. x is an array filled with randomly generated integers between 1 and 100. The method creates a new array with the items of x moved forward by n Elements that are rotated...
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...
Assignment Purpose The purpose of this lab is to write a well-commented java program that demonstrates...
Assignment Purpose The purpose of this lab is to write a well-commented java program that demonstrates the use of loops, and generation of random integers. Instructions You are taking some time off from your paint business and currently are on vacation in Bahamas. You decide to write a Java program that generates 10 random numbers between 1 and 20 (all integers). You cannot use arrays (even if you know what they are) to store these numbers. It then picks up...
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
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT