Images of Output
Code
/* ----------------------------------------------------------------------------
* This program accepts a string and determines whether the string is
* a palindrome.
* ----------------------------------------------------------------------------
* Inputs: string
* Processing: Remove non-alphabetic characters.
* Build new string with characters reversed.
* Compare lowercase original to lowercase reversed string.
* Outputs: Displayed answer: palindrome or not.
*/
package jennings_palindrome;
/*-----------------------
* Anna Jennings *
* COSC 2336-101CL *
* March 25, 2019 *
*-----------------------
*/
import javafx.application.Application;
import javafx.stage.Stage;
import javafx.scene.Scene;
import javafx.scene.layout.VBox;
import javafx.geometry.Pos;
import javafx.geometry.Insets;
import javafx.scene.control.Label;
import javafx.scene.control.TextField;
import javafx.scene.control.Button;
import javafx.scene.text.TextAlignment;
public class Jennings_Palindrome extends Application
{
private String str;
private String reversed;
private String original;
private int index;
private int reverseIndex;
private StringBuilder strb;
public static void main(String[] args)
{
launch(args);
}
@Override
public void start(Stage appStage)
{
Label directions = new Label("Enter a word or phrase to find out if it's a palindrome.");
directions.setId("directions");
directions.setWrapText(true);
directions.setTextAlignment(TextAlignment.JUSTIFY);
TextField textfield = new TextField();
Button button = new Button("CHECK TEXT");
Label note = new Label("Note: Non-alphabetic characters are ignored.");
button.setOnAction(event ->
{
String str = textfield.getText();
if (! str.isEmpty())
{
Boolean palindrome;
palindrome = PalindromeCheck(str);
if (palindrome)
{
directions.setText("Yep, that's a palindrome.");
}
else
{
directions.setText("That's not a palindrome.");
}
}
});
VBox vbox = new VBox(10, directions, textfield, button, note);
vbox.setPadding(new Insets(0, 15, 0, 15));
vbox.setAlignment(Pos.CENTER);
Scene scene = new Scene(vbox, 350, 150);
scene.getStylesheets().add("palindrome.css");
appStage.setResizable(false);
appStage.setScene(scene);
appStage.setTitle("Palindrome Detector");
appStage.show();
}
public static Boolean PalindromeCheck(String str)
{
StringBuilder strb = new StringBuilder();
int index = 0;
StringBuilder originalBuild = new StringBuilder();
// Get the non-alphabetic characters out of the string.
originalBuild = RemoveNonAlpha(str, index, strb);
String original = originalBuild.toString();
if (! original.isEmpty())
{
StringBuilder reverseBuild = new StringBuilder();
int reverseIndex = original.length() - 1;
// Reverse the string.
reverseBuild = ReverseWord(original, reverseIndex, reverseBuild);
String reversed = reverseBuild.toString();
if (original.equalsIgnoreCase(reversed))
{
return true;
}
else
{
return false;
}
}
else
{
return false;
}
}
// Get the non-alphabetic characters out of the string.
public static StringBuilder RemoveNonAlpha(String str, int index,
StringBuilder strb)
{
char ch = str.charAt(index);
if (Character.isLetter(ch))
{
strb.append(ch);
}
index++;
if (index < str.length())
{
RemoveNonAlpha(str, index, strb);
}
return strb;
}
// Reverse the string.
public static StringBuilder ReverseWord(String str, int revInd,
StringBuilder revStrb)
{
{
char ch = str.charAt(revInd);
revStrb.append(ch);
revInd--;
if (revInd >= 0)
{
ReverseWord(str, revInd, revStrb);
}
return revStrb;
}
}
}
/* palindrome.css */
.root {
-fx-font-family: Tahoma;
-fx-background-color: darkslategrey;
-fx-focus-color: transparent;
-fx-faint-focus-color: transparent;
}
.button {
-fx-background-color: darkorange;
-fx-border-color: black;
-fx-font-size: 10pt;
-fx-text-fill: black;
-fx-border-width: 1px 1px 1px 1px;
-fx-background-insets: 0, 1, 2;
-fx-background-radius: 5, 4, 3;
}
.button:hover {
-fx-background-color: chocolate;
}
.label {
-fx-font-size: 10pt;
-fx-text-fill: sandybrown;
}
.text-field {
-fx-border-color: black;
-fx-border-width: 1px 1px 1px 1px;
}
#directions {
-fx-font-weight: bold;
-fx-text-fill: white;
-fx-font-size: 9pt;
}