SlideShare a Scribd company logo
1 of 10
Download to read offline
Objective: Min-heap queue with customized comparator
Hospital emergency room assign patient priority base on symptom of the patient. Each patient
receives an identity number which prioritizes the order of emergency for the patient. The lower
the number is, the higher the emergency will be.
Use java.util.PriorityQueue to write a java program to create the emergency room registration
database. The patient.txt is the input file for patient record. Download patient.txt, Patient.java to
perform following specifications:
(a)Load all records from the input file.
(b)At the end, print the list in assigned priority order.
Given Code:
//*******************************************************************
// Patient.java
//*******************************************************************
public class Patient
{
private int id;
private String name;
private String emergencyCase;
//----------------------------------------------------------------
// Creates a customer with the specified id number.
//----------------------------------------------------------------
public Patient (int number, String custName,String er )
{
id = number;
name = custName;
emergencyCase = er;
}
//----------------------------------------------------------------
// Returns a string description of this customer.
//----------------------------------------------------------------
public String toString()
{
return "Patient priority id: " + id+" Patient name: "+name+" Symptom: "+emergencyCase;
}
public String getName()
{
return name;
}
public int getId()
{
return id;
}
public String getCase()
{
return emergencyCase;
}
}
/** Source code example for "A Practical Introduction to Data
Structures and Algorithm Analysis, 3rd Edition (Java)"
by Clifford A. Shaffer
Copyright 2008-2011 by Clifford A. Shaffer
*/
import java.util.*;
import java.math.*;
/** A bunch of utility functions. */
class DSutil {
/** Swap two Objects in an array
@param A The array
@param p1 Index of one Object in A
@param p2 Index of another Object A
*/
public static void swap(E[] A, int p1, int p2) {
E temp = A[p1];
A[p1] = A[p2];
A[p2] = temp;
}
/** Randomly permute the Objects in an array.
@param A The array
*/
// int version
// Randomly permute the values of array "A"
static void permute(int[] A) {
for (int i = A.length; i > 0; i--) // for each i
swap(A, i-1, DSutil.random(i)); // swap A[i-1] with
} // a random element
public static void swap(int[] A, int p1, int p2) {
int temp = A[p1];
A[p1] = A[p2];
A[p2] = temp;
}
/** Randomly permute the values in array A */
static void permute(E[] A) {
for (int i = A.length; i > 0; i--) // for each i
swap(A, i-1, DSutil.random(i)); // swap A[i-1] with
} // a random element
/** Initialize the random variable */
static private Random value = new Random(); // Hold the Random class object
/** Create a random number function from the standard Java Random
class. Turn it into a uniformly distributed value within the
range 0 to n-1 by taking the value mod n.
@param n The upper bound for the range.
@return A value in the range 0 to n-1.
*/
static int random(int n) {
return Math.abs(value.nextInt()) % n;
}
}
Text FIle:
10,Sam,Bleeding
02,Carla,Stroke
92,Woody,Flu
11,Diane,High-temperature
32,Norm,Stomach
55,Cliff,Broken-bone
06,Tom,Gun-wounds
22,Kristen,Pregnancy
Solution
Code:
//Include libraries
import java.util.Date;
import java.util.Random;
import java.util.Comparator;
import java.util.PriorityQueue;
//Define class Patient
class Patient
{
//Define variables
protected String name;
protected int lcategory;
protected Date ltimeArrived;
//Define method lgetName()
public String lgetName()
{
return name;
}
//Define method lsetName()
public void lsetName(String lnameIn)
{
this.name = lnameIn;
}
//Define lgetCategory()
public int lgetCategory()
{
return lcategory;
}
//Define lsetCategory()
public void lsetCategory(int lcategoryIn)
{
this.lcategory = lcategoryIn;
}
//Define getTimeArrived()
public java.util.Date getTimeArrived()
{
return ltimeArrived;
}
// Define default constructor
public Patient()
{
this.name = "Default Name"; this.lcategory = 5;
// Place to queue end
this.ltimeArrived = new Date();
}
//Define overloaded constructor
public Patient(String lnameIn, int lcategoryIn)
{
this.name = lnameIn;
this.lcategory = lcategoryIn;
this.ltimeArrived = new Date();
}
}
//Define class PatientComparator
class PatientComparator implements Comparator
{
//Define compare()
public int compare(Patient lp1, Patient lp2)
{
//Compare lcategory
if (lp1.lgetCategory() < lp2.lgetCategory())
return -1;
if (lp1.lgetCategory() > lp2.lgetCategory())
return 1;
else
{
if (lp1.getTimeArrived().before(lp2.getTimeArrived()))
return -1;
if (lp1.getTimeArrived().after(lp2.getTimeArrived()))
return 1;
}
return 0;
}
}
//Define class lPatientQueue
class lPatientQueue
{
PriorityQueue lpq;
//Define constructor
public lPatientQueue()
{
this.lpq = new PriorityQueue(1, new PatientComparator());
}
//Define lregisterPatient()
public void lregisterPatient(Patient p)
{
this.lpq.add(p);
}
//Define lgetNextPatient()
public Patient lgetNextPatient()
{
return (Patient) this.lpq.poll();
}
}
//Define class lEmergency
public class lEmergency
{
//Declare variables
private static final int lWAIT_LIMIT = 3000;
lPatientQueue lpq = new lPatientQueue();
private void lt()
{
try
{
Thread.sleep(new Random().nextInt(lWAIT_LIMIT));
} catch (InterruptedException le)
{
}
}
//Define lpatientArrives()
private void lpatientArrives(Patient p)
{
lpq.lregisterPatient(p);
System.out.println(" ARRIVAL: " + p.lgetName());
System.out.println(" time arrived: " + p.getTimeArrived());
System.out.println(" lcategory: " + p.lgetCategory());
System.out.println("------------------------------------"); lt();
} // end lpatientArrives method
private void ldoctorVisits()
{
Patient p = lpq.lgetNextPatient();
System.out.println(" VISIT: " + p.lgetName());
System.out.println(" time arrived: " + p.getTimeArrived());
System.out.println(" lcategory: " + p.lgetCategory());
System.out.println("------------------------------------");
lt();
} // end ldoctorVisits method
private void lsimulate()
{
System.out.println("------------------------------------");
System.out.println("ER OPEN");
System.out.println("------------------------------------");
lpatientArrives(new Patient("ABC", 3));
lpatientArrives(new Patient("XYZ", 1));
lpatientArrives(new Patient("DFC YUH", 2));
ldoctorVisits();
lpatientArrives(new Patient("QWE TYU", 2));
lpatientArrives(new Patient("Hen Kno", 4));
lpatientArrives(new Patient("Pat Hen", 2));
ldoctorVisits();
ldoctorVisits();
lpatientArrives(new Patient("Mar DrP", 1));
lpatientArrives(new Patient("Sam Adam", 3));
ldoctorVisits();
ldoctorVisits();
ldoctorVisits();
ldoctorVisits();
ldoctorVisits();
System.out.println("------------------------------------");
System.out.println("ER CLOSED");
System.out.println("------------------------------------");
} // end lsimulate method
public static void main(String[] args)
{
lEmergency ler = new lEmergency();
ler.lsimulate();
}
}
Sample Output:
E:Javajdk1.7.0_80bin>javac lEmergency.java
E:Javajdk1.7.0_80bin>java lEmergency
------------------------------------
ER OPEN
------------------------------------
ARRIVAL: ABC
time arrived: Wed Nov 23 10:25:59 PST 2016
lcategory: 3
------------------------------------
ARRIVAL: XYZ
time arrived: Wed Nov 23 10:26:00 PST 2016
lcategory: 1
------------------------------------
ARRIVAL: DFC YUH
time arrived: Wed Nov 23 10:26:02 PST 2016
lcategory: 2
------------------------------------
VISIT: XYZ
time arrived: Wed Nov 23 10:26:00 PST 2016
lcategory: 1
------------------------------------
ARRIVAL: QWE TYU
time arrived: Wed Nov 23 10:26:06 PST 2016
lcategory: 2
------------------------------------
ARRIVAL: Hen Kno
time arrived: Wed Nov 23 10:26:07 PST 2016
lcategory: 4
------------------------------------
ARRIVAL: Pat Hen
time arrived: Wed Nov 23 10:26:09 PST 2016
lcategory: 2
------------------------------------
VISIT: DFC YUH
time arrived: Wed Nov 23 10:26:02 PST 2016
lcategory: 2
------------------------------------
VISIT: QWE TYU
time arrived: Wed Nov 23 10:26:06 PST 2016
lcategory: 2
------------------------------------
ARRIVAL: Mar DrP
time arrived: Wed Nov 23 10:26:12 PST 2016
lcategory: 1
------------------------------------
ARRIVAL: Sam Adam
time arrived: Wed Nov 23 10:26:14 PST 2016
lcategory: 3
------------------------------------
VISIT: Mar DrP
time arrived: Wed Nov 23 10:26:12 PST 2016
lcategory: 1
------------------------------------
VISIT: Pat Hen
time arrived: Wed Nov 23 10:26:09 PST 2016
lcategory: 2
------------------------------------
VISIT: ABC
time arrived: Wed Nov 23 10:25:59 PST 2016
lcategory: 3
------------------------------------
VISIT: Sam Adam
time arrived: Wed Nov 23 10:26:14 PST 2016
lcategory: 3
------------------------------------
VISIT: Hen Kno
time arrived: Wed Nov 23 10:26:07 PST 2016
lcategory: 4
------------------------------------
------------------------------------
ER CLOSED
------------------------------------
E:Javajdk1.7.0_80bin>

