2012-11-06 62 views
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:  



       } 
      } 
      } 
     } 

Patient.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在這個特定的行有誤差 隊列patientQueue =新隊列<>();

錯誤是隊列是抽象的;不能實例化。我正在嘗試建立一個醫院患者隊列,其中1級是最高優先級,而4級是最低優先級。通過這樣做,我試圖實現arraylist和堆。哪裏不對?

+0

你剛纔複製+代碼從什麼地方粘貼? Queue類被故意做成抽象的,並且編譯器告訴你它不應該是。 –

回答

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> { ... }