java code with screenshots Suppose you want to write a program to manage the ord
ID: 3734879 • Letter: J
Question
java code with screenshots
Suppose you want to write a program to manage the order of patients in a clinic, each patient has a name and serial number and he/she waits in a waiting room until he see his serial number on a screen.
A. Write a class of patient with two variables name and serial number. The constructor has two parameters to initialize name and serial number variables. This class has a method to display the patient name and his serial number.
B. Write a linked list class as a queue of patients, which has a method to display the patients’ names and serial numbers, and after serving any patient it displays the next patient name and number.
Explanation / Answer
Please find my implementation.
Please rate my answer:
class Patient {
private String name;
private int serialNumber;
public Patient(String name, int serialNumber) {
this.name = name;
this.serialNumber = serialNumber;
}
public String getName() {
return name;
}
public int getSerialNumber() {
return serialNumber;
}
public void setName(String name) {
this.name = name;
}
public void setSerialNumber(int serialNumber) {
this.serialNumber = serialNumber;
}
@Override
public String toString() {
return serialNumber+". "+name;
}
}
public class PatientQueue{
class QNode
{
Patient patient;
QNode next;
// constructor to create a new linked list node
public QNode(Patient patient) {
this.patient = patient;
this.next = null;
}
}
QNode front, rear;
public PatientQueue() {
this.front = this.rear = null;
}
// Method to add an key to the queue.
public void enqueue(Patient key)
{
// Create a new LL node
QNode temp = new QNode(key);
// If queue is empty, then new node is front and rear both
if (this.rear == null)
{
this.front = this.rear = temp;
return;
}
// Add the new node at the end of queue and change rear
this.rear.next = temp;
this.rear = temp;
}
// Method to remove an key from queue.
public Patient dequeue()
{
// If queue is empty, return NULL.
if (this.front == null)
return null;
// Store previous front and move front one node ahead
QNode temp = this.front;
this.front = this.front.next;
// If front becomes NULL, then change rear also as NULL
if (this.front == null)
this.rear = null;
return temp.patient;
}
public static void main(String[] args)
{
PatientQueue q=new PatientQueue();
q.enqueue(new Patient("Pravesh", 1));
q.enqueue(new Patient("Mukesh", 2));
System.out.println("Prseent serving patient: "+q.dequeue());
System.out.println("Prseent serving patient: "+q.dequeue());
q.enqueue(new Patient("RAhul", 3));
q.enqueue(new Patient("Diler", 4));
q.enqueue(new Patient("Prity", 5));
}
}
/*
* Sample run:
Prseent serving patient: 1. Pravesh
Prseent serving patient: 2. Mukesh
*/
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.