当我编译我的代码,我收到以下错误......覆盖方法错误
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();
}
我如何更改第一个错误?我删除了前面提到的其他错误 – AndieM
我刚刚编辑了我的答案,包括一个建议,但我不知道你不知道你在做什么。特别是我不确定你想用StackNode类做什么。 –
是的,这比我在做的事更有意义。谢谢 – AndieM