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

Computer programming in JAVA. ER_Patient.java AND NoSuchException.java are compl

ID: 3918093 • Letter: C

Question

Computer programming in JAVA. ER_Patient.java AND NoSuchException.java are completed. The assignment is to be completed using the Vector Class. I need help with Heap.java and PriorityQueue.java.

ER_Patient.java :

/*

* CSC115 Assignment 6 : Emergency Room

* ER_Patient.java

* Created for use by CSC115 Summer 2018.

*/

import java.time.LocalTime;

import java.util.Random;

/**

* ER_Patient is a class that represents a patient who signs into the Emergency Room

* to be assessed and treated by an ER Physician.

* Each patient has :

* <ul>

* <li> an Id number, either accepted or generated,</li>

* <li> a time of admission, either accepted or generated,</li>

* <li> and a specific symptomCategory that determines the priority number [1,4], inclusive,

* calculated within the program.</li>

* </ul>

*/

public class ER_Patient implements Comparable<ER_Patient> {

/* Static variables necessary to randomize Id numbers */

private static final int numDigits = 2;

private static int range = (int)(Math.pow(10,numDigits));

private static boolean[] used = new boolean[range];

private static int[] fresh;

private static int currIndex = 0;

private static boolean open = false;

/* instance variables */

private String patientId;

private String symptomCategory;

private int priorityNum;

private LocalTime admitTime;

/**

* Create ER_Patient admission information.

* @param admitTime The time given in hh:mm:ss format.

* @param patientNumber A number given to the patient.

* If this number is unique, then it is applied to the Patient Id.

* If it is not unique, then a random unique Id is attributed to the

* patient.

* @param symptomCategory The category that determines the priority.

* Acceptable categories are:<ol>

* <li>Life-threatening</li>

* <li>Chronic</li>

* <li>Major fracture</li>

* <li>Walk-in</li>

* </ol>

*/

public ER_Patient(String admitTime, int patientNumber, String symptomCategory) {

if (!open) {

createUniqueIdList();

}

this.admitTime = LocalTime.parse(admitTime);

patientId = setUniqueId(patientNumber);

this.symptomCategory = symptomCategory;

priorityNum = determinePriority();

}

/**

* Create ER_patient admission information.

* A patient Id number is generated.

* @param admitTime The time given in hh:mm:ss format.

* @param symptomCategory The category that determines the priority.

* Acceptable categories are:<ol>

* <li>Life-threatening</li>

* <li>Chronic</li>

* <li>Major fracture</li>

* <li>Walk-in</li>

* </ol>

*/

public ER_Patient(String admitTime, String symptomCategory) {

if (!open) {

createUniqueIdList();

}

this.admitTime = LocalTime.parse(admitTime);

patientId = createUniqueId();

this.symptomCategory = symptomCategory;

priorityNum = determinePriority();

}

/**

* Create ER_patient dmission information.

* A patient Id number is generated.

* The admission time is the current time.

* @param symptomCategory The category that determines the priority.

* Acceptable categories are:<ol>

* <li>Life-threatening</li>

* <li>Chronic</li>

* <li>Major fracture</li>

* <li>Walk-in</li>

* </ol>

*/

public ER_Patient(String symptomCategory) {

if (!open) {

createUniqueIdList();

}

admitTime = LocalTime.now();

admitTime = admitTime.minusNanos(admitTime.getNano());

patientId = createUniqueId();

this.symptomCategory = symptomCategory;

priorityNum = determinePriority();

}

/*

* Given the symptom, assigns a priority number.

* Assumes that the

*/

private int determinePriority() {

switch(symptomCategory) {

case "Life-threatening":

return 1;

case "Chronic":

return 2;

case "Major fracture":

return 3;

case "Walk-in":

return 4;

default:

throw new NoSuchCategoryException("Category does not exist.");

}

}

/**

* @return The id number of the patient.

*/

public String getId() {

return patientId;

}

/**

* @return The priority number of the patient.

*/

public int getPriority() {

return priorityNum;

}

/**

* @return The patient's symptom category.

*/

public String getSymptomCategory() {

return symptomCategory;

}

/**

* Determines equality: If this patient has the same id, then

* equality is true.

* @param other The patient to compare to this one.

* @return True if the paitent id of this patient is the same

* as the other pateint's id.

*/

public boolean equals(ER_Patient other) {

return this.patientId.equals(other.patientId);

}

/**

* Determines whether this patient should be seen before the other patient.

* Order is determined first by priority number and secondarily by the admission

* time.

* @param other The patient to compare to this one.

* @return <ul>

* <li>a number less than 0 if this patient has a higher priority,</li>

* <li>0 if the priorities are equal (which should only happen if it is the same patient), or</li>

* <li>a number greater than 0 if the other patient has a higher priority.</li>

* </ul>

* @throws ClassCastException if the parameter object is not of type ER_Patient.

*/

public int compareTo(ER_Patient other) {

int priorityDiff = this.priorityNum - other.priorityNum;

if (priorityDiff != 0) {

return priorityDiff;

} else {

return this.admitTime.compareTo(other.admitTime);

}

}

/**

* The string representation of a Patient.

* Format:

* <pre>

* patientId: priorityNum admission time symptomCategory

* </pre>

* @return Information about this patient as a String.

*/

public String toString() {

StringBuilder s = new StringBuilder(range+2+2+8+symptomCategory.length());

s.append(patientId+": "+priorityNum+" ");

s.append(admitTime+" ");

s.append(symptomCategory);

return s.toString();

}

/**

* A private method that shuffles an array of integers between 0 and range.

* Used to determine the unique values.

* This method is only called once for the first time a ER_Patient is created.

*/

private void createUniqueIdList() {

fresh = new int[range];

for (int i=0; i<range; i++) {

fresh[i] = i;

}

// Implement the Durstenfeld shuffle

Random rand = new Random();

int r, tmp;

for (int i=range-1; i>0; i--) {

r = rand.nextInt(i+1);

tmp = fresh[i];

fresh[i] = fresh[r];

fresh[r] = tmp;

}

open = true;

}

/**

* A private method that sets the number as the patient id, after

* checking that it has not been used before.

* If it has, then a new number is generated instead.

*/

private String setUniqueId(int num) {

if (num < 0 || num > range-1) {

return createUniqueId();

}

if (used[num]) {

return createUniqueId();

}

return createId(num);

}

/**

* A private method that creates a unique patient id number.

*/

private String createUniqueId() {

while (currIndex < fresh.length && used[fresh[currIndex]]) {

currIndex++;

}

if (currIndex >= fresh.length) {

throw new RuntimeException("Cannot create the new ER_Patient: No unique id numbers available.");

}

return createId(fresh[currIndex++]);

}

/**

* Converts the checked number into a String starting with 'P'.

*/

private String createId(int num) {

StringBuilder sb = new StringBuilder(numDigits+1);

sb.append('P');

used[num] = true;

int base = range/10;

int currDigit;

while (base >= 1) {

currDigit = num/base;

if (currDigit != 0) {

num %= 10;

}

sb.append(currDigit);

base /= 10;

}

return sb.toString();

}

/**

* The unit tester for this class.

* @param args Not used.

*/

public static void main(String[] args) {

ER_Patient[] patients = new ER_Patient[5];

String[] complaints = {"Walk-in", "Life-threatening","Chronic","Major fracture", "Chronic"};

for (int i=0; i<5; i++) {

patients[i] = new ER_Patient(complaints[i]);

// spread out the admission times by 1 second

try {

Thread.sleep(1000);

} catch (InterruptedException e) {

System.out.println("sleep interrupted");

return;

}

}

System.out.println("Patients waiting:");

for (int i=0; i<5; i++) {

System.out.println(patients[i]);

}

}

}

