Question

Suppose there is a hospital with a waiting room filled with several patients. Patients are prioritized...

Suppose there is a hospital with a waiting room filled with several patients. Patients are prioritized based on how severe their casualities are. For instance, car crash andgun shot patients are priority level 1, so they go straight to the front of the queue. Patients with heavy chest pain or mild heart attacks are priority level 2, and so on.
Create a hospital that manages patients in a list. Implement it using the LinkedList API. (You may import ArrayList or List, but please do not import any other packages.) See Base_A11Q2.java for a starting place. In particular, see the HospitalADT interface which contains a description of the methods required in the to do list. Create a Patient class that implements Comparable and contains the name of the patient, the casualty of the patient, and the priority level. Make getters and a compareTo method that passes in another Patient and compares the priority levels of the patients. This method should compare the priority levels of the patients and return -1 if the first patient has a lower priority level than the second patient, 0 if the priority levels of the patients are equal, and 1 if the patient has a higher priority level than the second patient.

All patients are added into the hospital list. The highest priority task in a to do list is the task with the lowest priority level. When removing patients, your hospital list should return the lowest priority patient. Patients should be removed from the queue when a doctor lets each patient in for examination.

The attached file (Base_HW11Q2.java) already contains a driver that creates three Patient objects, adds them to a hospital list, and removes/displays them one by one. [25 points]

Sample Output:HW11 Output

import java.util.LinkedList;

public class Base_HW11Q2 {

    static class Patient implements Comparable<Patient> {
        //TODO: complete class
    }

    static interface HospitalADT {

        /**
         * Adds one patient to the hospital.
         * @param p  the patient to be added to the hospital.
         */
        public void add(Patient p);

        /**
         * Removes and returns the highest priority patient in the hospital.
         * @return the highest priority patient in the hospital.
         */
        public Patient remove();

        /**
         * Returns true if this hospital contains no patients.
         * @return true if this hospital is empty.
         */
        public boolean isEmpty();

        /**
         * Returns the number of patients in this hospital.
         * @return the integer representation of the size of the hospital.
         */
        public int size();
    }

    static class Hospital implements HospitalADT {
        //TODO: complete class
    }

    public static void main(String[] args) {
        Hospital hospital = new Hospital();
        Patient p1 = new Patient("Martha Reynolds", "heavy chest pain", 2);
        Patient p2 = new Patient("Dennis Johnson", "ear ache", 3);
        Patient p3 = new Patient("John Smith", "car crash", 1);
        hospital.add(p1);
        hospital.add(p2);
        hospital.add(p3);
        System.out.println(hospital.remove());
        System.out.println(hospital.remove());
        System.out.println(hospital.remove());
    }
}
1 0
Add a comment Improve this question Transcribed image text
Answer #1


import java.util.ArrayList;

public class Base_HW11Q2 {

  
  

    static class Patient implements Comparable<Patient> {
       String name;
       String casualty;
       int level;
      
       public Patient(String name,String casualty, int level ) {
           this.name = name;
           this.casualty = casualty;
           this.level =level;
           // TODO Auto-generated constructor stub
       }

       @Override
       public int compareTo(Patient arg0) {
           // TODO Auto-generated method stub
           return 0;
       }
        //TODO: complete class
      
       public String toString(){
           return "Patient[ Name: " + name + " Casulality: " + casualty + " Level: " + level + " ]";
       }
      
    }

    static interface HospitalADT {

        /**
         * Adds one patient to the hospital.
         * @param p the patient to be added to the hospital.
         */
        public void add(Patient p);

        /**
         * Removes and returns the highest priority patient in the hospital.
         * @return the highest priority patient in the hospital.
         */
        public Patient remove();

        /**
         * Returns true if this hospital contains no patients.
         * @return true if this hospital is empty.
         */
        public boolean isEmpty();

        /**
         * Returns the number of patients in this hospital.
         * @return the integer representation of the size of the hospital.
         */
        public int size();
    }

    static ArrayList<Patient> list = new ArrayList<Patient>();
    static class Hospital implements HospitalADT {

       @Override
       public void add(Patient p) {
           // TODO Auto-generated method stub
           list.add(p);
          
       }

       @Override
       public Patient remove() {
           // TODO Auto-generated method stub
           if(list.size()>0)
           return list.remove(0);
           else
               return null;
       }

       @Override
       public boolean isEmpty() {
           // TODO Auto-generated method stub
           return list.isEmpty();
       }

       @Override
       public int size() {
           // TODO Auto-generated method stub
           return list.size();
       }
        //TODO: complete class
    }

    public static void main(String[] args) {
        Hospital hospital = new Hospital();
        Patient p1 = new Patient("Martha Reynolds", "heavy chest pain", 2);
        Patient p2 = new Patient("Dennis Johnson", "ear ache", 3);
        Patient p3 = new Patient("John Smith", "car crash", 1);
        hospital.add(p1);
        hospital.add(p2);
        hospital.add(p3);
        System.out.println(hospital.remove());
        System.out.println(hospital.remove());
        System.out.println(hospital.remove());
    }
}
=====

See Output
2 import java.util.ArrayList; <terminated> Base HW11Q2 [Java Application] /Library/Internet Plug-Ins/JavaAppletPlugin.plugin/


Thanks, let me know if there is any concern.