More Related Content

Similar to Objective Min-heap queue with customized comparatorHospital emerg.pdf

Binary patching for fun and profit @ JUG.ru, 25.02.2012
Binary patching for fun and profit @ JUG.ru, 25.02.2012Binary patching for fun and profit @ JUG.ru, 25.02.2012
Binary patching for fun and profit @ JUG.ru, 25.02.2012
Anton Arhipov
 
In java , I want you to implement a Data Structure known as a Doubly.pdf
In java , I want you to implement a Data Structure known as a Doubly.pdfIn java , I want you to implement a Data Structure known as a Doubly.pdf
In java , I want you to implement a Data Structure known as a Doubly.pdf
aromalcom
 
Java fundamentals
Java fundamentalsJava fundamentals
Java fundamentals
HCMUTE
 
EmptyCollectionException-java -- - Represents the situation in which.docx
EmptyCollectionException-java --  - Represents the situation in which.docxEmptyCollectionException-java --  - Represents the situation in which.docx
EmptyCollectionException-java -- - Represents the situation in which.docx
BlakeSGMHemmingss
 
java compilerCompiler1.javajava compilerCompiler1.javaimport.docx
java compilerCompiler1.javajava compilerCompiler1.javaimport.docxjava compilerCompiler1.javajava compilerCompiler1.javaimport.docx
java compilerCompiler1.javajava compilerCompiler1.javaimport.docx
priestmanmable
 
