Question

In: Computer Science

A palindrome is a string that reads the same forward and backward, i.e., the letters are...

  1. A palindrome is a string that reads the same forward and backward, i.e., the letters are the same whether you read them from right to left or from left to right.

     Examples:

  1. radar à is a palindrome
  2. Able was I ere I saw Elba à is a palindrome
  3. good à not a palindrome

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.]

Solutions

Expert Solution

/****************** 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 *****************/


Related Solutions

Foundation of Computer Science A palindrome is a string that reads the same forward and backward....
Foundation of Computer Science A palindrome is a string that reads the same forward and backward. 1. Describe an algorithm that determines whether a string of n characters is a palindrome. 2. Write its corresponding program using your favorite programming language.
C++: A palindrome is a string that is the same backward as it is forward. For...
C++: A palindrome is a string that is the same backward as it is forward. For example, “tot” and “otto” are rather short palindromes. Write a program that lets a user enter a string and that passes to a bool function a reference to the string. The function should return true if the string is a palindrome and false otherwise. When you do the judgment, capitalization, spaces, and punctuation should be neglected, that is, “Madam, I’m Adam” should test as...
A palindrome is a word or phrase, which reads the same backward or forward. Write a...
A palindrome is a word or phrase, which reads the same backward or forward. Write a program that prompts the user for a string of characters terminated by a period and determines whether the string (without the period) is a palindrome. IMP: Assume that the input contains only letters and blanks. Assume also that the input is at most 30 characters long. Use an array of characters of size 30 to store the input! Disregard blanks when deciding if the...
In C++: This is the problem: [(1)] A palindrome is a string that reads the same...
In C++: This is the problem: [(1)] A palindrome is a string that reads the same forwards as backwards. Using only a fixed number of stacks and queues, the stack and queue ADT functions, and a fixed number of int and char variables, write an algorithm to determine if a string is a palindrome. Assume that the string is read from standard input one character at a time. The algorithm should output true or false as appropriate [(2)] Let Q...
A palindrome is a word or a phrase that is the same when read both forward and backward.
6.7 LAB: PalindromeA palindrome is a word or a phrase that is the same when read both forward and backward. Examples are: "bob," "sees," or "never odd or even" (ignoring spaces). Write a program whose input is a word or phrase, and that outputs whether the input is a palindrome.Ex: If the input is:bobthe output is:bob is a palindromeEx: If the input is:bobbythe output is:bobby is not a palindromeHint: Start by removing spaces. Then check if a string is equivalent to it's reverse.This is my code:s =...
IN JAVA - [(1)] A palindrome is a string that reads the same forwards as backwards....
IN JAVA - [(1)] A palindrome is a string that reads the same forwards as backwards. Using only a fixed number of stacks and queues, the stack and queue ADT functions, and a fixed number of int and char variables, write an algorithm to determine if a string is a palindrome. Assume that the string is read from standard input one character at a time. The algorithm should output true or false as appropriate [(2)] Let Q be a non-empty...
JAVA. A palindrome is a word or a phrase that is the same when read both forward and backward.
java please. A palindrome is a word or a phrase that is the same when read both forward and backward. Examples are: "bob," "sees," or "never odd or even" (ignoring spaces). Write a program whose input is a word or phrase, and that outputs whether the input is a palindrome.Ex: If the input is:bobthe output is:bob is a palindromeEx: If the input is:bobbythe output is:bobby is not a palindromeHint: Start by removing spaces. Then check if a string is equivalent to it's reverse.Hint: Start by just handling single-word...
(Palindrome number - A number is a palindrome if it reads the same from right to...
(Palindrome number - A number is a palindrome if it reads the same from right to left and from left to right, for example 676 is a palindrome number) Write a program that prompts the user to enter a three-digit integer number and determines whether it is a palindrome number or not In Java Please
1. A is called a palindrome if it reads the same from left and right. For...
1. A is called a palindrome if it reads the same from left and right. For instance, 13631 is a palindrome, while 435734 is not. A 6-digit number n is randomly chosen. Find the probability of the event that (a) n is a palindrome. (b) n is odd and a palindrome. (c) n is even and a palindrome.
A is called a palindrome if it reads the same from left and right. For instance,...
A is called a palindrome if it reads the same from left and right. For instance, 13631 is a palindrome, while 435734 is not. A 6-digit number n is randomly chosen. Find the probability of the event that (a) n is a palindrome. (b) n is odd and a palindrome. (c) n is even and a palindrome.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT