Member Avatar for iamthwee

Hello every1,

My assinement is like so. I need to write a program which accepts a word at input and then checks to c if it is a palindrome. My teachers says that it must be dun using stacks :rolleyes: otherwise we won't get any marks.

I have no idea how to start, pls help me.

Oh yes i need to make it object oriented and then incorporate it into a nifty looking GUI.

Anyone no how to make a stack? :!:

Do you have to create your own stack? If not, then java should provide one for you. I believe when you push something onto the stack it gets put at the top. So, all you have to do is loop through the stack and test whether the character you just popped off is equal to the character at that specific position in the real string. An easier way is to simply use stringbuffer to reverse the string and then compare them.

Member Avatar for iamthwee

Do you have to create your own stack? If not, then java should provide one for you. I believe when you push something onto the stack it gets put at the top. So, all you have to do is loop through the stack and test whether the character you just popped off is equal to the character at that specific position in the real string. An easier way is to simply use stringbuffer to reverse the string and then compare them.

Hello thanQ for ur hasty reply, I have to create my own stack,that's wat she says - a separate stack class. But a don't know where to begin. But just out of interest where is the stack defined by java? Do u have a link or summat? Maybe I cud use some ideas from there.

I need sum sudoe code as well for the actual thing. :cheesy:

You should first learn to communicate in regular English, that should make it a lot easier to get your points across as well as making it a lot easier to find resources online and in your library.

When I put "cud" into a search engine I will find a lot of information about cows and camels for example, but I doubt that's what you had in mind.

If you do a little research in the API documentation (mandatory reading!) you'd find out where the Stack class and its brothers and sisters are located.

Member Avatar for iamthwee

You should first learn to communicate in regular English, that should make it a lot easier to get your points across as well as making it a lot easier to find resources online and in your library.

When I put "cud" into a search engine I will find a lot of information about cows and camels for example, but I doubt that's what you had in mind.

If you do a little research in the API documentation (mandatory reading!) you'd find out where the Stack class and its brothers and sisters are located.

A link wud be great, jwenting, I've read the API but i don't no wat it is or where the stack is. BTW I have to write my own stack, pls help???

you've obviously not read the API docs or you'd have found it.
There's a class called Stack which is well documented.

Member Avatar for iamthwee

you've obviously not read the API docs or you'd have found it.
There's a class called Stack which is well documented.

hello i got this...

http://java.sun.com/j2se/1.3/docs/api/java/util/Stack.html

but the silly thing won't show me any examples, or what header files to include. :rolleyes: Jwenting is that the api thingie and how do I use it. GIve me example. I just dont no. :cry:

Here a quick program i put together for ya. Hope it helps.

import java.awt.*;
import hsa.Console;


public class Test
{
static Console c;


public static void main (String[] args)
{
c = new Console ();
String reverse = ""  ;
String word ;
word = c.readLine();
for (int count = word.length()- 1 ; count >= 0 ; count --)
{
reverse = reverse + (word.charAt(count));
}
c.println (word + " backwards is :" + reverse);
if (word.equals (reverse))
{
c.println (word + "(is a paledrome");
}
else
{
c.println (word + "(is not paledrome");
}
}
}

There are no headerfiles in Java.
I suggest you read up on import statements first of all...

And there is enough information there to know what a stack has in the form of operations.
From there you should be able to get a decent idea of what a stack actually does, which is the first step towards writing your own.

You might also want to get a decent book or two on data structures and algorithms.

Member Avatar for iamthwee

ok I managed to get something, although I still couldn't use the java API for the stack because there are no actual examples!!! If anyone would show me how and which libraries to import I would be grateful.

tdizzle342, that's wrong, read my original post, I need to use the stack!

So I managed to come up with something that uses the stack to print out a word backwards. Now how do I use this for palindromes?

And the class supports the stack of type char, how do I get it to support a stack of a string type?

// Stack.java: stack implementation
public class Stack {
   private int maxStack;
   private int emptyStack;
   private int top;
   private char[] items;


   public Stack(int size) {
      maxStack= size;
      emptyStack = -1;
      top = emptyStack;
      items = new char[maxStack];
   }

   

   public void push(char c) {
      items[++top] = c;
   }

