Exercise 8.9. Create a new program named Palindrome.java. 2. Write a method name
ID: 663678 • Letter: E
Question
Exercise 8.9. Create a new program named Palindrome.java. 2. Write a method named first that takes a String and returns the first letter, and one named last that returns the last letter 3. Write a method named niddle that takes a String and returns a sub- string that contains everything ercept the first and last characters 104 Chapter 8. Strings and things Hint: read the documentation of the substring method in the String class. Run a few tests to make sure you understand how substring works before you try to write niddle. What happens if you invoke niddle on a string that has only two letters? One letter? No letters 1. The usual definition of a palindrome is a word that reads the same both forward and backward, like "otto and "palindromeemordnilap. An alternative way to define a property like this is to specify a way of testing for the property. For example, we might say, "a single letter is a palindrome, and a two-letter word is a palindrome if the letters are the same, and any other word is a palindrome if the first letter is the same as the last and the middle is a palindrome. Write a recursive method named isPalindrome that takes a String and that returns a boolean indicating whether the word is a palindrome or not 5. Once you have a working palindrome checker, look for ways to simplify it by reducing the mumber of conditions you check. Hint: it might be useful to adopt the definition that the empty string is a palindrome 6. On a piece of paper, figure out a strategy for checking palindromes iteratively. There are several possible approaches, so make sure you have a solid plan before you start writing code. 7. Implement your strategy in a method called isPalindromeIter 8. Optional: Appendix B provides code for reading a list of words from a file. Read a list of words and print the palindromesExplanation / Answer
Answer1-
Palindrome java code-
import java.util.*;
class Palindrome {
public static void main(String args[])
{
String original, reverse = "";
Scanner in = new Scanner(System.in);
System.out.println("Enter a string to check if it is a palindrome");
original = in.nextLine();
int length = original.length();
for ( int i = length - 1; i >= 0; i-- )
reverse = reverse + original.charAt(i);
if (original.equals(reverse))
System.out.println("Entered string is a palindrome.");
else
System.out.println("Entered string is not a palindrome.");
}
}
2.
public class Test {
public static String first ( String original ){
String initial = "";
String[] split = original.split(" ");
initial= value.substring(0,1);
return initial;
}
public static void main(String[] arg){
System.out.println( first("Hoya") );
}
}
public class Test {
public static String last ( String original ){
String initial = "";
String[] split = original.split(" ");
initial= string.substring(string.length() - 1)
return initial;
}
public static void main(String[] arg){
System.out.println( last("Hoya") );
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.