public class Palindrome extends JFrame
{
private String input;private Stack<Character> charStack = new Stack<Character>();


public Palindrome(String input)
{
this.input = input;
fillStack();
}
private void fillStack()
{
for(int i = 0, len = input.length();
i < len;
++i)
{
charStack.push(input.charAt(i));
}
}
private String buildReverse()
{
StringBuilder result = new StringBuilder();
while (! charaStack.empty())
{
result.append(charStack.pop());
}
return result.toString();
}
public boolean isPalindrome()
{
return input.equalsIngnorCase(buildReverse());
}
public Palindrome()
{
//Create the input text box
final TextField input = new TextField("");


// Create the input area and place in the NORTH area.
Panel inputPanel = new Panel(new GridLayout(2,3));
inputPanel.add(new Label("")); //position 1
inputPanel.add(new Label("")); //position 2
inputPanel.add(new Label("")); //position 3
inputPanel.add(new Label("Please enter a word:", Label.RIGHT)); //position 4
inputPanel.add(input); //position 5
inputPanel.add(new Label("")); //position 6
add(inputPanel, BorderLayout.NORTH);



// Create a label for output
final Label output = new Label("", Label.CENTER);


// Create the output area and place it in the CENTER area.
Panel outputPanel = new Panel(new GridLayout(2,1));
outputPanel.add(new Label("")); //position 1
outputPanel.add(output); //position 2
add(outputPanel, BorderLayout.CENTER);



// Create a button and add a listener.
final Button palButton = new Button("Is A Palindrome?");
palButton.addActionListener
(
new ActionListener() {
public void actionPerformed(ActionEvent e) {


if (palButton.getActionCommand() == "Is A Palindrome?")
{
String original = input.getText();
boolean isPal = Palindrome(original);


if (isPal)
{
output.setText("The word: " + input.getText() +
" , is a palindrome!");


palButton.setLabel("Clear Word");
}
else
{
output.setText("The word: " + input.getText() +
" , is not a palindrome!");
input.setText("");
input.requestFocus();
}
}
else if (palButton.getActionCommand() == "Clear Word")
{
output.setText("");
input.setText("");
input.requestFocus();
palButton.setLabel("Is A Palindrome?");
}
}
});



// Create the button area and place SOUTH area.
Panel buttonPanel = new Panel(new GridLayout(3, 3));
buttonPanel.add(new Label("")); //position 1
buttonPanel.add(new Label("")); //position 2
buttonPanel.add(new Label("")); //position 3
buttonPanel.add(new Label("")); //position 4
buttonPanel.add(palButton, "2,2"); //position 5
buttonPanel.add(new Label("")); //position 6
buttonPanel.add(new Label("")); //position 7
buttonPanel.add(new Label("")); //position 8
buttonPanel.add(new Label("")); //position 9
add(buttonPanel, BorderLayout.SOUTH);



// Set frame properties
setTitle("Lab5 - Using the Stack");
setSize(new Dimension(600, 300));
setLocation(350, 225);
show();
allowClosing();


}


/**
* This method listens for a WindowEvent requesting to close the application
*/
public void allowClosing()
{
addWindowListener(new WindowAdapter()
{
public void windowClosing (WindowEvent e)
{
System.exit(0);
}
});
}


/**
* This method calls a new PalindromeTest application
*/
public static void main(String[] args)
{


new Palindromes();


}
}

Recommended Answers

All 10 Replies

I'm getting an <identifier> expected error.
on this line here:
private Stack<Character> charStack = new Stack<Character>();

you're not importing your Stack class so I assume it's in the same package, but is it actually there?

Member Avatar for iamthwee

private Stack<Character> charStack = new Stack<Character>();

This is more of a question,since i don't really no myself. But it looks as if you are using syntax from c++. The < > symbols would indicate use of templates.

Since java does not support the use of templates this would be illegal?

http://homepages.feis.herts.ac.uk/~msc_fl/fl-node59.html

And the mention of templates rekindles the age old debate, can java ever be as fast as c? He he.

:-|

Member Avatar for iamthwee

Another good point, jwenting mentioned in another thread, is to tackle this problem bit by bit.

It looks as if you went onto the next stage of designing the GUI without even making sure your stack worked correctly?

I was doing something similiar just recently... Here is my code below. I've kept it short and sweet for clarity.

// 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

//@ Criterion for palindromes as pointed out by jwenting:
//@ The word should be exactly the same
//@ when spelt backwards

  public class Stackmain {

  public static void main(String[] args)
       {
    Stack s = new Stack(100); 
    //  create a new stack of 100 chars
    
    //test case alter here
    String test = "eye";
    
    //@ convert string to char array
    char[] thwee = test.toCharArray();
    
    for(int i=0; i<thwee.length; i++)
    {
        //@ push chars onto the stack
        //@ note when we pop chars systematically 
        //@ from the stack it is the
        //@ same as printing the
        //@ original string backwards
        s.push(thwee[i]);
    }
    
    
    int count=0;
    for(int i=0; i<thwee.length; i++)
    {
       char temp = s.pop();
       if(thwee[i]==temp)
       {
           // @ increment count if chars match
           count++;
       }
    }

       if(count==thwee.length)
       {
           System.out.println("PALINDROME");
       }
       else
       {
           System.out.println("NOT PALINDROME");
       }

    //System.out.println();
    // while (!s.empty())
  }
}

This is more of a question,since i don't really no myself. But it looks as if you are using syntax from c++. The < > symbols would indicate use of templates.

Since java does not support the use of templates this would be illegal?

:-|

Actually, it's called generics and it's been supported in Java for a year or so in classfile version 49.0 (compiler version 1.5, runtime version 5.0).

Member Avatar for iamthwee

Actually, it's called generics and it's been supported in Java for a year or so in classfile version 49.0 (compiler version 1.5, runtime version 5.0).

Do u have a link? Does that mean the <syntax> is allowed in java? Sorrie, I like to learn all that I can.
:sad:

Does that mean the <syntax> is allowed in java?

Yes, it's called generics! I thought JW just told you that :confused:

check the 1.5 javadoc, especially for the Collections API. It's full of it ;)
The new version of the JLS also mentions it (if it's out by now), so does any recent Java book worth the paper it's printed on.
Then there's a load of articles all around the net.

Member Avatar for iamthwee

check the 1.5 javadoc, especially for the Collections API. It's full of it ;)
The new version of the JLS also mentions it (if it's out by now), so does any recent Java book worth the paper it's printed on.
Then there's a load of articles all around the net.

Sweet, I'm reading this up now. Ha I totally missed it wen I was doing my research on the web, probably because it's fairly recent. :eek:

Tell me does it afford the same similarities as templates do in c++?

yes and no. It's similar in ways, different in other ways.

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.