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;
        }


        }

Queue.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 QueuepatientQueue = new Queue<>(); のこの特定の行でエラーが発生しました。

エラーは Queue is abstract; です。インスタンス化できません。レベル 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 に答える