__________________________________________________________________________________________________

NoSuchCategoryException.java :

/*

* CSC115 Assignment 5 : Emergency Room

* NoSuchCategoryException.java

* Created for use by CSC115 Summer 2018.

*/

/**

* An runtime exception indicating that a particular category was not found

* for a process to continue.

* In this context, it is used to indicate that a

* particular ER_Patient symptom was not recognized and an ER priority number

* cannot be attributed to the patient.

*/

public class NoSuchCategoryException extends RuntimeException {

/**

* Create an exception.

* @param msg The message associated with this exception.

*/

public NoSuchCategoryException(String msg) {

super(msg);

}

/**

* Create a default exception.

*/

public NoSuchCategoryException() {

super();

}

}

__________________________________________________________________________________________________

Heap.java PLEASE HELP. Keep the vector class but feel free to edit the methods

import java.util.NoSuchElementException;

import java.util.Vector;

import java.util.Comparator;

/*

* The shell of the class, to be completed as part

* of CSC115 Assignment 5: Emergency Room.

*/

public class Heap<E extends Comparable<E>> {

private Vector<E> heapArray;

private Comparator <? super E> comparator;

public Heap() {

heapArray = new Vector <E> ();

}

public int size(){

return heapArray.size();

}

public boolean heapIsEmpty(){

if (heapArray.size() == 0){

return true;

} else return false;

}

public void heapInsert(E item) throws NoSuchElementException {

heapArray.addElement(item);

trickleUp(item);

}

private void trickleUp(E item){

int index = heapArray.size()-1;

int parent = (index -1) / 2 ;

while (parent >=0 && compareItems(heapArray.get(index), heapArray.get(parent))<0){

E temp = heapArray.get(parent);

heapArray.set(parent, heapArray.get(index));

heapArray.set(index,temp);

index = parent;

parent = (index -1) / 2;

}

}

public E getRootItem() {

return heapArray.get(0);

}

public E heapDelete(){

E rootItem = null;

int index;

if (!heapIsEmpty()){

rootItem = heapArray.get(0);

index = heapArray.size() - 1;

heapArray.set (0, heapArray.get(index));

heapArray.remove (index);

heapRebuild(0);

}

return rootItem;

}

private void heapRebuild (int root){

int child = 2*root +1;

if (child < heapArray.size()){

int rightChild = child +1;

if ( (rightChild < heapArray.size())&& (compareItems(heapArray.get(rightChild),heapArray.get(child))<0)){

child = rightChild;

}

}

if (compareItems(heapArray.get(root), heapArray.get(child))>0){

E temp = heapArray.get(root);

heapArray.set(root,heapArray.get(child));

heapArray.set(child,temp);

heapRebuild(child);

}

}

public int compareItems(E item1, E item2){

if (comparator == null){

return (item1).compareTo(item2);

} else {

return comparator.compare(item1, item2);

}

}

  

  

public static void main (String [] args){

}

}

