Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

A palindrome is a string like \"radar\" and \"racecar\" that reads the same in e

ID: 3630490 • Letter: A

Question

A palindrome is a string like "radar" and "racecar" that reads the same in either
direction. Write a static method called isPalindrome() that takes a String
object as a parameter and uses a stack to determine if it is a palindrome,
returning true if it is and false if it is not. Use an instance of either our
ArrayStack<T> or LLStack<T> class. A string of length 1 and an empty string
should both be considered palindromes. Throw an exception for null values. A
written version of this method is all that is required. You can’t use a
primitive type like char when creating an instance of a generic class. However, if
you create a stack of type Character (the wrapper class for char), then you can
store char values in the stack.
Code of ArrayStack<T>
/*
* ArrayStack.java
*
*
*/

/**
* A generic class that implements our Stack interface using an array.
*/
public class ArrayStack<T> implements Stack<T> {
private T[] items; // the items on the stack
private int top; // the index of the top item

/**
* Constructs an ArrayStack object with the specified maximum size
* for a stack that is initially empty.
*/
public ArrayStack(int maxSize) {
items = (T[])new Object[maxSize];
top = -1;
}

/**
* push - adds the specified item to the top of the stack.
* Returns false if the stack is full, and true otherwise.
*/
public boolean push(T item) {
if (isFull())
return false;
top++;
items[top] = item;
return true;
}

/**
* pop - removes the item at the top of the stack and returns a
* reference to the removed object. Returns null if the stack is
* empty.
*/
public T pop() {
if (isEmpty())
return null;
T removed = items[top];
items[top] = null;
top--;
return removed;
}

/**
* peek - returns a reference to the item at the top of the stack
* without removing it. Returns null is the stack is empty.
*/
public T peek() {
if (isEmpty())
return null;
return items[top];
}

/**
* isEmpty - returns true if the stack is empty, and false otherwise
*/
public boolean isEmpty() {
return (top == -1);
}

/**
* isFull - returns true if the stack is full, and false otherwise
*/
public boolean isFull() {
return (top == items.length - 1);
}

/**
* toString - converts the stack into a String of the form
* {top, one-below-top, two-below-top, ...}
*/
public String toString() {
String str = "{";

for (int i = top; i >= 0; i--) {
str = str + items[i];
if (i > 0)
str = str + ", ";
}

str = str + "}";
return str;
}
}

Explanation / Answer

public class ArrayStack { private T[] items; // the items on the stack private int top; // the index of the top item /** * Constructs an ArrayStack object with the specified maximum size * for a stack that is initially empty. */ public ArrayStack(int maxSize) { items = (T[])new Object[maxSize]; top = -1; } /** * push - adds the specified item to the top of the stack. * Returns false if the stack is full, and true otherwise. */ public boolean push(T item) { if (isFull()) return false; top++; items[top] = item; return true; } /** * pop - removes the item at the top of the stack and returns a * reference to the removed object. Returns null if the stack is * empty. */ public T pop() { if (isEmpty()) return null; T removed = items[top]; items[top] = null; top--; return removed; } /** * peek - returns a reference to the item at the top of the stack * without removing it. Returns null is the stack is empty. */ public T peek() { if (isEmpty()) return null; return items[top]; } /** * isEmpty - returns true if the stack is empty, and false otherwise */ public boolean isEmpty() { return (top == -1); } /** * isFull - returns true if the stack is full, and false otherwise */ public boolean isFull() { return (top == items.length - 1); } /** * toString - converts the stack into a String of the form * {top, one-below-top, two-below-top, ...} */ @Override public String toString() { String str = "{"; for (int i = top; i >= 0; i--) { str = str + items[i]; if (i > 0) str = str + ", "; } str = str + "}"; return str; } } public class PalinStackGen { public static void main(String[] args) { System.out.println("is palindrome : " + isPalindrome("radar")); } private static boolean isPalindrome(String strInput) { int numChar = strInput.length(); if(numChar == 0 || numChar == 1) { return true; } ArrayStack s = new ArrayStack(numChar); for(int i=0;i
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote