2

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.

Zombo
  • 1
  • 62
  • 391
  • 407
Biofoard
  • 23
  • 3
  • And what are those errors? I'm guessing the `` is the culprit. Remove it & re-compile. The type parameter should only be needed when declaring the variable. – Vineet Aug 11 '15 at 02:16
  • so i fixed that part and one other error in the part. it the comes with an error "MyQueue is not abstract and does not overide method iterator<> where String is a type variable – Biofoard Aug 11 '15 at 02:49

2 Answers2

0

First of all your making things Complex, for a simple string why do you want use stack or queue . i guess below logic would help you

String original, reverse = "";
      Scanner in = new Scanner(System.in);

      System.out.println("Enter a string to check if it is a palindrome");
      original = in.nextLine();

      int length = original.length();

      for ( int i = length - 1; i >= 0; i-- )
         reverse = reverse + original.charAt(i);

      if (original.equals(reverse))
         System.out.println("Entered string is a palindrome.");
      else
         System.out.println("Entered string is not a palindrome.");
ihappyk
  • 525
  • 1
  • 5
  • 16
  • why do you need to keep additional `reverse` string? you may simply loop floor(length/2) times, checking equality of original[0] with original[length-1] then original[1] with original[length-2] etc. – mangusta Aug 11 '15 at 02:37
  • in the prompt for the code we have to use both stack and queue for checking the palindrome – Biofoard Aug 11 '15 at 02:44
0

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.

Community
  • 1
  • 1
Vineet
  • 897
  • 10
  • 27
  • You may [accept](http://stackoverflow.com/help/someone-answers) the answer & close the question. – Vineet Aug 11 '15 at 04:29
  • i do, except when adding the code where you mentioned the fixing i received a new error – Biofoard Aug 11 '15 at 04:41
  • it deals with this public String dequeue() { if (this.isEmpty()) { throw new RuntimeException("Tried to dequeue an empty queue"); } else { String itemToReturn = queue[front]; and says type T can't be converted to String – Biofoard Aug 11 '15 at 04:43
  • It should be `T itemToReturn = queue[front];` where `T` is the type parameter. – Vineet Aug 11 '15 at 04:45
  • um i think i just have bad coding haha. By changing that T itemToReturn like you show causes the return itemToReturn; to have the same error of T can not be converted to String. – Biofoard Aug 11 '15 at 04:51
  • Yes, you would have to replace accordingly. Best go through the tutorial I provided to better understand & learn properly of its usage rather than me suggesting edits on every error you encounter. – Vineet Aug 11 '15 at 05:00
  • i found out what was wrong the public dequeue was "public String dequeue" so that was causing it to try to convert. so by switching it to public T dequeue it started working except the printing in the loop which is easy compared to everything else – Biofoard Aug 11 '15 at 05:17