0

Lab9.java

        package lab9;

        import java.util.Iterator;
        import java.util.LinkedList;
        import javax.swing.JOptionPane;


        public class Lab9  {


            public static void main(String[] args) {

             int SSN;
             int priority;
             Patient PR;
             boolean done = false;




             String[] choices = {"Add patient", "Discharge patient", "Status of Treatment Room","Exit"};
             String[] selections = {"Admit","Exit"};
             String[] decisions = {"Treatment Room", "Waiting Room"};


             Queue<Patient> patientQueue = new Queue<>(); 

             while(!done)
             {
                 int choice = JOptionPane.showOptionDialog(null,
                         "Select Option",
                         "ER Menu",
                         JOptionPane.YES_NO_CANCEL_OPTION,
                         JOptionPane.QUESTION_MESSAGE,
                         null,
                         choices,
                         choices[0]);
                 switch(choice)
                 {
                     case 0: 
                                String SSN_1 = JOptionPane.showInputDialog(null,"Enter Social Security Number of Patient");
                                if (SSN_1 == null || SSN_1.length() > 9)
                                {
                                    JOptionPane.showMessageDialog(null,"Error input");
                                    break;
                                }
                                SSN = Integer.parseInt(SSN_1);
                                String priority_1 = JOptionPane.showInputDialog(null,"Enter Priority Level of Patient with a SSN of "+SSN+
                                        "\n1 = Critical"
                                        + "\n"
                                + "2 = Urgent"
                                        + "\n"
                                + "3 = Moderate"
                                        + "\n"
                                + "4 = Low");
                                if (priority_1 == null || priority_1.length() > 1)
                                {
                                    JOptionPane.showMessageDialog(null,"Error input");
                                    break;
                                }
                                priority = Integer.parseInt(priority_1);
                                if(priority < 0 || priority > 4)
                                {   
                                          JOptionPane.showMessageDialog(null,"Invalid priority");
                                          break;
                                }
                                PR = new Patient(SSN,priority);
                                patientQueue.insert(PR);
                                break;

                     case 1:    



                 }
             }
            }
        }

病人.java

        package lab9;

        public class Patient {

        private int SocialSecurityNumber;
        private int Level;


        public Patient (int SSN, int priority)
        {

        SocialSecurityNumber = SSN;
        Level = priority;

        }

        public void setSSN (int SSN)
        {

        SocialSecurityNumber = SSN;

        }

        public int getSSN()
        {
            return SocialSecurityNumber;
        }

        public void setPriority (int priority)
        {

        Level = priority;

        }

        public int getPriority()
        {
            return Level;
        }


        }

队列.java

package lab9;

import java.util.*;
import java.util.PriorityQueue;

public abstract class Queue<T> extends AbstractQueue<T>{

ArrayList<T> theData = new ArrayList<>();

Comparator<T> comparator = null;

public boolean insert (T Patient)
{
    theData.add(Patient);
    return upwardFix(theData.size() - 1);
}

private boolean upwardFix(int a){

    if( a >= this.size() || a < 0)
    {
        return false;
    }
    if (a == 0)
    {
        return true;
    }
    int child = a;
    int parent = (child - 1)/2;
    while(parent >= 0 && compare(theData.get(parent), theData.get(child)) > 0)
    {
     swap (parent, child);
     child = parent;
     parent = (child - 1)/2;
    }
    return true;
}

public T remove()
{
    T result = theData.get(0);

    if (theData.size() == 1)
    {
        theData.remove(0);
        return result;
    }
    theData.set(0, theData.remove(theData.size() - 1));
    downwardFix(0);
    return result;
}

private boolean downwardFix(int a)
{
        if (a<0 || a>=this.size()) {
        return false;
    }
        if (a>=size()/2) {
        return true;
    }
        int parent = a;
        while (true) {
            int leftChild = 2 * parent + 1;
            if (leftChild >= theData.size()) {
                break; 
            }
            int rightChild = leftChild + 1;
            int minChild = leftChild; 

            if (rightChild < theData.size()
                    && compare(theData.get(leftChild), theData.get(rightChild)) > 0) {
                minChild = rightChild;
            }
            if (compare(theData.get(parent), theData.get(minChild)) > 0) {
                swap(parent, minChild);
                parent = minChild;
            } else { 
                break;
            }
        }
        return true;
    }

    private int compare(T left, T right) {

        if(comparator != null)
        {
            return comparator.compare(left, right);
        }
        else
        {
            return ((Comparable<T>) left).compareTo (right);
        }
    }

    private void swap(int parent, int child) {

    }

    public int size() {
        return 0;
    }
}

我在 Lab9.java Queue patientQueue = new Queue<>(); 的这一行遇到错误

错误是队列是抽象的;无法实例化。我正在尝试创建一个医院患者队列,其中 1 级是最高优先级,而 4 级是最低优先级。通过这样做,我正在尝试实现arraylist和堆。怎么了?

4

1 回答 1

4
public abstract class Queue<T> extends AbstractQueue<T>{

你的类Queue是抽象的。

将其更改为public class Queue<T> extends AbstractQueue<T>{.

或者,您可以创建一个扩展抽象Queue类的具体类,例如:

public class ConcreteQueue<T> extends Queue<T> { ... }
于 2012-11-06T02:00:57.397 回答