----------Evaluator-java---------------- package evaluator- import j.docx
----------Evaluator-java---------------- package evaluator-   import j.docx----------Evaluator-java---------------- package evaluator-   import j.docx
----------Evaluator-java---------------- package evaluator- import j.docx
janettjz6sfehrle
 
java write a program to evaluate the postfix expressionthe program.pdf
java write a program to evaluate the postfix expressionthe program.pdfjava write a program to evaluate the postfix expressionthe program.pdf
java write a program to evaluate the postfix expressionthe program.pdf
arjuntelecom26
 
New folderjsjfArrayStack.classpackage jsjf;publicsynchronize.docx
New folderjsjfArrayStack.classpackage jsjf;publicsynchronize.docxNew folderjsjfArrayStack.classpackage jsjf;publicsynchronize.docx
New folderjsjfArrayStack.classpackage jsjf;publicsynchronize.docx
curwenmichaela
 
Using NetBeansImplement a queue named QueueLL using a Linked List .pdf
Using NetBeansImplement a queue named QueueLL using a Linked List .pdfUsing NetBeansImplement a queue named QueueLL using a Linked List .pdf
Using NetBeansImplement a queue named QueueLL using a Linked List .pdf
siennatimbok52331
 
In this lab, we will write an application to store a deck of cards i.pdf
In this lab, we will write an application to store a deck of cards i.pdfIn this lab, we will write an application to store a deck of cards i.pdf
In this lab, we will write an application to store a deck of cards i.pdf
contact41
 
JAVA OOP project; desperately need help asap im begging.Been stuck.pdf
JAVA OOP project; desperately need help asap im begging.Been stuck.pdfJAVA OOP project; desperately need help asap im begging.Been stuck.pdf
JAVA OOP project; desperately need help asap im begging.Been stuck.pdf
fantasiatheoutofthef
 

Similar to Objective Min-heap queue with customized comparatorHospital emerg.pdf (20)

MT_01_unittest_python.pdf
MT_01_unittest_python.pdfMT_01_unittest_python.pdf
MT_01_unittest_python.pdf
 
Java 5 Features
Java 5 FeaturesJava 5 Features
Java 5 Features
 
Decoding Kotlin - Your guide to solving the mysterious in Kotlin.pptx
Decoding Kotlin - Your guide to solving the mysterious in Kotlin.pptxDecoding Kotlin - Your guide to solving the mysterious in Kotlin.pptx
Decoding Kotlin - Your guide to solving the mysterious in Kotlin.pptx
 
Ad java prac sol set
Ad java prac sol setAd java prac sol set
Ad java prac sol set
 
Binary patching for fun and profit @ JUG.ru, 25.02.2012
Binary patching for fun and profit @ JUG.ru, 25.02.2012Binary patching for fun and profit @ JUG.ru, 25.02.2012
Binary patching for fun and profit @ JUG.ru, 25.02.2012
 
srgoc
srgocsrgoc
srgoc
 
In java , I want you to implement a Data Structure known as a Doubly.pdf
In java , I want you to implement a Data Structure known as a Doubly.pdfIn java , I want you to implement a Data Structure known as a Doubly.pdf
In java , I want you to implement a Data Structure known as a Doubly.pdf
 
Java fundamentals
Java fundamentalsJava fundamentals
Java fundamentals
 
EmptyCollectionException-java -- - Represents the situation in which.docx
EmptyCollectionException-java --  - Represents the situation in which.docxEmptyCollectionException-java --  - Represents the situation in which.docx
EmptyCollectionException-java -- - Represents the situation in which.docx
 
