Program Description: Write a Java program that prompts the user for a word, then
ID: 3679448 • Letter: P
Question
Program Description:
Write a Java program that prompts the user for a word, then determines if that word entered is a palindrome or not. Once the determination has been made, the program will prompt the user whether or not they want to enter another word. The program will continue to run until the user presses any key other than ‘Y’ or ‘y’.
Program Requirements:
1. The program must be written in the Java language.
2. A String array based stack class must be created with the methods Push(), Pop(), Peek(), isEmpty(), and isFull(). This class should be named StringStack, and must be part of the org.uofm.tools package.
3. The program will perform the following steps within the main method: a. Describe the program to the user. b. Prompt the user to enter a word. Note that only a single word can be entered. If the user enters more than one word, the program should notify the user that they did not enter a word and the program should terminate. c. Perform a check on the word to determine whether or not the word entered is a palindrome. Whether the word is a palindrome or not, tell the user. d. The program will then prompt the user whether or not they want to enter another word. If the user enters ‘Y’ or ‘y’, the program will prompt the user to enter a word. Any other key entry will cause the program to stop. e. Once the user determines they do not want to continue, display the developer information.
4. A program description is required for this exercise, as it is required for all laboratory exercises.
5. Developer information will be displayed upon completion of the program. The format required is the same format used in previous laboratory exercises. 6. Descriptive comments are required throughout the program
Explanation / Answer
java program:
import java.util.Scanner;
import java.io.*;
public class Plindrome {
public static void main(String []args)throws IOException
{
String ch="Y";
Scanner scan=new Scanner(System.in);//reading the string
while(ch.equalsIgnoreCase("Y"))
{
System.out.print("Enter a word:");
String text=scan.next();
for(int i=0;i<text.length();i++)
if(text.charAt(i)==32)//checking for space
{
System.out.println("More than one word Entered");
System.exit(0);
}
StringStack ss=new StringStack();
for(int i=0;i<text.length();i++)//for loop for to string is palindrome or not
{
if(i<text.length()/2)
ss.push(text.charAt(i));
else if(text.length()%2==1&&i==text.length()%2)
{}
else
if(ss.peek()==text.charAt(i))
ss.pop();
}
if(ss.IsEmpty())//here we decided string is palindrome or not
System.out.println(text+" is palindrome");
else
System.out.println(text+" is not palindrome");
System.out.print("Enter (Y/y) for enter another word");
ch=scan.next();
}
}
}
class StringStack{
char []Stack=new char[30];
int top=-1;
public void push(char c){
if(!this.IsFull())
{
top++;
Stack[top]=c;
}
else
System.out.println("Stack is full");
}
public char pop(){
char c=' ';
if(!this.IsEmpty())
{
c=Stack[top];
top--;
}
else
System.out.println("Stack is Empty");
return c;
}
public char peek(){
return Stack[top];
}
public boolean IsEmpty(){
if(top==-1)
return true;
else
return false;
}
public boolean IsFull(){
if(top==Stack.length-1)
return true;
else
return false;
}
}
output:
run:
Enter a word:maram
maram is palindrome
Enter (Y/y) for enter another wordy
Enter a word:durga
durga is not palindrome
Enter (Y/y) for enter another wordy
Enter a word:maram durga
maram is palindrome
Enter (Y/y) for enter another wordBUILD SUCCESSFUL (total time: 23 seconds)
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.