Search code examples
javapalindrome

Palindrome with Array based stack and queue check


For an assignment we are applying what is said in the title. i have written all the code out, but when I am compiling the code i get four errors dealing with the line 19 of code.

while(!myQueue<String>.isEmpty() & !myStack.isEmpty()){

this is the full code if it also helps

    import java.util.*;
    public class Palindrome{
         public static void main(String[] args){
         Scanner scan = new Scanner(System.in);
         String userInputConversion;
         String userInput;
         MyStack myStack = new MyStack();
         MyQueue<String> myQueue = new MyQueue<String>();
         System.out.println("Enter in a possible Palindrome. ");
         userInputConversion = scan.next();
         userInput = userInputConversion.toLowerCase();
         String s = new String();
    for(int i = 0; i < userInput.length(); i++){
        s = "" + userInput.charAt(i);
        System.out.print(s);
        myQueue.enqueue(s);
        myStack.push(s);
    }
    while(!myQueue<String>.isEmpty() & !myStack.isEmpty()){
        String deQueued = myQueue.dequeue();
        String popped = myStack.pop();
    if(deQueued == popped)
        System.out.println("Input is a palindrome. ");
    else
        System.out.println("input isnt a palindrome. ");
    }
        }
    }
    class MyStack{
    private String[] stack;
    private int top;
    public MyStack(){
    stack = new String [100];
    top = 0;
}
public String push(String pushP){
    if(top >= stack.length){
        System.out.println("Error: MyStack.push(): stack overflow");
        return "yes";
    }
    stack[top] = pushP;
    top++;
}
public String pop(){
    if(top <= 0){
        System.out.print("Error in MyStack.pop(): stack empty");
        return "n";
    }
    top--;
    return stack[top];
}
public boolean isEmpty(){
    if(top == 0){
        return true;
    }
    else{
        return false;
    }
    }
    `}
    class MyQueue<String> implements Iterable<String> {
    private String[] queue;
    private int front = 0;
    private int rear = 0;
    private int currentSize = 0;

public MyQueue(){
    queue = (String[])(new Object[1]);
    front = 0;
    rear = 0;
    currentSize = 0;
}
public boolean isEmpty() {
    return (currentSize == 0);
}
public int currentSize() {
    return currentSize;
}
public void enqueue(String String) {
    if (currentSize == queue.length - 1) {    
        resize(2 * queue.length);
    }

    queue[rear++] = String;

    if (rear == queue.length) {
        rear = 0;  
    }

    currentSize++;
}

public String dequeue() {
    if (this.isEmpty()) {
        throw new RuntimeException("Tried to dequeue an empty queue");
    }
    else {
        String itemToReturn = queue[front];
        queue[front++] = null; 
        currentSize--;
        if (front == queue.length) {
            front = 0;
        }
        if (currentSize == queue.length / 4) {
            resize(queue.length / 2);
        }

        return itemToReturn;
    }
}

private void resize(int capacity) {
    String[] newArray = (String[]) new Object[capacity];
    for (int i = 0; i < currentSize; i++) {
        newArray[i] = queue[(front + i) % queue.length];
    }
    queue = newArray;
    front = 0;
    rear = currentSize;
}
}

if anyone can help that would be great or give some pointers.


Solution

  • For your 2nd compilation error, The type MyQueue<String> must implement the inherited abstract method Iterable<String>.iterator(), you can either

    • implement the public Iterator<String> iterator() method
    • remove the implements Iterable<String> statement
    • or make MyQueue abstract

    Making MyQueue abstract won't help you much & I also don't see any place in the code where you need an iterator or make use of the fact that MyQueue is Iterable. Being a queue, you would want to use its signature methods - enqueue & dequeue. So, you can safely go for option 2. Else to implement, this answer should help.

    You also haven't implemented the concept of type arguments perfectly. You would want to use a Type Parameter in the class definition; e.g. class MyQueue<String> becomes class MyQueue<T>. Likewise the member variables & methods would also change.

    You 3rd compilation error, This method must return a result of type String is simply because your push() method doesn't have a return statement at the end. It's better to simply make it void, since you're not using the returned String "yes" anywhere. For StackOverflow, you can throw an RuntimeException, just like you did in your dequeue.

    Few pointers

    • You've made the classic mistake of comparing Strings with == instead of .equals() in the statement if (deQueued == popped).
    • Make it a practice to close your scanner/resources, even though in this case there's no harm.

    You have a little logical error in your while loop that compares the characters - I'll let you figure that one out.