Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

JAVA Programming. In this task, fill in the code inside the recursivePalindrome

ID: 3704841 • Letter: J

Question

JAVA Programming. In this task, fill in the code inside the recursivePalindrome method to determine if a String representing a word or sentence in a palindrome. The method will return true if the String is a palindrome, false if the String is not a palindrome.

/* 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
//
// End of student code
//
}

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" );
}
}
}

/* 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 );
}
}
}
}

Explanation / Answer

Add the following code

Code

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 isPalindrome(pal , 0 , pal.length() - 1); // replace this dummy return statement

   //

   // End of student code

   //

}

public boolean isPalindrome(String pal, int p, int r)

{

     // if there is only one character in the current substrinf

     if( p == r )

         return true;

    

     // if the character at p and r are no same

     // string is not palindrome

     if( pal.charAt(p) != pal.charAt(r) )

         return false;

    

     if( p < r + 1 )

         // recursively check if the substring in between p and r is also palindrome

         return isPalindrome( pal , p + 1 , r - 1 );

        

        return true;

}