java compilerCompiler1.javajava compilerCompiler1.javaimport.docx
java compilerCompiler1.javajava compilerCompiler1.javaimport.docxjava compilerCompiler1.javajava compilerCompiler1.javaimport.docx
java compilerCompiler1.javajava compilerCompiler1.javaimport.docx
 
----------Evaluator-java---------------- package evaluator- import j.docx
----------Evaluator-java---------------- package evaluator-   import j.docx----------Evaluator-java---------------- package evaluator-   import j.docx
----------Evaluator-java---------------- package evaluator- import j.docx
 
java write a program to evaluate the postfix expressionthe program.pdf
java write a program to evaluate the postfix expressionthe program.pdfjava write a program to evaluate the postfix expressionthe program.pdf
java write a program to evaluate the postfix expressionthe program.pdf
 
07-Basic-Input-Output.ppt
07-Basic-Input-Output.ppt07-Basic-Input-Output.ppt
07-Basic-Input-Output.ppt
 
New folderjsjfArrayStack.classpackage jsjf;publicsynchronize.docx
New folderjsjfArrayStack.classpackage jsjf;publicsynchronize.docxNew folderjsjfArrayStack.classpackage jsjf;publicsynchronize.docx
New folderjsjfArrayStack.classpackage jsjf;publicsynchronize.docx
 
Using Array Approach, Linked List approach, and Delete Byte Approach.pdf
Using Array Approach, Linked List approach, and Delete Byte Approach.pdfUsing Array Approach, Linked List approach, and Delete Byte Approach.pdf
Using Array Approach, Linked List approach, and Delete Byte Approach.pdf
 
Advance Java Programs skeleton
Advance Java Programs skeletonAdvance Java Programs skeleton
Advance Java Programs skeleton
 
Using NetBeansImplement a queue named QueueLL using a Linked List .pdf
Using NetBeansImplement a queue named QueueLL using a Linked List .pdfUsing NetBeansImplement a queue named QueueLL using a Linked List .pdf
Using NetBeansImplement a queue named QueueLL using a Linked List .pdf
 
JAVA Question : Programming Assignment
JAVA Question : Programming AssignmentJAVA Question : Programming Assignment
JAVA Question : Programming Assignment
 
In this lab, we will write an application to store a deck of cards i.pdf
In this lab, we will write an application to store a deck of cards i.pdfIn this lab, we will write an application to store a deck of cards i.pdf
In this lab, we will write an application to store a deck of cards i.pdf
 
JAVA OOP project; desperately need help asap im begging.Been stuck.pdf
JAVA OOP project; desperately need help asap im begging.Been stuck.pdfJAVA OOP project; desperately need help asap im begging.Been stuck.pdf
JAVA OOP project; desperately need help asap im begging.Been stuck.pdf
 

More from ezhilvizhiyan

Hi please complete the following with detailed working out Find the .pdf
Hi please complete the following with detailed working out Find the .pdfHi please complete the following with detailed working out Find the .pdf
Hi please complete the following with detailed working out Find the .pdf
ezhilvizhiyan
 
Explain and discuss 4G wireless communications and their advantages..pdf
Explain and discuss 4G wireless communications and their advantages..pdfExplain and discuss 4G wireless communications and their advantages..pdf
Explain and discuss 4G wireless communications and their advantages..pdf
ezhilvizhiyan
 
Draw and describe a module of the cross-cultural communication pr.pdf
Draw and describe a module of the cross-cultural communication pr.pdfDraw and describe a module of the cross-cultural communication pr.pdf
Draw and describe a module of the cross-cultural communication pr.pdf
ezhilvizhiyan
 
Describe the original purpose of the Clean Air Act Policy of 1963. E.pdf
Describe the original purpose of the Clean Air Act Policy of 1963. E.pdfDescribe the original purpose of the Clean Air Act Policy of 1963. E.pdf
Describe the original purpose of the Clean Air Act Policy of 1963. E.pdf
ezhilvizhiyan
 
7. In many respects metal binding is similar to the binding of a prot.pdf
7. In many respects metal binding is similar to the binding of a prot.pdf7. In many respects metal binding is similar to the binding of a prot.pdf
7. In many respects metal binding is similar to the binding of a prot.pdf
ezhilvizhiyan
 
3. Photosynthetic organisms produce about 300 x 1015 g of oxygen per .pdf
3. Photosynthetic organisms produce about 300 x 1015 g of oxygen per .pdf3. Photosynthetic organisms produce about 300 x 1015 g of oxygen per .pdf
3. Photosynthetic organisms produce about 300 x 1015 g of oxygen per .pdf
ezhilvizhiyan
 
This project will implement a simple usernamepassword lookup system.pdf
This project will implement a simple usernamepassword lookup system.pdfThis project will implement a simple usernamepassword lookup system.pdf
This project will implement a simple usernamepassword lookup system.pdf
ezhilvizhiyan
 
