i have to write five methods... i have tried to write the first 2but they are wr
ID: 3609625 • Letter: I
Question
i have to write five methods... i have tried to write the first 2but they are wrong... please help... i'll also attach a testprogram for it.....// provide some interesting services
public class Analyze {
// doubled(): returns whether the first half of stringparameter s is the
// same as the second half
public static boolean doubled( String s ) {
int n = s.length();
int x = 0;
int y = n;
while ( n / 2 > 0 ){
if ( s.charAt (x) == s.charAt (y/2 +x) ){
}
else{
return false;
}
n = n - 2;
}
x++;
return true;
}
// aPallindrome(): reports whether string parameter s is thesame sequence
// of characters forwards and backwards
public static boolean aPallindrome( String s ) {
int n = s.length();
int x = 0;
int y = n;
while ( n / 2 > 0 ){
if ( s.charAt (x) == s.charAt (y - 1- x) ){
}
else{
return false;
}
n = n - 2;
}
x++;
return true;
}
// deTab(): returns a new string similar to string parameters, but with
// tab characters replaced by blanks.
// dePunct(): returns a new string similar to stringparameter s, but with
// punctuation characters replaced by blanks
// monospace(): returns a new string similar to stringparameter s, but with
// sequences of blanks replaced by a single blank.
}
the test program is.... it is correct
// provide some interesting services
import java.util.*;
public class TestAnalyze {
public static void main( String[] args ) {
Scanner stdin = new Scanner( System.in );
System.out.print(" Enter a string: " );
String input1 = stdin.nextLine();
boolean b1 = Analyze.doubled( input1 );
System.out.println( "Is " + input1 + " doubled: " + b1);
System.out.print(" Enter a string: " );
String input2 = stdin.nextLine();
boolean b2 = Analyze.aPallindrome( input2 );
System.out.println( "Is " + input2 + " a pallindrome: " + b2);
System.out.print(" Enter a line of text: " );
String input3 = stdin.nextLine();
String out3 = Analyze.deTab( input3 );
System.out.println( input3 + " after detabbing: " + out3);
System.out.print(" Enter a line of text: " );
String input4 = stdin.nextLine();
String out4 = Analyze.dePunct( input4 );
System.out.println( input4 + " after stripping punctuation:" + out4 );
System.out.print(" Enter a line of text: " );
String input5 = stdin.nextLine();
String out5 = Analyze.monospace( input5 );
System.out.println( input5 + " after trimming blanks: " +out5 );
}
}
Explanation / Answer
//Here is the correctedmethods.. public static boolean doubled( String s ) { int n = s.length(); int x = 0; if(n%2 !=0) //must be even return false; for(x=0;xRelated Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.