我试图创建类isPalindrome并调用StackArrayBased和QueueArrayBased虽然编译错误表明我的逻辑错误?我似乎无法找到为什么它找不到这两个类的问题。首先我给了我的Palindrome代码,以下是我试图调用的两个类。
import java.util.Scanner;
public class isPalindrome {
public static void main(String[] args){
Scanner scan = new Scanner(System.in);
System.out.println("Word to check: ");
String userInput = scan.nextLine();
userInput = userInput.toUpperCase();
System.out.print(isPal(userInput)+" ");
}
public static boolean isPal(String str){
QueueArrayBased aQueue = new QueueArrayBased();
StackArrayBased aStack = new StackArrayBased();
for (int i = 0; i < str.length(); i++){
aQueue.enqueue(str.charAt(i));
aStack.push(str.charAt(i));
}
//start to compare
while(!aQueue.isEmpty()){
if(aQueue.dequeue() != aStack.pop()){
return false;
}
}
StackArrayBased
import ListReferenceBased.*;
public class StackArrayBased implements StackInterface
{
final int MAX_STACK = 50; // maximum size of stack
private Object[] items;
private int top, count;
public StackArrayBased()
{
items = new Object[MAX_STACK];
top = -1;
count = 0;
} // end default constructor
public boolean isEmpty()
{
return (top < 0);
} // end isEmpty
public boolean isFull()
{
return (top == MAX_STACK-1);
} // end isFull
public void push(Object newItem) throws StackException
{
if (!isFull()) {
items[++top] = newItem;
count++;
} else {
throw new StackException("StackException on " + "push: stack full");
} // end if
} // end push
public void popAll()
{
items = new Object[MAX_STACK];
top = -1;
count = 0;
} // end popAll
public Object pop() throws StackException
{
if (!isEmpty()) {
count--;
return items[top--];
} else {
throw new StackException("StackException on " + "pop: stack empty");
} // end if
} // end pop
public Object peek() throws StackException
{
if (!isEmpty()) {
return items[top];
} else {
throw new StackException("Stack exception on " + "peek - stack empty");
} // end if
} // end peek
public String toString()
{
StringBuilder st = new StringBuilder();
for( int i=0; i < count; i++){
st.append(items[top+i]+" ");
}
return st.toString();
}
} // end StackArrayBased
QueueArrayBased
public class QueueArrayBased implements QueueInterface
{
private final int MAX_QUEUE = 50; // maximum size of queue
private Object[] items;
private int front, back, count;
public QueueArrayBased()
{
items = new Object[MAX_QUEUE];
front = 0;
back = MAX_QUEUE-1;
count = 0;
} // end default constructor
// queue operations:
public boolean isEmpty()
{
return count==0;
} // end isEmpty
public boolean isFull()
{
return count==MAX_QUEUE;
} // end isFull
public void enqueue(Object newItem) throws QueueException
{
if (!isFull()) {
back = (back+1)%MAX_QUEUE;
items[back] = newItem;
++count;
} else {
throw new QueueException("QueueException on enqueue: Queue full");
} // end if
} // end enqueue
public Object dequeue() throws QueueException
{
if (!isEmpty()) {
// queue is not empty; remove front
Object queueFront = items[front];
front = (front+1)%MAX_QUEUE;
--count;
return queueFront;
} else {
throw new QueueException("QueueException on dequeue: Queue empty");
} // end if
} // end dequeue
public void dequeueAll()
{
items = new Object[MAX_QUEUE];
front = 0;
back = MAX_QUEUE-1;
count = 0;
} // end dequeueAll
public Object peek() throws QueueException
{
if (!isEmpty()) {
// queue is not empty; retrieve front
return items[front];
} else {
throw new QueueException("Queue exception on peek: Queue empty");
} // end if
} // end peek
public String toString()
{
StringBuilder st = new StringBuilder();
for(int i = 0; i < count; i++){
st.append(items[front+i]+" ");
}
return st.toString();
} // end of toString
``} //结束QueueArrayBased