The following code is based on the Josephus problem, the code does c.pdf
The following code is based on the Josephus problem, the code does c.pdfThe following code is based on the Josephus problem, the code does c.pdf
The following code is based on the Josephus problem, the code does c.pdf
ezhilvizhiyan
 
Tech transfers should the fed Gov’t keep subsidizing University .pdf
Tech transfers should the fed Gov’t keep subsidizing University .pdfTech transfers should the fed Gov’t keep subsidizing University .pdf
Tech transfers should the fed Gov’t keep subsidizing University .pdf
ezhilvizhiyan
 
Remaining Time 31 minutes, 22 seconds QUESTION 1 Question Completion.pdf
Remaining Time 31 minutes, 22 seconds QUESTION 1 Question Completion.pdfRemaining Time 31 minutes, 22 seconds QUESTION 1 Question Completion.pdf
Remaining Time 31 minutes, 22 seconds QUESTION 1 Question Completion.pdf
ezhilvizhiyan
 

More from ezhilvizhiyan (20)

Hi please complete the following with detailed working out Find the .pdf
Hi please complete the following with detailed working out Find the .pdfHi please complete the following with detailed working out Find the .pdf
Hi please complete the following with detailed working out Find the .pdf
 
Explain and discuss 4G wireless communications and their advantages..pdf
Explain and discuss 4G wireless communications and their advantages..pdfExplain and discuss 4G wireless communications and their advantages..pdf
Explain and discuss 4G wireless communications and their advantages..pdf
 
Draw and describe a module of the cross-cultural communication pr.pdf
Draw and describe a module of the cross-cultural communication pr.pdfDraw and describe a module of the cross-cultural communication pr.pdf
Draw and describe a module of the cross-cultural communication pr.pdf
 
Discuss the reasons why visions fail. What steps can be implemented .pdf
Discuss the reasons why visions fail. What steps can be implemented .pdfDiscuss the reasons why visions fail. What steps can be implemented .pdf
Discuss the reasons why visions fail. What steps can be implemented .pdf
 
Describe the original purpose of the Clean Air Act Policy of 1963. E.pdf
Describe the original purpose of the Clean Air Act Policy of 1963. E.pdfDescribe the original purpose of the Clean Air Act Policy of 1963. E.pdf
Describe the original purpose of the Clean Air Act Policy of 1963. E.pdf
 
Continuity 100 Let f be a continuous function on a metric space X. L.pdf
Continuity 100 Let f be a continuous function on a metric space X. L.pdfContinuity 100 Let f be a continuous function on a metric space X. L.pdf
Continuity 100 Let f be a continuous function on a metric space X. L.pdf
 
Admitting New Partner With Bonus Cody Jenkins and Lacey Tanner formed.pdf
Admitting New Partner With Bonus Cody Jenkins and Lacey Tanner formed.pdfAdmitting New Partner With Bonus Cody Jenkins and Lacey Tanner formed.pdf
Admitting New Partner With Bonus Cody Jenkins and Lacey Tanner formed.pdf
 
7. In many respects metal binding is similar to the binding of a prot.pdf
7. In many respects metal binding is similar to the binding of a prot.pdf7. In many respects metal binding is similar to the binding of a prot.pdf
7. In many respects metal binding is similar to the binding of a prot.pdf
 
3. Photosynthetic organisms produce about 300 x 1015 g of oxygen per .pdf
3. Photosynthetic organisms produce about 300 x 1015 g of oxygen per .pdf3. Photosynthetic organisms produce about 300 x 1015 g of oxygen per .pdf
3. Photosynthetic organisms produce about 300 x 1015 g of oxygen per .pdf
 
1. Which is less soluble in water, 1-pentanol or 1-heptanol Explain..pdf
1. Which is less soluble in water, 1-pentanol or 1-heptanol Explain..pdf1. Which is less soluble in water, 1-pentanol or 1-heptanol Explain..pdf
1. Which is less soluble in water, 1-pentanol or 1-heptanol Explain..pdf
 
X Company has the following budgeted cash flows for JanuaryIf the .pdf
X Company has the following budgeted cash flows for JanuaryIf the .pdfX Company has the following budgeted cash flows for JanuaryIf the .pdf
X Company has the following budgeted cash flows for JanuaryIf the .pdf
 
Why did animal phyla appear so suddenly during the Cambrian explosion.pdf
Why did animal phyla appear so suddenly during the Cambrian explosion.pdfWhy did animal phyla appear so suddenly during the Cambrian explosion.pdf
Why did animal phyla appear so suddenly during the Cambrian explosion.pdf
 
Which of the following information-management systems uses artificia.pdf
Which of the following information-management systems uses artificia.pdfWhich of the following information-management systems uses artificia.pdf
Which of the following information-management systems uses artificia.pdf
 
Which of the following was part of the reason for the European excha.pdf
Which of the following was part of the reason for the European excha.pdfWhich of the following was part of the reason for the European excha.pdf
Which of the following was part of the reason for the European excha.pdf
 
