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...
1.Write a Java program that prompts the user for a month and day and then prints...
1.Write a Java program that prompts the user for a month and day and then prints the season determined by the following rules. If an invalid value of month (<1 or >12) or invalid day is input, the program should display an error message and stop. Notice that whether the day is invalid depends on the month! You may assume that the user will never enter anything other than integers (no random strings or floats will be tested.) Tips: Break...
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.
Write program#1 upload .java file. #1 Write a java program that prompts the user for input...
Write program#1 upload .java file. #1 Write a java program that prompts the user for input using the Scanner class. First to enter their first name. Then prompts the user to enter their last name. Then prompts the user to enter their city. Then prompts the user to enter their state. Then prompts the user to enter their zip code. Concatenate first name and last name into full_name. Using String Class is optional. Use the String Class to replace zip...
Write program#1 upload .java file. #1 Write a java program that prompts the user for input...
Write program#1 upload .java file. #1 Write a java program that prompts the user for input using the Scanner class. First to enter their first name. Then prompts the user to enter their last name. Then prompts the user to enter their city. Then prompts the user to enter their state. Then prompts the user to enter their zip code.   Concatenate first name and last name into full_name. Using String Class is optional. Use the String Class to replace zip...
(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...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT