我在编写代码时收到以下错误...
BSLinkedList.java:10: error: BSLinkedList is not abstract and does not override abstract method push(T) in BoundedStack
public class BSLinkedList <T> implements BoundedStack<T>{
^
where T is a type-variable:
T extends Object declared in class BSLinkedList
我试图确保我使用的所有内容都是通用类型T.我在这个主题上发现的所有问题都说人们没有从界面实现方法,但是我已经实现了方法push以及我界面中的所有其他方法!我感谢任何人提供的任何帮助或指导!谢谢!
import java.util.EmptyStackException;
import java.util.*;
/**
* The class BSLinkedList implements the Stack ADT as described
* in cpsc331Stack using a linked list
*
* @version 1.0
*/
public class BSLinkedList <T> implements BoundedStack<T>{
public class StackNode<T> {
private T value;
public StackNode<T> next;
private int capacity;
public StackNode<T> top;
private int size;
public StackNode(T x, StackNode<T> n){
value = x;
next = n;
}
public void BSLinkedList(int capacity) {
assert capacity >= 0;
LinkedList<T> stack = new LinkedList<T>();
size = 0;
top = (StackNode<T>)null;
}
public boolean isEmpty(){
assert size >= 0;
if (size == 0){
return true;
}
else{
return false;
}
}
public boolean isFull(){
if (size == capacity){
return true;
}
else{
return false;
}
}
public int capacity(){
return capacity;
}
public int size(){
return size;
}
public void push(T x) {
if (isFull()){
throw new FullStackException();
}
else{
++size;
top = new StackNode<T>(x, top);
}
}
public T top(){
if(isEmpty()){
throw new EmptyStackException();
}
else{
return top.value;
}
}
public T pop(){
if (isEmpty()){
throw new EmptyStackException();
}
else{
T e = top.value;
top = top.next;
--size;
return e;
}
}
}
}
和界面......
/**
* The BoundedStack interface represents the Bounded Stack ADT as described in CPSC 331.
* This interface extends the interface cpsc331Stack.
*
* @version 1.0
*/
public interface BoundedStack<T> extends cpsc331Stack<T>{
/**
* Returns the number of elements currently on the stack.
*
* @return the number of elements on the stack
*/
public int size();
/**
* Returns the maximum number of elements the stack can store.
*
* @return the maximum number of elements the stack can store
*/
public int capacity();
/**
* Tests whether the stack is full.
*
* @return true if number of elements in the stack is equal to
* the stack's capacity, false otherwise
*/
public boolean isFull();
/**
* Pushes the object x onto the top of the stack.
*
* @param x object to be pushed onto the stack.
* @throws FullStackException if the stack is full
*/
public void push (T x);
}
正常工作的其他实现......
import java.util.EmptyStackException;
/**
* The class BSArray implements the Stack ADT as described
* in cpsc331Stack using an array
*
* @version 1.0
*/
public class BSArray <T> implements BoundedStack<T>{
private T[] stack;
private int size;
private int top = -1;;
private int capacity;
/**
* Creates a new BSArray of size capacity
*
* @param capacity integer value of the maximum number of elements the array can store
*/
public BSArray(int capacity) {
assert capacity >= 0;
stack = (T[]) new Object[capacity];
}
/**
* Tests whether or not the stack is empty.
*
* @return true if the stack is empty, false otherwise
*/
public boolean isEmpty(){
if (size >= 0){
return true;
}
else{
return false;
}
}
/**
* Tests whether or not the stack is full.
*
* @return true if number of elements in the stack is equal to
* the stack's capacity, false otherwise
*/
public boolean isFull(){
if (size == capacity){
return true;
}
else{
return false;
}
}
/**
* Returns the maximum number of elements the stack can store.
*
* @return the maximum number of elements the stack can store
*/
public int capacity(){
return capacity;
}
/**
* Returns the number of elements currently on the stack.
*
* @return the number of elements on the stack
*/
public int size(){
return size;
}
/**
* Pushes the object x onto the top of the stack.
*
* @param x object to be pushed onto the stack.
* @throws FullStackException if the stack is full
*/
public void push(T x){
if (isFull()){
throw new FullStackException();
}
else{
++top;
++size;
stack[top] = x;
}
}
/**
* Returns the object at the top of the stack.
*
* @return reference to the item at the top of the stack
* @throws EmptyStackException if the stack is empty
*/
public T top(){
if(isEmpty()){
throw new EmptyStackException();
}
else{
return (T) stack[top];
}
}
/**
* Removes and returns the object at the top of the stack.
*
* @return reference to the item at the top of the stack
* @throws EmptyStackException if the stack is empty
*/
public T pop(){
if (isEmpty()){
throw new EmptyStackException();
}
else{
assert top >= -1;
T e = stack[top];
stack[top] = null;
--top;
--size;
return e;
}
}
}
cpsc331Stack界面
/**
* The cpsc331Stack interface represents the Stack ADT as described
* in CPSC 331.
*
* @version 1.0
*/
public interface cpsc331Stack<T> {
/**
* Tests whether the stack is empty.
*
* @return true if the stack is empty, false otherwise
*/
public boolean isEmpty();
/**
* Pushes the object x onto the top of the stack.
*
* @param x object to be pushed onto the stack.
*/
public void push(T x);
/**
* Returns the object at the top of the stack.
*
* @return reference to the item at the top of the stack
* @throws EmptyStackException if the stack is empty
*/
public T top();
/**
* Removes and returns the object at the top of the stack.
*
* @return reference to the item at the top of the stack
* @throws EmptyStackException if the stack is empty
*/
public T pop();
}
答案 0 :(得分:1)
这有点推测,但我相信您遇到的问题是因为您的push()
方法实际上并未覆盖您在界面中指定的确切签名。你有这个:
public void push(T x) throws FullStackException { }
但界面有这个:
public void push (T x);
要快速修复,您可以更新界面以抛出FullStackException
。
顺便说一句,您看到的错误是标准的Java 101错误,当您扩展抽象类而不覆盖该类中包含的所有抽象方法时会发生错误。所以你的编译器认为那里有一个抽象的方法push()
你永远不会覆盖(即使你做过,至少从功能上来说)。
答案 1 :(得分:0)
您已经在StackNode内部类中实现了这些方法,但它是实现该接口的外部类BSLinkedList。
此外,如前一个答案中所述,push方法声明了一个不在接口方法签名中的异常。当您修复其他问题时,这将导致不同的错误。
**在第二个例子中编辑你已经正确地实现了实现接口的类中的方法(你没有内部类)。
这是你想要实现的目标吗?
/**
* The class BSLinkedList implements the Stack ADT as described
* in cpsc331Stack using a linked list
*
* @version 1.0
*/
public class BSLinkedList<T> implements BoundedStack<T> {
private int capacity;
public StackNode<T> top;
private int size;
public void BSLinkedList(int capacity) {
assert capacity >= 0;
LinkedList<T> stack = new LinkedList<T>();
size = 0;
top = null;
}
public boolean isEmpty() {
assert size >= 0;
if (size == 0) {
return true;
} else {
return false;
}
}
public boolean isFull() {
if (size == capacity) {
return true;
} else {
return false;
}
}
public int capacity() {
return capacity;
}
public int size() {
return size;
}
@Override
public void push(T x) {
if (isFull()) {
throw new FullStackException();
} else {
++size;
top = new StackNode<T>(x, top);
}
}
public T top() {
if (isEmpty()) {
throw new EmptyStackException();
} else {
return top.value;
}
}
public T pop() {
if (isEmpty()) {
throw new EmptyStackException();
} else {
T e = top.value;
top = top.next;
--size;
return e;
}
}
public class StackNode<T> {
private T value;
public StackNode<T> next;
public StackNode(T x, StackNode<T> n) {
value = x;
next = n;
}
}
}
答案 2 :(得分:0)
您的方法签名应与其覆盖的方法完全匹配。因此,要么从BSLinkedList类的push方法中删除抛出Exception,要么在BoundedStack接口的push方法中添加throws异常。 此外,您的类将覆盖BoundedStack接口的所有方法。但是,可能有某些方法在cpsc331Stack中定义,您必须在类中实现这些方法。 请在此发布cpsc331Stack界面的源代码,以便我们更好地帮助您
答案 3 :(得分:0)
您实际上已经在内部类&#34; StackNode&#34;中定义了方法size(),capacity(),isFull(),push()。这是在&#34; BSLinkedList&#34;中定义的。如果这是你实际想要的,那么&#34; StackNode&#34;类应该实现&#34; BoundedStack&#34;而不是&#34; BSLinkedList&#34;实现&#34; BoundStack&#34;否则该方法应该在&#34; BSLinkedList&#34;中定义。
`class BSLinkedList <T>{
public class StackNode<T> implements BoundedStack<T>{
private T value;
public StackNode<T> next;
private int capacity;
public StackNode<T> top;
private int size;
public StackNode(T x, StackNode<T> n){
value = x;
next = n;
}
public void BSLinkedList(int capacity) {
assert capacity >= 0;
LinkedList<T> stack = new LinkedList<T>();
size = 0;
top = (StackNode<T>)null;
}
public boolean isEmpty(){
assert size >= 0;
if (size == 0){
return true;
}
else{
return false;
}
}
@Override
public boolean isFull(){
if (size == capacity){
return true;
}
else{
return false;
}
}
@Override
public int capacity(){
return capacity;
}
@Override
public int size(){
return size;
}
@Override
public void push(T x) {
if (isFull()){
throw new FullStackException();
}
else{
++size;
top = new StackNode<T>(x, top);
}
}
public T top(){
if(isEmpty()){
throw new EmptyStackException();
}
else{
return top.value;
}
}
public T pop(){
if (isEmpty()){
throw new EmptyStackException();
}
else{
T e = top.value;
top = top.next;
--size;
return e;
}
}
}
}`