Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

there are 10 patients P1,P2...P10 are waiting to see a doctor in Rabigh General

ID: 3583466 • Letter: T

Question

there are 10 patients P1,P2...P10 are waiting to see a doctor in Rabigh General Hospital. there are five seats available for the patients to wait in queue. P1 comes first followed by P2 then P3... and the last person is P10.

Write a single JAVA program that will implement the queue of the patients.[ Either Array or Linked list can be used for the queue]

Hints

Create a queue of 5 seats

place first 5 patients in the queue

print content of the queue

Remove from patient

Add another patient

print content of the queue

Repeat removing and adding patient until P10 is added and removed

Explanation / Answer

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


public class Patient {

   public static void main(String a[]){
       Queue<String> patient=new LinkedList<String>();
      
       //add 5 patients
       patient.add("P1");
       patient.add("P2");
       patient.add("P3");
       patient.add("P4");
       patient.add("P5");
      
       //print queue
       System.out.println(patient);
      
       //remove 1 patient from the front and add new patient at the end of queue
       //upto P10
       for(int i=1;i<=5;i++){
           System.out.println("P"+i+" is removed and P"+(5+i)+" is added");
           patient.remove();
           patient.add("P"+(i+5));
          
           System.out.println(patient);
       }
      
       /*
       * no more patients to add now. so that remove patients one by one
       * in the order of their arrival.
       */
       for(int i=6;i<=10;i++){
           patient.remove();
           System.out.println("P"+i+" is removed");
           System.out.println(patient);
       }
   }
}

Output:

[P1, P2, P3, P4, P5]
P1 is removed and P6 is added
[P2, P3, P4, P5, P6]
P2 is removed and P7 is added
[P3, P4, P5, P6, P7]
P3 is removed and P8 is added
[P4, P5, P6, P7, P8]
P4 is removed and P9 is added
[P5, P6, P7, P8, P9]
P5 is removed and P10 is added
[P6, P7, P8, P9, P10]
P6 is removed
[P7, P8, P9, P10]
P7 is removed
[P8, P9, P10]
P8 is removed
[P9, P10]
P9 is removed
[P10]
P10 is removed
[]