In: Computer Science
Examples:
Write a java program to read a line of text and tell if the line is a palindrome. Use a stack to read each non-blank character on a stack. Treat both upper-case and lower-case version of the letter as being the same character.
- Provide these 5 sample outputs and tell if each is a palindrome or not.
Too bad--I hid a boot
Some men interpret eight memos
"Go Hang a Salami! I'm a Lasagna Hog"
(title of a book on palindromes by Jon Agee, 1991)
A man, a plan, a canal—Panama
Gateman sees my name, garageman sees name tag
Show the LinkedtStackADT<T> interface
2. Create a LinkedStackDS<T> with the following methods: default constructor, overloaded constructor, copy constructor, isEmptyStack, push, peek, pop
3. Create a private inner StackNode<T> class with the following methods: default constructor, overloaded constructor, toString
3. Exception classes: StackException, StackUnderflowException, StackOverflowException
4. Create a PalindromeDemo class that instantiates a LinkedStackDS<Character> object. Execute a do-while loop that asks the user using dialog boxes to “Input a String for Palindrome Test:” Use the replaceAll method to remove all blanks and special characters from testStr. Output whether or not it is a palindrome in a dialog box. [Use the 5 inputs given on the other handout sheet for testing.]
/****************** below is the code ***************/
/****************** LinkedStackADT.java ******************/
package code;
public interface LinkedStackADT<T> {
public boolean isEmptyStack();
public void push(T ele);
public T peek() throws StackUnderflowException;
public T pop() throws StackUnderflowException;
}
/******************* LinkedStackDS.java ********************/
package code;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
public class LinkedStackDS<T> implements LinkedStackADT<T> {
private class StackNode<T> {
private T data;
private StackNode<T> next;
public StackNode() {
this.next = null;
}
public StackNode(T data) {
this.data = data;
this.next = null;
}
public StackNode(T data, StackNode<T> next) {
this.data = data;
this.next = next;
}
@Override
public String toString() {
return data.toString();
}
}
private int size;
private StackNode<T> head;
public LinkedStackDS() {
head = null;
size = 0;
}
public LinkedStackDS(T[] arr) {
head = null;
size = 0;
for (T ele : arr) {
this.push(ele);
}
}
public LinkedStackDS(LinkedStackDS<T> s) {
head = null;
size = 0;
StackNode<T> curNode = s.head;
while (curNode != null) {
this.push(curNode.data);
curNode = curNode.next;
}
}
@Override
public boolean isEmptyStack() {
return head == null;
}
@Override
public void push(T ele) {
if (head == null) {
head = new StackNode<>(ele);
}
else {
head = new StackNode<>(ele, head);
}
++size;
}
@Override
public T peek() throws StackUnderflowException {
if (head == null)
throw new StackUnderflowException();
return head.data;
}
@Override
public T pop() throws StackUnderflowException {
if (head == null)
throw new StackUnderflowException();
T val = head.data;
head = head.next;
--size;
return val;
}
}
/****************** StackException.java *******************/
package code;
public class StackException extends Exception {
public StackException(String message) {
super(message);
}
public StackException() {
super();
}
}
/********************* StackUnderflowException.java *********************/
package code;
public class StackUnderflowException extends StackException {
public StackUnderflowException(String message) {
super(message);
}
public StackUnderflowException() {
super("Stack is Empty!");
}
}
/***************** StackOverflowException.java ****************/
package code;
public class StackOverflowException extends StackException {
public StackOverflowException(String message) {
super(message);
}
public StackOverflowException() {
super("Stack has reached it's full capacity");
}
}
/********* Note: being a LinkedStack, overflow won't occur ***************/
/***************** PallindromeDemo.java *************************/
package code;
import javax.swing.*;
import java.util.Scanner;
public class PalindromeDemo {
public static void main(String[] args) throws StackUnderflowException {
JFrame frame = new JFrame("Screen");
frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
frame.setVisible(true);
LinkedStackDS<Character> ls = new LinkedStackDS<>();
Scanner sc = new Scanner(System.in);
String input;
boolean retry = true;
do {
input = JOptionPane.showInputDialog(frame, "Input a String for Palindrome test: (\"exit\" to quit)");
input = input.trim().replaceAll("[^a-zA-Z]+", "").toLowerCase();
if (input.equalsIgnoreCase("exit")) {
retry = false;
break;
}
for (int i = 0; i < input.length(); i++) {
ls.push(input.charAt(i));
}
String reverseInput = "";
while (!ls.isEmptyStack()) {
reverseInput += ls.pop();
}
if (input.equals(reverseInput))
JOptionPane.showMessageDialog(frame, "Yes, That's a Palindrome");
else
JOptionPane.showMessageDialog(frame, "No! that isn't a palindrome.");
while (!ls.isEmptyStack()) {
try {
ls.pop();
} catch (StackUnderflowException ex) {
System.out.println(ex.getMessage());
}
}
} while (retry);
frame.dispose();
}
}
/***************** Output ********************/
/***************** If you have any questions, you may ask in comments **************/
/********************** Thanks for reading *****************/