you need to use the code below , to add this >>> Write a method that takes a str
ID: 3724809 • Letter: Y
Question
you need to use the code below , to add this >>> Write a method that takes a string of characters as a parameter and return true if the string is a palindrome and false otherwise. Your method should use a queue and stack to check the string. Test your method by reading a string from the keyboard and passing it to the method. Hint: in the method, store a copy of the string in a stack and another copy in a queue and compare.
------------------------------------------------------------------------------------------------------------
import java.util.*;
public class testReverseQueue{
static Scanner console = new Scanner(System.in);
public static void main(String[] args) {
ArrayQueue myQueue = new ArrayQueue(100);
int num;
while(true){
System.out.print("Enter an integer value (999 to stop): ");
num = console.nextInt();
if(num==999)
break;
myQueue.enqueue(num);
}
System.out.println("Content of myQueue befor reversing: ");
System.out.println(myQueue.toString());
reverseQueue(myQueue);
System.out.println("queue content after reversing:");
System.out.println(myQueue.toString());
} // End of main
public static void reverseQueue(ArrayQueue Q){
int q_size;
q_size=Q.size();
ArrayStack tempStack = new ArrayStack(q_size);
for(int i=0; i<q_size; ++i)
tempStack.push(Q.dequeue());
for(int i=0; i<q_size; ++i)
Q.enqueue(tempStack.pop());
}// End of reverseQueue Method
}// end of reverseArrayQueue class
class ArrayStack {
private int[] data;
private int t = -1; // index of the top element in stack
public ArrayStack(int capacity) {
data = new int[capacity];
}
public int size() { return (t + 1); }
public boolean isEmpty() { return (t == -1); }
public void push(int e){
++t;
data[t] = e;
}
public int top() {
return data[t];
}
public int pop() {
int answer = data[t];
t--;
return answer;
}
public String toString() {
String str = "";
for (int j = t; j >= 0; j--) {
str=str+" "+ data[j];
}
return str;
}
}// end of ArrayStack class
class ArrayQueue {
// instance variables
/** Default array capacity. */
public static final int CAPACITY = 1000;
private int[] data;
private int front = 0; // index of front
private int qSize = 0; // queue size
// constructors
public ArrayQueue() {
data = new int[CAPACITY];
}
public ArrayQueue(int capacity) {
data = new int[capacity];
}
public int size() {
return qSize;
}
public boolean isEmpty() {
return (qSize == 0);
}
public void enqueue(int e) {
int avail = (front + qSize) % data.length;
data[avail] = e;
qSize++;
}
public int first() {
return data[front];
}
public int dequeue() {
int answer = data[front];
front = (front + 1) % data.length;
qSize--;
return answer;
}
public String toString() {
StringBuilder sb = new StringBuilder("(");
int k = front;
for (int j=0; j < qSize; j++) {
if (j > 0)
sb.append(", ");
sb.append(data[k]);
k = (k + 1) % data.length;
}
sb.append(")");
return sb.toString();
}
}// End of ArrayQueue class
public class Student{
private String name;
private int grade;
private double cgpa;
public Student (){
name="";
grade=0;
cgpa=0.0;
}
public void setStudent(String n, int g, double c){
name = n;
grade = g;
cgpa = c;
}
public String getName(){
return name;
}
public int getGrade(){
return grade;
}
public double getCGPA(){
return cgpa;
}
}// End of class Student
Explanation / Answer
###########################################################
//Method to check Palindrome, you can put this method anywhere
#####################################################
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.