Question
a java program Exercise 8.7 Create a program called Recurse.java and type in the following methods: /** * Returns the first character of the given
a java program
Exercise 8.7 Create a program called Recurse.java and type in the following methods: /** * Returns the first character of the given String. */ public static char first(String s) { return s.charAt(0); } /** * Returns all but the first letter of the given String. */ public static String rest(String s) { return s.substring(1); } /** * Returns all but the first and last letter of the String. */ public static String middle(String s) { return s.substring(1, s.length() - 1); } /** * Returns the length of the given String. */ public static int length(String s) { return s.length(); } 1. Write some code in main that tests each of these methods. Make sure they work, and you understand what they do. 2. Using these methods, and without using any other String methods, write a method called printString that takes a string as a parameter and that displays the letters of the string, one on each line. It should be a void method.
3. Again using only these methods, write a method called printBackward that does the same thing as printString but that displays the string backward (again, one character per line). 4. Now write a method called reverseString that takes a string as a parameter and that returns a new string as a return value. The new string should contain the same letters as the parameter, but in reverse order. String backwards = reverseString("coffee"); System.out.println(backwards); The output of this example code should be: eeffoc 5. A palindrome is a word that reads the same both forward and backward, like otto and palindromeemordnilap. Heres one way to test whether a string is a palindrome: A single letter is a palindrome, a two-letter word is a palindrome if the letters are the same, and any other word is a palindrome if the rst letter is the same as the last and the middle is a palindrome. Write a recursive method named isPalindrome that takes a String and returns a boolean indicating whether the word is a palindrome.
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started