Question

In: Computer Science

Write a Java program that prompts the user to input a string and prints whether it...

Write a Java program that prompts the user to input a string and prints whether it is a palindrome. A palindrome is a string which reads the same backward as forward, such as Madam (disregarding punctuation and the distinction between uppercase and lowercase letters). The program must use the stack data structure.


The program must include the following classes:
The StackX class (or you can use the Java Stack class).
The Palindrome class which must contain a method named palindrome() of type boolean. The method must return true if the string is a palindrome and false otherwise.


The Test class which must contain the main method. In the main method, you must prompt the user to input a string and print “The string is a palindrome” if the inputted string is a palindrome. Otherwise, you must print “The string is not a palindrome.”

Here is a sample run:
Enter a word: Madam
The string is a palindrome
Here is another sample run:
Enter a word: Able was I, ere I saw Elba.
The string is a palindrome
Here is another sample run:
Enter a word: Data Structures.
The string is not a palindrome

======================================================================================

public class StackX {

private long [] stackArray ;

private int top ;

public StackX ( int size )

{

stackArray = new long [ size ]; // create the stack

top = -1; // set the top to -1

}

public void push ( long j ) {

stackArray [++ top ] = j ; // increment top , insert item

}

public long pop ()

{

return stackArray [ top - -]; // access item , decrement top

}

Solutions

Expert Solution

Program Code [JAVA]

import java.util.Scanner;
import java.util.Stack;

// Palindrome class

class Palindrome {

    String word;

    // Constructor

    public Palindrome(String word) {
        this.word = word;
    }

    // palindrome method

    public boolean palindrome() {

        // Return true if word is palindrome else false
        // Java Stack class to Store characters & converting whole string to lowercase for case-insensitive match

        Stack<Character> stack = new Stack<Character>();
        word = word.toLowerCase();

        // Pushing each character of word in StackX

        for (int i=0; i<word.length(); i++)
            stack.push(word.charAt(i));

        // Now popping from stack and matching with each character - If any wrong match it is not palindrome

        for (int i=0; i<word.length(); i++) {

            if(stack.pop() != word.charAt(i))
                return false;
        }
        return true;
    }
}

public class Test {

    public static void main(String[] args) {

        Scanner input = new Scanner(System.in);

        // Asking user to input a string

        System.out.print("Enter a word: ");
        String word = input.nextLine();

        Palindrome p = new Palindrome(word);    // calling palindrome function

        if(p.palindrome())
            System.out.println("The string is a palindrome");
        else
            System.out.println("The string is not a palindrome");
    }
}

Sample Output:-

----------------------------------------------------------------------
COMMENT DOWN FOR ANY QUERIES!!!
HIT A THUMBS UP IF YOU DO LIKE IT!!!


Related Solutions

Write a Python program which prompts the user to input a string. Then, print the string...
Write a Python program which prompts the user to input a string. Then, print the string in reverse to the terminal Sample output Please enter a word: "zeus" The reverse of zeus is suez Hint: There are several ways to accomplish this. Recall that a string is an itterable object and therefore can be used with a for loop
(JAVA) Create a program that prompts the user for an age input. The program defines the...
(JAVA) Create a program that prompts the user for an age input. The program defines the age group of the user. Follow the table below to construct the output for your program. Age Age Group 0 Baby 1-3 Toddler 4-11 Child 12-17 Teenager 18-21 Young Adult 22-64 Adult 65+ Senior Negative Number Invalid Input Sample Input Enter an age: 18 Sample Output: You are a young adult. Sample Input Enter an age: 29 Sample Output: You are an adult. Sample...
Write a program that prompts a user for an integer from 1 to 99 and prints...
Write a program that prompts a user for an integer from 1 to 99 and prints it as an amount in words. The program will loop in case the user wants to input an additional number. If the user enters -99, the program will exit. Example: Input: 89 Output: Eighty nine Input: 45 Output: Fourty five Input: -99 Output: Have a nice day. <program exits> For this project, you are to: 1) You should validate any data coming from the...
Write a program which prompts the user for a positive integer, and then prints out the...
Write a program which prompts the user for a positive integer, and then prints out the prime factorization of their response. Do not import anything other than the Scanner. One way you might go about this using nested loops: Start a "factor" variable at 2 In a loop: repeatedly print the current factor, and divide the user input by it, until the user input is no longer divisible by the factor increment the factor This plan is by no stretch...
Write a program that prompts a user for an integer from 1 to 99 and prints...
Write a program that prompts a user for an integer from 1 to 99 and prints it as an amount in words. The program will loop in case the user wants to input an additional number. If the user enters -99, the program will exit. Example: Input: 89 Output: Eighty nine Input: 45 Output: Fourty five Input: -99 Output: Have a nice day. <program exits> c++ project. need help.
in C++ programing language Write a program that prompts the user for an integer, then prints...
in C++ programing language Write a program that prompts the user for an integer, then prints all of the numbers from one to that integer, separated by spaces. Use a loop to print the numbers. But for multiples of three, print "Fizz" instead of the number, and for the multiples of five print "Buzz". For numbers which are multiples of both three and five print "FizzBuzz". Drop to a new line after printing each 20 numbers. If the user typed...
⦁   Write a Bash script that prompts for user input and reads a string of text...
⦁   Write a Bash script that prompts for user input and reads a string of text from the user. If the user enters a no null (no empty) string, the script should prompt the user to re-enter again the string; otherwise should display the string entered “. ⦁   Given an array of the following four integers (3, 5, 13, 14); write a Bash script to display the second and all elements in the array.    ⦁   Write a short Bas...
Write a MIPS assembly program that prompts the user first for a string, then for a...
Write a MIPS assembly program that prompts the user first for a string, then for a character. The program should then search the string for the character and print out the number of times the character appears in the string. You can use as many restrictions as you want, just be sure to list them. You must allocate space in the .data segment of your program for the user string.
Write a Java program that takes in a string and a number and prints back the...
Write a Java program that takes in a string and a number and prints back the string from the number repeatedly until the first character... for example Pasadena and 4 will print PasaPasPaP. Ask the user for the string and a number Print back the string from the number repeatedly until the first character For both programs please utilize: methods arrays loops Turn in screenshots
Write a Java application with a JavaFXGUI that takes a String input by the user and...
Write a Java application with a JavaFXGUI that takes a String input by the user and shows whether the string contains all 26 letters of the (English version of the Latin) alphabet. For example, "Pack my box with five dozen liquor jugs" contains all 26 letters, but "The quick frown box jumps over the hazy log" does not contain a d. It does not matter whether one or more letters appear more than once. The GUI needs, at minimum, a...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT