The Artima Developer Community
Sponsored Link

Legacy Java Answers Forum
November 2000

Advertisement

Advertisement

This page contains an archived post to the Java Answers Forum made prior to February 25, 2002. If you wish to participate in discussions, please visit the new Artima Forums.

Message:

Palindrome Applet

Posted by TJ on December 04, 2001 at 2:30 AM

> > I am having problems creating a stack and queue. The problem is as the following:

> > A Palindrome is a word which reads the same both backwords and fowards. One way to check whether a word is a palindrome is to take each letter of the word one at a time and to place them onto both a stack and a queue. The letters are then read back from these structures and, if all letters match, the word is a palindrome.e.g tot.

> >
> > Three seperate classes are to be designed and developed.
> > The first class is to be called CharStack which will have the following public methods:

> > CharStack() - the constructor for the class which creates an empty stack.
> > void pushChar(char ch) - pushes the single chracter ch onto the stack.
> > char popChar() - pops the top character of the stack and returns it.
> > void destroyStack() - removes any remaining contents of the stack.
> > boolean isEmptyStack() - returns True if the stack is empty, otherwise False.

> > The second class is to be called CharQueue and will offer the following public methods:

> > CharQueue() - the constructor for the class which creates an empty queue.
> > void addChar(char ch) - adds the single chracter ch to the queue.
> > char popChar() - takes the head character of the queue and returns it.
> > void destroyQueue() - removes any remaining contents of the queue.
> > boolean isEmptyQueue() - returns True if the queue is empty, otherwise False.

> > The final class is to be a Java application which uses the above two classes. It should allow the user to type in a word (of an unknown length) and will use the above method of checking whether that word is a palindrome and will report the results to the user. The programme should exit when # is types in.

> > I will be greatful if you can solve my problem by writing this programme.

> > thank you for your time.

> // Palindrome.java
> // determine if input strings are palindromes
> public class Palindrome
> {
> public static void main(String[] args)
> {
> do {
> System.out.print("Enter a sentence: ");
> String sentence = SavitchIn.readLine();
> if (isPalindrome(sentence))
> System.out.println("That is a palindrome.");
> else
> System.out.println("That is not a palindrome.");
> } while (keepGoing());
> } // main
> // return true if s is a palindrome
> private static boolean isPalindrome(String s)
> {
> String simple = ""; // will hold just the letters in s
> String reverse = ""; // will hold the letters of s in reverse
> char c;
> s = s.toLowerCase();
> for (int i = 0; i < s.length(); ++i)
> {
> c = s.charAt(i);
> // could use: if (Character.isLetter(c))
> if ((c >= 'a') && (c <= 'z'))
> {
> simple = simple + c;
> reverse = c + reverse;
> }
> }
> return (simple.equals(reverse));
> } // isPalindrome
> // return true if the user wants to continue
> private static boolean keepGoing()
> {
> System.out.print("Enter another sentence? [y/n] ");
> char answer = SavitchIn.readLineNonwhiteChar();
> return ((answer == 'y') || (answer == 'Y'));
> } // keepGoing
> } // class

I saw this post and have been trying to write an applett that will allow a user to type in a word or phrase and then determine whether the string is a palindrome. Do you know how to do this and if so would you be will ing to share the code. I'm brand new to programming and need this asap. I've been working for three weeks and just can't get it. Thanks is advance for your help!






Replies:

Sponsored Links



Google
  Web Artima.com   
Copyright © 1996-2009 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use - Advertise with Us