__________________________________________________________________________________________________

PriorityQueue.java : Again, feel free to edit.

import java.util.NoSuchElementException;

/*

* The shell of the class, to be completed as part of the

* CSC115 Assignment 5 : Emergency Room

*/

public class PriorityQueue<E extends Comparable<E>> {

private Heap<E> heap;

public PriorityQueue() {

heap = new Heap<E>();

}

public boolean pqIsEmpty(){

return heap.heapIsEmpty();

}

public void pqInsert ( E newItem) {

heap.heapInsert (newItem);

}

public E pqDelete () {

return heap.heapDelete();

}

public static void main(String[] args) {

}

}

__________________________________________________________________________________________________

D Data Abstraction.Proble assn5,pdf G how to screenshot on wi ? Secure https://connex.csc.uvic.ca access/content/attachment/Sf8f2fc93d22-4523-a63a-fa2ad423f1ce/Assignments/bafbae57 fete 43ed-892d-789c91c90ee6/assn5.pdf a?? 115 The Emergency Room Objectives Upon completion of this assignment, you need to be able to: . Create a binary tree using a Vector, which is Java's version of a resizable array Create a version of the PriorityQueue ADT, using a Heap . Apply daia abstraction, hiding a complex data type inside a simpler ADT. Continue to apply principles of inheritance, encapsulation and modularity Continue to apply good programming practice in - designing programs, - proper documentation, and -testing and debugging code. References: .CSC113 Java coding conventions. . Textbook Chapter 12 . The specification pages provided. Introduction A group of patients are sitting in the local hospital's Emergency waiting room, when the attending ER physician arrives for ber shift. The triage nurse has already assessed patients by their main complaint and provides an electronic device whereby the physician can touch the screen and the next patient chart is provided. The ondering of the charts is determined ENG 1:15 PM O Type here to search US 2018-07-25 4

Explanation / Answer