   public char pop() {
      return items[top--];
   }

   public boolean full()  {
      return top + 1 == maxStack;
   }

   public boolean empty()  {
      return top == emptyStack;
   }
}
//Stackmain.java
public class Stackmain {

  public static void main(String[] args)
       {
    Stack s = new Stack(10); // 10 chars
    char ch;
   
    s.push('i');
    s.push('a');
    s.push('m');
    s.push('t');
    s.push('h');
    s.push('w');
    s.push('e');
    s.push('e');
    while (!s.empty())
       System.out.print(s.pop());
    System.out.println();
  }
}

that's a nice basic stack.
Turn your string into an array of char, feed those into the stack, then pop them while looping backwards through your char array.
If each pair match you have your palindrome, as soon as something doesn't match you know it's not a palindrome.

The shortest solution in Java I can come up with is

String str = "eye";
System.out.println(str + " is " + (!str.equals((new StringBuilder(str)).reverse().toString())?"not ":"" + " a palindrome");

but that obviously doesn't match your requirements (and try to explain it to your teacher ;) ).

Member Avatar for iamthwee

Turn your string into an array of char, feed those into the stack, then pop them while looping backwards through your char array.
If each pair match you have your palindrome, as soon as something doesn't match you know it's not a palindrome.

Excellent idea jwenting. I was also thinking this...

1.If the word length is odd, find the middle index value using ((length/2)+1) then pop this letter out.
2.Now the word length is even divide by 2 and store this as middle index.
3.Push characters onto the stack until you get to the middle index value.
4.Once at the middle pop each character from the stack and check to see if they match the subsequent character in the string.
5.If one doesn't match return error message.
6.If all stack is popped until empty and all characters match return success message.

Is that any good, does it even make sense?

String str = "eye";
System.out.println(str + " is " + (!str.equals((new StringBuilder(str)).reverse().toString())?"not ":"" + " a palindrome");

I could explain it to my teacher, but she is so incompetent she probably wouldn't even know what it does. :p

why go to that trouble? Saves a few CPU cycles by spending a few ;)
I'd go with that solution if on a severely memory stressed system and/or when having to process extreme amounts of data, in which case the memory savings might be (relatively) significant.
But normally I'd say start out with the simplest possible solution, if that's good enough there's no need to complicate things further.
And yes, some teachers hate such an attitude. Did it once applying a method for some physics calculation I'd learned elsewhere to an exam question designed to test another method (though not mentioning it had to be used).
Was done in 5 minutes on a question that should have needed 20. Teacher wasn't pleased but had to credit me with a correct answer and method of reaching it ;)

mind I forgot some braces in that example, correct code is

System.out.println(str + " is " + ((!str.equals((new StringBuilder(str)).reverse().toString()))?"not ":"") + " a palindrome");

which you'd probably want to turn into a method rather than typing it out all the time.

Member Avatar for iamthwee

I always go for the easiest solution too. And now that I come to think of it your idea ...

Turn your string into an array of char, feed those into the stack, then [B]pop them while looping backwards[/B] through your char array.
If each pair match you have your palindrome, as soon as something doesn't match you know it's not a palindrome.

is actually a lot cleaner than mine and still uses the stack. However, your other, rather obfuscated version...

System.out.println(str + " is " + ((!str.equals((new StringBuilder(str)).reverse().toString()))?"not ":"") + " a palindrome");

would probably be asking for trouble. In most cases, I always question what my teacher's give me. But I guess in this case I can see that she is just trying to demonstrate how a stack might be useful, and using the palindrome as a generic example.

She is incompetent though, and I find myself having to correct her. Take for example that last assignment she gave. I'm not sure if you can remember but it was to use a binary tree to sort names into alphabetical order and then create a delete method to erase a name from the tree.

Only after I pointed out that the delete method was a far from trival one, did she reconsider the difficultly of that requirement and then told the class they didn't have to do that part! :rolleyes:

Once I get this bit sorted, I may need help with the GUI part. ThanQ

yes, the assignment is really about learning what a stack is and how to write and use one.
So you'll have to do that.

I was just giving that other solution as an example of how the problem could be solved in one line making use of the full power of the language.
Thinking outside the box can help in real life, when in education it can indeed get you into trouble.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.