Ture or false or uncertain ( explain why) Thank you! e. Output per c.pdf
Ture or false or uncertain ( explain why) Thank you! e. Output per c.pdfTure or false or uncertain ( explain why) Thank you! e. Output per c.pdf
Ture or false or uncertain ( explain why) Thank you! e. Output per c.pdf
 
This is for a C programDene a Car structure type in your header le.pdf
This is for a C programDene a Car structure type in your header le.pdfThis is for a C programDene a Car structure type in your header le.pdf
This is for a C programDene a Car structure type in your header le.pdf
 
This project will implement a simple usernamepassword lookup system.pdf
This project will implement a simple usernamepassword lookup system.pdfThis project will implement a simple usernamepassword lookup system.pdf
This project will implement a simple usernamepassword lookup system.pdf
 
The following code is based on the Josephus problem, the code does c.pdf
The following code is based on the Josephus problem, the code does c.pdfThe following code is based on the Josephus problem, the code does c.pdf
The following code is based on the Josephus problem, the code does c.pdf
 
Tech transfers should the fed Gov’t keep subsidizing University .pdf
Tech transfers should the fed Gov’t keep subsidizing University .pdfTech transfers should the fed Gov’t keep subsidizing University .pdf
Tech transfers should the fed Gov’t keep subsidizing University .pdf
 
Remaining Time 31 minutes, 22 seconds QUESTION 1 Question Completion.pdf
Remaining Time 31 minutes, 22 seconds QUESTION 1 Question Completion.pdfRemaining Time 31 minutes, 22 seconds QUESTION 1 Question Completion.pdf
Remaining Time 31 minutes, 22 seconds QUESTION 1 Question Completion.pdf
 

Recently uploaded

The basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptxThe basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptx
heathfieldcps1
 
1029-Danh muc Sach Giao Khoa khoi 6.pdf
1029-Danh muc Sach Giao Khoa khoi  6.pdf1029-Danh muc Sach Giao Khoa khoi  6.pdf
1029-Danh muc Sach Giao Khoa khoi 6.pdf
QucHHunhnh
 

Recently uploaded (20)

Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17
 
ICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptxICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptx
 
microwave assisted reaction. General introduction
microwave assisted reaction. General introductionmicrowave assisted reaction. General introduction
microwave assisted reaction. General introduction
 
Unit-IV- Pharma. Marketing Channels.pptx
Unit-IV- Pharma. Marketing Channels.pptxUnit-IV- Pharma. Marketing Channels.pptx
Unit-IV- Pharma. Marketing Channels.pptx
 
General Principles of Intellectual Property: Concepts of Intellectual Proper...
General Principles of Intellectual Property: Concepts of Intellectual  Proper...General Principles of Intellectual Property: Concepts of Intellectual  Proper...
General Principles of Intellectual Property: Concepts of Intellectual Proper...
 
PROCESS RECORDING FORMAT.docx
PROCESS      RECORDING        FORMAT.docxPROCESS      RECORDING        FORMAT.docx
PROCESS RECORDING FORMAT.docx
 
Micro-Scholarship, What it is, How can it help me.pdf
Micro-Scholarship, What it is, How can it help me.pdfMicro-Scholarship, What it is, How can it help me.pdf
Micro-Scholarship, What it is, How can it help me.pdf
 
The basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptxThe basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptx
 
Energy Resources. ( B. Pharmacy, 1st Year, Sem-II) Natural Resources
Energy Resources. ( B. Pharmacy, 1st Year, Sem-II) Natural ResourcesEnergy Resources. ( B. Pharmacy, 1st Year, Sem-II) Natural Resources
Energy Resources. ( B. Pharmacy, 1st Year, Sem-II) Natural Resources
 
Mixin Classes in Odoo 17 How to Extend Models Using Mixin Classes
Mixin Classes in Odoo 17  How to Extend Models Using Mixin ClassesMixin Classes in Odoo 17  How to Extend Models Using Mixin Classes
Mixin Classes in Odoo 17 How to Extend Models Using Mixin Classes
 