import java.util.NoSuchElementException;
import java.util.Vector;

public class Heap<E extends Comparable<E>>{

   private Vector<E> heapArray;
   private final int INITIAL_SIZE = 1;
   private final int VECTOR_INCREMENT = 1;

   //default constructor
   public Heap(){
        heapArray = new Vector<E>(INITIAL_SIZE, VECTOR_INCREMENT);
   }

   // Determines if a heap object is empty.
  
   public boolean isEmpty(){
       return heapArray.isEmpty();
   }

   // Determines the number of items in a heap.
  
   public int size(){
       return heapArray.size();
   }

   // Inserts an item into a heap.
  
   public void insert(E item){
       heapArray.add(item);
       bubbleUp(size() - 1); //bubble the added item up if needed,
       //preserve min heap
   }

   // Recursive method used after insert to preserve min heap order.
  
   private void bubbleUp(int index){
       if(heapArray.isEmpty())
           throw new NoSuchElementException("The heap is empty.");
       if(index < 0 || index >= size())
           throw new NoSuchElementException("The passed index was out of range.");
       int parentIndex = ((index + 1) / 2) - 1;
       E child = heapArray.get(index);
       if(index == 0 || child.compareTo(heapArray.get(parentIndex)) >= 0)
           return;
       heapArray.set(index, heapArray.get(parentIndex));
       heapArray.set(parentIndex, child);
       bubbleUp(parentIndex);
   }

   // Removes and returns the root item (smallest item in min heap)
  
   public E removeRootItem(){
       if(isEmpty())
           throw new NoSuchElementException("The heap is empty.");
       if(size() == 1)
           return heapArray.remove(0);
       E temp = heapArray.get(0);
       heapArray.set(0, heapArray.remove(size() - 1));
       bubbleDown(0);
       return temp;
   }


   // Recursive method used after remove to preserve min heap order.
  
   private void bubbleDown(int index){
       if(heapArray.isEmpty())
           throw new NoSuchElementException("The heap is empty.");
       if(index < 0)
           throw new NoSuchElementException("The passed index was negative.");
       if(index + 1 > size() / 2)
           return;
      
       int childIndexRight = index * 2 + 2;
       boolean childRightExist = childIndexRight < size();
       E rightChild = null;
       if(childRightExist)
           rightChild = heapArray.get(childIndexRight);
      
       int childIndexLeft = index * 2 + 1;
       boolean childLeftExist = childIndexLeft < size();
       E leftChild = null;
       if(childLeftExist)
           leftChild = heapArray.get(childIndexLeft);
      
       E parent = heapArray.get(index);
      
       if(childLeftExist && childRightExist){
           if(leftChild.compareTo(rightChild) <= 0 && parent.compareTo(leftChild) > 0){
              
               heapArray.set(index, leftChild);
               heapArray.set(childIndexLeft, parent);
               bubbleDown(childIndexLeft);
           }
           else if(parent.compareTo(rightChild) > 0){
              
               heapArray.set(index, rightChild);
               heapArray.set(childIndexRight, parent);
               bubbleDown(childIndexRight);
           }
       }
       else if(childLeftExist
           && parent.compareTo(leftChild) > 0){
               heapArray.set(index, leftChild);
               heapArray.set(childIndexLeft, parent);
               bubbleDown(childIndexLeft);
       }
       else if(childRightExist
           && parent.compareTo(rightChild) > 0){
               heapArray.set(index, rightChild);
               heapArray.set(childIndexRight, parent);
               bubbleDown(childIndexRight);
       }
   }/

   // Returns the root item (smallest item in min heap), without removing
  
   public E getRootItem(){
       if(isEmpty())
           throw new NoSuchElementException("The heap is empty.");
       return heapArray.get(0);
   }
   // Returns a String form of heapArray
  
