programming palindromes with array based stack and queue

OvbSb
timer Asked: Aug 10th, 2015

Question Description

i have worked on this code all night and I am getting errors i need to have it looked through and get it to actually compile and run to be able to see if an input is actually a palindrome

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 myQueue = new MyQueue();
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.add(s);
myStack.push(s);
}
while(!myQueue.isQueueEmpty()){
if (myQueue.deQueue() == myStack.pop()){
System.out.println("Not a Palindrome. ");
return false;
}
else{
System.out.println("Is a Palindrome. ");
return true;
}
}
}
}
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{
private int capacity = 2;
    private String[] queue;
    int front = 0;
    int rear = -1;
    int currentSize = 0;
     
    public MyQueue(){
        queue = new String[this.capacity];
    }
    public void add(String item) {
         
        if (isQueueFull()) {
            System.out.println("Queue is full, increase capacity...");
            increaseCapacity();
        }
        rear++;
        if(rear >= queue.length && currentSize != queue.length){
            rear = 0;
        }
        queue[rear] = item;
        currentSize++;
    }
    public void deQueue(){
        if (isQueueEmpty()) {
            System.out.println("Underflow ! Unable to remove element from Queue");
        } else {
            front++;
            if(front > queue.length-1){
                System.out.println("removed: "+queue[front-1]);
                front = 0;
            } else {
                System.out.println("removed: "+queue[front-1]);
            }
            currentSize--;
        }
    }
    public boolean isQueueFull(){
        boolean status = false;
        if (currentSize == queue.length){
            status = true;
        }
        return status;
    }
    public boolean isQueueEmpty(){
        boolean status = false;
        if (currentSize == 0){
            status = true;
        }
        return status;
    }
     
    private void increaseCapacity(){
        int newCapacity = this.queue.length*2;
        String[] newArr = new String[newCapacity];
        int tmpFront = front;
        int index = -1;
        while(true){
            newArr[++index] = this.queue[tmpFront];
            tmpFront++;
            if(tmpFront == this.queue.length){
                tmpFront = 0;
            }
            if(currentSize == index+1){
                break;
            }
        }
        this.queue = newArr;
        System.out.println("New array capacity: "+this.queue.length);
        this.front = 0;
        this.rear = index;
    }
}

User generated content is uploaded by users for the purposes of learning and should be used following Studypool's honor code & terms of service.

This question has not been answered.

Create a free account to get help with this and any other question!

Related Tags

Brown University





1271 Tutors

California Institute of Technology




2131 Tutors

Carnegie Mellon University




982 Tutors

Columbia University





1256 Tutors

Dartmouth University





2113 Tutors

Emory University





2279 Tutors

Harvard University





599 Tutors

Massachusetts Institute of Technology



2319 Tutors

New York University





1645 Tutors

Notre Dam University





1911 Tutors

Oklahoma University





2122 Tutors

Pennsylvania State University





932 Tutors

Princeton University





1211 Tutors

Stanford University





983 Tutors

University of California





1282 Tutors

Oxford University





123 Tutors

Yale University





2325 Tutors