Ecological Succession. ( ECOSYSTEM, B. Pharmacy, 1st Year, Sem-II, Environmen...
Ecological Succession. ( ECOSYSTEM, B. Pharmacy, 1st Year, Sem-II, Environmen...Ecological Succession. ( ECOSYSTEM, B. Pharmacy, 1st Year, Sem-II, Environmen...
Ecological Succession. ( ECOSYSTEM, B. Pharmacy, 1st Year, Sem-II, Environmen...
 
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
 
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
 
Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104
 
Z Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot GraphZ Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot Graph
 
1029-Danh muc Sach Giao Khoa khoi 6.pdf
1029-Danh muc Sach Giao Khoa khoi  6.pdf1029-Danh muc Sach Giao Khoa khoi  6.pdf
1029-Danh muc Sach Giao Khoa khoi 6.pdf
 
Unit-V; Pricing (Pharma Marketing Management).pptx
Unit-V; Pricing (Pharma Marketing Management).pptxUnit-V; Pricing (Pharma Marketing Management).pptx
Unit-V; Pricing (Pharma Marketing Management).pptx
 
Holdier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdfHoldier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdf
 
Unit-IV; Professional Sales Representative (PSR).pptx
Unit-IV; Professional Sales Representative (PSR).pptxUnit-IV; Professional Sales Representative (PSR).pptx
Unit-IV; Professional Sales Representative (PSR).pptx
 
Introduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The BasicsIntroduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The Basics
 

Objective Min-heap queue with customized comparatorHospital emerg.pdf

  • 1. Objective: Min-heap queue with customized comparator Hospital emergency room assign patient priority base on symptom of the patient. Each patient receives an identity number which prioritizes the order of emergency for the patient. The lower the number is, the higher the emergency will be. Use java.util.PriorityQueue to write a java program to create the emergency room registration database. The patient.txt is the input file for patient record. Download patient.txt, Patient.java to perform following specifications: (a)Load all records from the input file. (b)At the end, print the list in assigned priority order. Given Code: //******************************************************************* // Patient.java //******************************************************************* public class Patient { private int id; private String name; private String emergencyCase; //---------------------------------------------------------------- // Creates a customer with the specified id number. //---------------------------------------------------------------- public Patient (int number, String custName,String er ) { id = number; name = custName; emergencyCase = er; } //---------------------------------------------------------------- // Returns a string description of this customer. //---------------------------------------------------------------- public String toString() { return "Patient priority id: " + id+" Patient name: "+name+" Symptom: "+emergencyCase; }
  • 2. public String getName() { return name; } public int getId() { return id; } public String getCase() { return emergencyCase; } } /** Source code example for "A Practical Introduction to Data Structures and Algorithm Analysis, 3rd Edition (Java)" by Clifford A. Shaffer Copyright 2008-2011 by Clifford A. Shaffer */ import java.util.*; import java.math.*; /** A bunch of utility functions. */ class DSutil { /** Swap two Objects in an array @param A The array @param p1 Index of one Object in A @param p2 Index of another Object A */ public static void swap(E[] A, int p1, int p2) { E temp = A[p1]; A[p1] = A[p2]; A[p2] = temp; } /** Randomly permute the Objects in an array. @param A The array
  • 3. */ // int version // Randomly permute the values of array "A" static void permute(int[] A) { for (int i = A.length; i > 0; i--) // for each i swap(A, i-1, DSutil.random(i)); // swap A[i-1] with } // a random element public static void swap(int[] A, int p1, int p2) { int temp = A[p1]; A[p1] = A[p2]; A[p2] = temp; } /** Randomly permute the values in array A */ static void permute(E[] A) { for (int i = A.length; i > 0; i--) // for each i swap(A, i-1, DSutil.random(i)); // swap A[i-1] with } // a random element /** Initialize the random variable */ static private Random value = new Random(); // Hold the Random class object /** Create a random number function from the standard Java Random class. Turn it into a uniformly distributed value within the range 0 to n-1 by taking the value mod n. @param n The upper bound for the range. @return A value in the range 0 to n-1. */ static int random(int n) { return Math.abs(value.nextInt()) % n; } } Text FIle: 10,Sam,Bleeding 02,Carla,Stroke 92,Woody,Flu 11,Diane,High-temperature 32,Norm,Stomach 55,Cliff,Broken-bone
  • 4. 06,Tom,Gun-wounds 22,Kristen,Pregnancy Solution Code: //Include libraries import java.util.Date; import java.util.Random; import java.util.Comparator; import java.util.PriorityQueue; //Define class Patient class Patient { //Define variables protected String name; protected int lcategory; protected Date ltimeArrived; //Define method lgetName() public String lgetName() { return name; } //Define method lsetName() public void lsetName(String lnameIn) { this.name = lnameIn; } //Define lgetCategory() public int lgetCategory() { return lcategory; } //Define lsetCategory() public void lsetCategory(int lcategoryIn) {
  • 5. this.lcategory = lcategoryIn; } //Define getTimeArrived() public java.util.Date getTimeArrived() { return ltimeArrived; } // Define default constructor public Patient() { this.name = "Default Name"; this.lcategory = 5; // Place to queue end this.ltimeArrived = new Date(); } //Define overloaded constructor public Patient(String lnameIn, int lcategoryIn) { this.name = lnameIn; this.lcategory = lcategoryIn; this.ltimeArrived = new Date(); } } //Define class PatientComparator class PatientComparator implements Comparator { //Define compare() public int compare(Patient lp1, Patient lp2) { //Compare lcategory if (lp1.lgetCategory() < lp2.lgetCategory()) return -1; if (lp1.lgetCategory() > lp2.lgetCategory()) return 1; else {
  • 6. if (lp1.getTimeArrived().before(lp2.getTimeArrived())) return -1; if (lp1.getTimeArrived().after(lp2.getTimeArrived())) return 1; } return 0; } } //Define class lPatientQueue class lPatientQueue { PriorityQueue lpq; //Define constructor public lPatientQueue() { this.lpq = new PriorityQueue(1, new PatientComparator()); } //Define lregisterPatient() public void lregisterPatient(Patient p) { this.lpq.add(p); } //Define lgetNextPatient() public Patient lgetNextPatient() { return (Patient) this.lpq.poll(); } } //Define class lEmergency public class lEmergency { //Declare variables private static final int lWAIT_LIMIT = 3000; lPatientQueue lpq = new lPatientQueue();
  • 7. private void lt() { try { Thread.sleep(new Random().nextInt(lWAIT_LIMIT)); } catch (InterruptedException le) { } } //Define lpatientArrives() private void lpatientArrives(Patient p) { lpq.lregisterPatient(p); System.out.println(" ARRIVAL: " + p.lgetName()); System.out.println(" time arrived: " + p.getTimeArrived()); System.out.println(" lcategory: " + p.lgetCategory()); System.out.println("------------------------------------"); lt(); } // end lpatientArrives method private void ldoctorVisits() { Patient p = lpq.lgetNextPatient(); System.out.println(" VISIT: " + p.lgetName()); System.out.println(" time arrived: " + p.getTimeArrived()); System.out.println(" lcategory: " + p.lgetCategory()); System.out.println("------------------------------------"); lt(); } // end ldoctorVisits method private void lsimulate() { System.out.println("------------------------------------"); System.out.println("ER OPEN"); System.out.println("------------------------------------"); lpatientArrives(new Patient("ABC", 3)); lpatientArrives(new Patient("XYZ", 1)); lpatientArrives(new Patient("DFC YUH", 2)); ldoctorVisits();
  • 8. lpatientArrives(new Patient("QWE TYU", 2)); lpatientArrives(new Patient("Hen Kno", 4)); lpatientArrives(new Patient("Pat Hen", 2)); ldoctorVisits(); ldoctorVisits(); lpatientArrives(new Patient("Mar DrP", 1)); lpatientArrives(new Patient("Sam Adam", 3)); ldoctorVisits(); ldoctorVisits(); ldoctorVisits(); ldoctorVisits(); ldoctorVisits(); System.out.println("------------------------------------"); System.out.println("ER CLOSED"); System.out.println("------------------------------------"); } // end lsimulate method public static void main(String[] args) { lEmergency ler = new lEmergency(); ler.lsimulate(); } } Sample Output: E:Javajdk1.7.0_80bin>javac lEmergency.java E:Javajdk1.7.0_80bin>java lEmergency ------------------------------------ ER OPEN ------------------------------------ ARRIVAL: ABC time arrived: Wed Nov 23 10:25:59 PST 2016 lcategory: 3 ------------------------------------ ARRIVAL: XYZ time arrived: Wed Nov 23 10:26:00 PST 2016 lcategory: 1 ------------------------------------
  • 9. ARRIVAL: DFC YUH time arrived: Wed Nov 23 10:26:02 PST 2016 lcategory: 2 ------------------------------------ VISIT: XYZ time arrived: Wed Nov 23 10:26:00 PST 2016 lcategory: 1 ------------------------------------ ARRIVAL: QWE TYU time arrived: Wed Nov 23 10:26:06 PST 2016 lcategory: 2 ------------------------------------ ARRIVAL: Hen Kno time arrived: Wed Nov 23 10:26:07 PST 2016 lcategory: 4 ------------------------------------ ARRIVAL: Pat Hen time arrived: Wed Nov 23 10:26:09 PST 2016 lcategory: 2 ------------------------------------ VISIT: DFC YUH time arrived: Wed Nov 23 10:26:02 PST 2016 lcategory: 2 ------------------------------------ VISIT: QWE TYU time arrived: Wed Nov 23 10:26:06 PST 2016 lcategory: 2 ------------------------------------ ARRIVAL: Mar DrP time arrived: Wed Nov 23 10:26:12 PST 2016 lcategory: 1 ------------------------------------ ARRIVAL: Sam Adam time arrived: Wed Nov 23 10:26:14 PST 2016 lcategory: 3 ------------------------------------
  • 10. VISIT: Mar DrP time arrived: Wed Nov 23 10:26:12 PST 2016 lcategory: 1 ------------------------------------ VISIT: Pat Hen time arrived: Wed Nov 23 10:26:09 PST 2016 lcategory: 2 ------------------------------------ VISIT: ABC time arrived: Wed Nov 23 10:25:59 PST 2016 lcategory: 3 ------------------------------------ VISIT: Sam Adam time arrived: Wed Nov 23 10:26:14 PST 2016 lcategory: 3 ------------------------------------ VISIT: Hen Kno time arrived: Wed Nov 23 10:26:07 PST 2016 lcategory: 4 ------------------------------------ ------------------------------------ ER CLOSED ------------------------------------ E:Javajdk1.7.0_80bin>