   public String toString(){
       String heap = new String();
       heap = "{";
       if(!isEmpty())
           heap += heapArray.get(0);
       for(int i = 1; i < size(); i++){
           heap += ", " + heapArray.get(i);
       }
       heap += "}";
       return heap;
   }
   //Main is used for internal testing here
   public static void main(String[] args){
       Heap heap = new Heap();
       System.out.println("Created new empty heap: " + heap);
       System.out.print("Is heap empty?: " + heap.isEmpty());
       System.out.println(" Size = " + heap.size());
       int a = 0;
       int b = 1;
       int c = 2;
       int d = 3;
       int e = 4;
       int f = 5;
       int g = 6;

       //TESTING insert, empty, size, getRoot and toString methods
       heap.insert(g);
       System.out.println("Inserting g = 6: " + heap);
       System.out.print("Empty = " + heap.isEmpty());
       System.out.print(" Size = " + heap.size());
       System.out.println(" Root = " + heap.getRootItem());
       heap.insert(f);
       System.out.println("Inserting f = 5: " + heap);
       System.out.print("Empty = " + heap.isEmpty());
       System.out.print(" Size = " + heap.size());
       System.out.println(" Root = " + heap.getRootItem());
       heap.insert(e);
       System.out.println("Inserting e = 4: " + heap);
       System.out.print("Empty = " + heap.isEmpty());
       System.out.print(" Size = " + heap.size());
       System.out.println(" Root = " + heap.getRootItem());
       heap.insert(d);
       System.out.println("Inserting d = 3: " + heap);
       System.out.print("Empty = " + heap.isEmpty());
       System.out.print(" Size = " + heap.size());
       System.out.println(" Root = " + heap.getRootItem());
       heap.insert(c);
       System.out.println("Inserting c = 2: " + heap);
       System.out.print("Empty = " + heap.isEmpty());
       System.out.print(" Size = " + heap.size());
       System.out.println(" Root = " + heap.getRootItem());
       heap.insert(b);
       System.out.println("Inserting b = 1: " + heap);
       System.out.print("Empty = " + heap.isEmpty());
       System.out.print(" Size = " + heap.size());
       System.out.println(" Root = " + heap.getRootItem());
       heap.insert(a);
       System.out.println("Inserting a = 0: " + heap);
       System.out.print("Empty = " + heap.isEmpty());
       System.out.print(" Size = " + heap.size());
       System.out.println(" Root = " + heap.getRootItem());

       //Testing remove methods
       System.out.println("Remove root = " + heap.removeRootItem() + ": " + heap);
       System.out.print("Empty = " + heap.isEmpty());
       System.out.print(" Size = " + heap.size());
       System.out.println(" Root = " + heap.getRootItem());

       System.out.println("Remove root = " + heap.removeRootItem() + ": " + heap);
       System.out.print("Empty = " + heap.isEmpty());
       System.out.print(" Size = " + heap.size());
       System.out.println(" Root = " + heap.getRootItem());

       System.out.println("Remove root = " + heap.removeRootItem() + ": " + heap);
       System.out.print("Empty = " + heap.isEmpty());
       System.out.print(" Size = " + heap.size());
       System.out.println(" Root = " + heap.getRootItem());

       System.out.println("Remove root = " + heap.removeRootItem() + ": " + heap);
       System.out.print("Empty = " + heap.isEmpty());
       System.out.print(" Size = " + heap.size());
       System.out.println(" Root = " + heap.getRootItem());

       System.out.println("Remove root = " + heap.removeRootItem() + ": " + heap);
       System.out.print("Empty = " + heap.isEmpty());
       System.out.print(" Size = " + heap.size());
       System.out.println(" Root = " + heap.getRootItem());

       System.out.println("Remove root = " + heap.removeRootItem() + ": " + heap);
       System.out.print("Empty = " + heap.isEmpty());
       System.out.print(" Size = " + heap.size());
       System.out.println(" Root = " + heap.getRootItem());

       System.out.println("Remove root = " + heap.removeRootItem() + ": " + heap);
       System.out.print("Empty = " + heap.isEmpty());
       System.out.println(" Size = " + heap.size());

       //testing exceptions
       System.out.println("Testing Exceptions on empty heap:");
       try{
           heap.getRootItem();
       }
       catch(NoSuchElementException E){
           System.out.println("Succesfully caught getRootItem on empty heap exception.");
       }
       try{
           heap.removeRootItem();
       }
       catch(NoSuchElementException E){
           System.out.println("Succesfully caught removeRootItem on empty heap exception.");
       }
   }

}
---------------------------------------------------------------------------------------------------------
import java.util.NoSuchElementException;

