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 Java program that prompts the user to input a word (String). The program must...
Write a Java program that prompts the user to input a word (String). The program must print the reversed word with all consecutive duplicate characters removed. The program must contain the following classes: - The StackX class (you can use the Java Stack class). - The Reverse class which must contain a private data field called “word” of type string, a constructor, and a void method called revNoDup(). The revNoDup() method must reverse the word and remove the consecutive duplicate...
Write a program that prompts the user to input a string. The program then uses the...
Write a program that prompts the user to input a string. The program then uses the function substr to remove all the vowels from the string. For example, if str=”There”, then after removing all the vowels, str=”Thr”. After removing all the vowels, output the string. Your program must contain a function to remove all the vowels and a function to determine whether a character is a vowel. You must insert the following comments at the beginning of your program and...
Write a C++ Program Write a program that prompts the user to input a string. The...
Write a C++ Program Write a program that prompts the user to input a string. The program then uses the function substr to remove all the vowels from the string. For example, if str=”There”, then after removing all the vowels, str=”Thr”. After removing all the vowels, output the string. Your program must contain a function to remove all the vowels and a function to determine whether a character is a vowel. You must insert the following comments at the beginning...
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: Write a program that prompts the user to input a type of medication and the...
JAVA: Write a program that prompts the user to input a type of medication and the output will be a list of side effects that can occur from that medication.
(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...
Question 1: Write a Java program that prompts the user to input a file name (existing...
Question 1: Write a Java program that prompts the user to input a file name (existing text file), then calculate and display the numbers of lines in that file. Also calculate and display the length of the longest line in that file. For example, if the input file has the following lines: Hello This is the longest line Bye The output should be: The file has 3 lines. The longest line is line 2 and it has 24 characters. Test...
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.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT