Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

/* Palindrome * Anderson, Franceschi */ import java.awt.Graphics; import java.awt.Color; import java.awt.Font; public class Palindrome { // upper left cordinates of start of animation private

/* Palindrome * Anderson, Franceschi */

import java.awt.Graphics; import java.awt.Color; import java.awt.Font;

public class Palindrome { // upper left cordinates of start of animation private static final int X_START = 100; private static final int Y_START = 50;

// String representing the String private String pal; // boolean representing the exact result private boolean exactResult; // boolean representing the computed computed by the student private boolean result; // String array representing the various Strings // as they are called by the recursive method private String [] arrPal; // current index of arrPal int index;

// animation started or not private boolean started = false; // exact result already computed or not private boolean resultSet = false;

public Palindrome( String s ) { pal = s; arrPal = new String[20]; arrPal[0] = s; index = -1; exactResult = calcExactResult( pal ); result = false; }

public Palindrome( ) { pal = ""; arrPal = new String[20]; index = -1; exactResult = false; result = false; }

public void setResult( boolean r ) { result = r; resultSet = true; }

public void setPalString( String s ) { pal = s; arrPal[0] = s; index = -1; resultSet = false; exactResult = calcExactResult( pal ); }

public String getPalString( ) { return pal; }

public void setStarted( boolean s ) { started = s; }

public boolean calcExactResult( String s ) { if ( s.length( ) <= 1 ) return true; else if ( s.substring( 0, 1 ).equalsIgnoreCase( s.substring( s.length( ) - 1, s.length( ) ) ) ) return calcExactResult( s.substring( 1, s.length( ) - 1 ) ); else return false; }

public boolean getExactResult( ) { return exactResult; }

public void updatePalindrome( String newS ) { index++; arrPal[index] = newS; }

public String convertResult( boolean finalResult ) { if ( finalResult ) return " is a palindrome"; else return " is not a palindrome"; }

public void draw( Graphics g ) { String temp = ""; if ( started ) { g.setFont( new Font( "Courier", Font.PLAIN, 18 ) ); g.setColor( Color.BLUE );

for ( int i = 0; i <= index; i++ ) { g.drawString( arrPal[i], X_START + 11 * i, Y_START + i * 20 ); }

if ( resultSet ) { g.setFont( new Font( "Serif", Font.BOLD, 18 ) ); g.setColor( Color.RED ); g.drawString( "Your result is: " + pal + convertResult( result ), X_START / 4, Y_START + ( index + 2 ) * 20 ); g.setColor( new Color( 0, 255, 127 ) ); g.drawString( "The correct result is: " + pal + convertResult( exactResult ), X_START / 4, Y_START + ( index + 4 ) * 20 ); } } } }

/* PalindromeClient * Anderson, Franceschi */

import javax.swing.JOptionPane; import javax.swing.JFrame; import java.awt.Graphics;

public class PalindromeClient extends JFrame { private Palindrome palind; boolean started = false;

public PalindromeClient() { palind = new Palindrome(); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setSize(570, 400); setVisible(true); }

public Palindrome getPalindrome() { return palind; }

public void setStarted(boolean b) { started = b; }

public boolean recursivePalindrome(String pal) { // ***** Student writes the body of this method *****

// Using recursion, determine if a String representing // a word or a sentence is a palindrome // If it is, return true, otherwise return false // We call the method animate inside the body of this method // The call to animate is already coded below animate(pal);

// Student code starts here:

return true; // replace this dummy return statement

// Student code ends here. }

public void animate(String pal) { palind.updatePalindrome(pal); palind.setStarted(true); started = true; repaint(); try { Thread.sleep(1000); // wait for the animation to finish } catch (Exception e) { } }

public void paint(Graphics g) { if (started) { super.paint(g); palind.draw(g); } }

public String getPalString() { boolean goodInput = false; String palString = ""; // palString is limited to 26 characters while (!goodInput) { try { palString = JOptionPane.showInputDialog(null, "Enter a word or phrase between 1 and 26 characters long"); if (palString != null) { if (palString.length() <= 26 && palString.length() >= 1) { goodInput = true; } } else { System.exit(0); } } catch (Exception e) { } } return palString; }

public static void main(String[] args) { PalindromeClient app = new PalindromeClient(); // ask user for input boolean result = false;

// the program will loop until the user clicks on "Cancel" while (true) { String appPal = app.getPalString(); (app.getPalindrome()).setPalString(appPal); app.setStarted(true); // Start result = app.recursivePalindrome((app.getPalindrome()).getPalString()); // Finish last step in animation (app.getPalindrome()).setResult(result); app.repaint(); System.out.println("The correct result is " + (app.getPalindrome()).getExactResult()); System.out.println("Your result is " + result); System.out.println("Done "); // done JOptionPane.showMessageDialog(null, "Done"); } } }

Please only reply if know the answer.

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Database Principles Programming And Performance

Authors: Patrick O'Neil, Elizabeth O'Neil

2nd Edition

1558605800, 978-1558605800

More Books

Students also viewed these Databases questions

Question

How do Data Types perform data validation?

Answered: 1 week ago

Question

How does Referential Integrity work?

Answered: 1 week ago