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();
}
}
visual one 0 Newbie Poster
visual one 0 Newbie Poster
jwenting 1,889 duckman Team Colleague
iamthwee
iamthwee
jwenting 1,889 duckman Team Colleague
iamthwee
server_crash 64 Postaholic
jwenting 1,889 duckman Team Colleague
iamthwee
jwenting 1,889 duckman Team Colleague
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.