Add a comment
Know the answer?
Add Answer to:
Suppose there is a hospital with a waiting room filled with several patients. Patients are prioritized...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Priority Queue Demo import java.util.*; public class PriorityQueueDemo {    public static void main(String[] args) {...

    Priority Queue Demo import java.util.*; public class PriorityQueueDemo {    public static void main(String[] args) {        //TODO 1: Create a priority queue of Strings and assign it to variable queue1               //TODO 2: Add Oklahoma, Indiana, Georgia, Texas to queue1                      System.out.println("Priority queue using Comparable:");        //TODO 3: remove from queue1 all the strings one by one        // with the "smaller" strings (higher priority) ahead of "bigger"...

  • (file ToDo.java) Add code for an equals method that checks the description of a ToDo item...

    (file ToDo.java) Add code for an equals method that checks the description of a ToDo item with a passed ToDo item’s description. The method should return true if they are identical and false if the descriptions are not identical. public class Lab8Interfaces {    /**    * @param args    * the command line arguments    */    public static void main(String[] args) {        ToDoItem[] arr = new ToDoItem[5];        ToDoItem toDo1 = new ToDoItem("Hospital Project");   ...

  • Can someone implement this using singly linked list. Please and thank you public interface Priori...

    Can someone implement this using singly linked list. Please and thank you public interface PriorityQueueInterface<T extends Comparable<T>> { /** * Adds a new entry to this priority queue * @param newEntry An object to be added. */ void add(T newEntry); /** Removes and returns the entry having the highest priority. * @return Either the object having the highest priority or, if the priority * queue is empty before the operation, null. */ T remove(); /** Retrieves the entry having the...

  • Complete the implementation of the LinkedStack class presented in Chapter 13. Specifically, complete the implementations of...

    Complete the implementation of the LinkedStack class presented in Chapter 13. Specifically, complete the implementations of the peek, isEmpty, size, and toString methods. See Base_A06Q1.java for a starting place and a description of these methods. Here is the base given: /** * Write a description of the program here. * * @author Lewis et al., (your name) * @version (program version) */ import java.util.Iterator; public class Base_A06Q1 { /** * Program entry point for stack testing. * @param args Argument...

  • JAVA Have not gotten the public Iterator<Item> iterator() . Can someone explain it please? import java.util.Iterator;...

    JAVA Have not gotten the public Iterator<Item> iterator() . Can someone explain it please? import java.util.Iterator; /* * GroupsQueue class supporting addition and removal of items * with respect to a given number of priorities and with * respect to the FIFO (first-in first-out) order for items * with the same priority. * * An example, where GroupsQueue would be useful is the airline * boarding process: every passenger gets assigned a priority, * usually a number, e.g., group 1,...

  • JAVA PROGRAMMING You are given an interface PriorityQueue (the source code is at the end of...

    JAVA PROGRAMMING You are given an interface PriorityQueue (the source code is at the end of this document) that specifies the protocols for a priority queue with generic values and priorities, where the priorities are discretely ordered rather than comparable. Implement the class ListofQueuesPQ implements PriorityQueue to fulfill the requirements of the interface (see the Javadoc in the interface’s source code). Use an ListofQueues in order to fulfill the needs of the interface. You will need to make sub-classes within...

  • import java.util.Scanner; import class17.HeapPriorityQueue; import class17.PriorityQueue; /*************** * Homework D * * * Remove any initial...

    import java.util.Scanner; import class17.HeapPriorityQueue; import class17.PriorityQueue; /*************** * Homework D * * * Remove any initial package declaration that might be added to your file in * case you edit it in eclipse. * * The goal of the homework is to create two ArrayList based implementations of * a Priority Queue as explained in Section 9.2 (in 9.2.4 and 9.2.5) of the * textbook. * * These are to be made by completing the classes PQunsorted and PQsorted as...

  • In a file named LLBag.java, write a class called LLBag that implements the Bag interface using...

    In a file named LLBag.java, write a class called LLBag that implements the Bag interface using a linked list instead of an array. You may use a linked list with or without a dummy head node. Bag interface code: /* * Bag.java * * Computer Science 112, Boston University */ /* * An interface for a Bag ADT. */ public interface Bag { /*    * adds the specified item to the Bag. Returns true on success    * and...

  • NO ONE HAS PROVIDED THE CORRECT CODE TO PROVIDE THE GIVEN OUTPUT. PLEASE PROVIDE CODE THAT...

    NO ONE HAS PROVIDED THE CORRECT CODE TO PROVIDE THE GIVEN OUTPUT. PLEASE PROVIDE CODE THAT WOULD CAUSE THE HW1.java TO PRINT THE RIGHT DATA.!!! The LinkedList class implements both the List interface and the Stack interface, but several methods (listed below) are missing bodies. Write the code so it works correctly. You should submit one file, LinkedList.java. Do not change the interfaces. Do not change the public method headers. Do not rename the LinkedList class. None of your methods...

  • JAVA PROGRAMMING You are given an interface PriorityQueue (the source code is at the end of...

    JAVA PROGRAMMING You are given an interface PriorityQueue (the source code is at the end of this document) that specifies the protocols for a priority queue with generic values and priorities, where the priorities are discretely ordered rather than comparable. Implement the class ArrayListPQ implements PriorityQueue to fulfill the requirements of the interface (see the Javadoc in the interface’s source code). Use an ArrayList in order to fulfill the needs of the interface. For each constructor and method in your...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT