/* Palindrome Program by Mrs. Hodara, Seabury Hall School */ import java.io.*; import java.awt.*; import java.awt.event.*; import java.awt.*; import java.applet.*; public class Palindrome extends Applet implements ActionListener { public void init() { setLayout(new BorderLayout()); setBackground(Color.cyan); setForeground(Color.red); Panel linePanel = new Panel(); Label lineLabel = new Label ("Enter phrase to check, please."); linePanel.add(lineLabel); lineField = new TextField(50); linePanel.add(lineField); add(linePanel, "North"); Panel resultPanel = new Panel(); Label resultLabel = new Label("Is it a palindrome?"); resultPanel.add(resultLabel); resultField = new TextField(6); resultPanel.add(resultField); add(resultPanel, "Center"); Panel buttonPanel = new Panel(); buttonPanel.setLayout(new FlowLayout()); Button b = new Button("Test"); b.addActionListener(this); buttonPanel.add(b); b = new Button ("Clear"); b.addActionListener(this); buttonPanel.add(b); add(buttonPanel, "South"); } public void actionPerformed(ActionEvent e) { if (e.getActionCommand().equals("Test")) testLine(); else if ( e.getActionCommand().equals("Clear")) { lineField.setText(""); resultField.setText(""); } repaint(); } public static String StripPunc(String phrase) { int index =0; String newPhrase=""; phrase = phrase.toLowerCase(); for (index=0;index=97 && phrase.charAt(index)<=122) { newPhrase = newPhrase + phrase.charAt(index); } return(newPhrase); } private void testLine() { nLine=""; line = lineField.getText(); int lineLength = line.length(); nLine=nLine +(StripPunc(line)); if (nLine.length()>0) { if (palindrome (nLine)) resultField.setText("True"); else resultField.setText("False"); } } public static boolean palindrome (String myPal) { int inL=-1, inR=myPal.length()-1; do { inL++; }while((inL<=myPal.length()/2-1)&&(myPal.charAt(inL)==myPal.charAt(inR-inL))); if (inL>myPal.length()/2-1) return true; return false; } private String nLine=""; private TextField lineField; private String line = ""; private TextField resultField; private String result = ""; }