public class PriorityQueue<E extends Comparable<E>> {
  
   private Heap<E> heap;//min heap to store priority queue

   //default constructor
   public PriorityQueue(){
        heap = new Heap();//initialize empty heap
   }

   // Inserts an item into the priority queue
  
   public void enqueue(E item){
       heap.insert(item);
   }
   
    // Removes and returns the item of highest priority from the queue
   
    public E dequeue(){
        if(isEmpty())
            throw new NoSuchElementException("The queue is empty.");
        return heap.removeRootItem();
    }

    //Returns the item of highest priority from the queue
   
    public E peek(){
        if(isEmpty())
            throw new NoSuchElementException("The queue is empty.");
        return heap.getRootItem();
    }

    // Determines if the queue is empty.
   
    public boolean isEmpty(){
        return heap.isEmpty();
    }

    // Returns a String form of the queue, preserving its order.
   
    public String toString(){
        return heap.toString();
    }

    //main is used for testing
    public static void main(String[] args) throws InterruptedException {
          PriorityQueue priorityList = new PriorityQueue();
         
          ER_Patient patientA = new ER_Patient("Life-threatening");
          Thread.sleep(1000);
          ER_Patient patientB = new ER_Patient("Chronic");
          Thread.sleep(1000);
          ER_Patient patientC = new ER_Patient("Major fracture");
          Thread.sleep(1000);
          ER_Patient patientD = new ER_Patient("Walk-in");
          Thread.sleep(1000);
          ER_Patient patientA2 = new ER_Patient("Life-threatening");
          Thread.sleep(1000);
          ER_Patient patientB2 = new ER_Patient("Chronic");
          Thread.sleep(1000);
          ER_Patient patientC2 = new ER_Patient("Major fracture");
          Thread.sleep(1000);
          ER_Patient patientD2 = new ER_Patient("Walk-in");

          System.out.println("Created new PriorityQueue.");
          System.out.println("Empty = " + priorityList.isEmpty());


          //testing enqueue/peek/isEmpty by adding in random order
          priorityList.enqueue(patientB2);
          System.out.println("Next in line: " + priorityList.peek());
          System.out.println("Empty = " + priorityList.isEmpty());
          priorityList.enqueue(patientD);
          System.out.println("Next in line: " + priorityList.peek());
          System.out.println("Empty = " + priorityList.isEmpty());
          priorityList.enqueue(patientC);
          System.out.println("Next in line: " + priorityList.peek());
          System.out.println("Empty = " + priorityList.isEmpty());
          priorityList.enqueue(patientB);
          System.out.println("Next in line: " + priorityList.peek());
          System.out.println("Empty = " + priorityList.isEmpty());
          priorityList.enqueue(patientA2);
          System.out.println("Next in line: " + priorityList.peek());
          System.out.println("Empty = " + priorityList.isEmpty());
          priorityList.enqueue(patientD2);
          System.out.println("Next in line: " + priorityList.peek());
          System.out.println("Empty = " + priorityList.isEmpty());
          priorityList.enqueue(patientC2);
          System.out.println("Next in line: " + priorityList.peek());
          System.out.println("Empty = " + priorityList.isEmpty());
          priorityList.enqueue(patientA);
          System.out.println("Next in line: " + priorityList.peek());
          System.out.println("Empty = " + priorityList.isEmpty());


          //testing dequeue
          System.out.println("Printing patients in order: ");
          System.out.println(priorityList.dequeue());
          System.out.println(priorityList.dequeue());
          System.out.println(priorityList.dequeue());
          System.out.println(priorityList.dequeue());
          System.out.println(priorityList.dequeue());
          System.out.println(priorityList.dequeue());
          System.out.println(priorityList.dequeue());
          System.out.println(priorityList.dequeue());

          //testing exceptions
           System.out.println("Testing Exceptions on empty queue:");
           try{
               priorityList.peek();
           }
           catch(NoSuchElementException E){
               System.out.println("Succesfully caught peek on empty queue exception.");
           }
           try{
               priorityList.dequeue();  
           }
           catch(NoSuchElementException E){
               System.out.println("Succesfully caught dequeue on empty queue exception.");
           }


    }
          
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote