gpt4 book ai didi

java - 队列是抽象的;无法实例化

转载 作者:行者123 更新时间:2023-12-01 18:58:21 35 4
gpt4 key购买 nike

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 上的这一特定行上遇到错误 队列病人队列 = new Queue<>();

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

最佳答案

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

您的类(class)Queue是抽象的。

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

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

public class ConcreteQueue<T> extends Queue<T> { ... }

关于java - 队列是抽象的;无法实例化,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13243315/

35 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com