Java Programming in Netbeans Write a program class that has two subroutines and
ID: 3828571 • Letter: J
Question
Java Programming in Netbeans
Write a program class that has two subroutines and a main routine. The program should be a part of the “firstsubroutines” class and you should name your project firstsubroutines .
Your program must prompt the user to enter a string. The program must then test the string entered by the user to determine whether it is a palindrome. A palindrome is a string that reads the same backwards and forwards, such as "radar", "racecar", and "able was I ere I saw elba". It is customary to ignore spaces, punctuation, and capitalization when looking for palindromes. For example, "A man, a plan, a canal. Panama!" is considered to be a palindrome.
To determine whether a string is a palindrome, you can: convert the string to lower case; remove any non-letter characters from the string; and compare the resulting string with the reverse of the same string. If they are equal, then the original string is considered to be a palindrome.
Here’s the code for computing the reverse of str:
String reverse;
int i;
reverse = "";
for (i = str.length() - 1; i >= 0; i--) {
reverse = reverse + str.charAt(i);
}
Write a static subroutine that finds the reverse of a string. The subroutine should have one parameter of type String and a return value of type String.
You must also write a second subroutine that takes a String as a parameter and returns a String. This subroutine should make a new string that is a copy of the parameter string, except that all the non-letters have been omitted. The new string should be returned as the value of the subroutine. You can tell that a character, ch is a lower-case letter by testing if (ch >= 'a' && ch <= 'z')
(Note that for the operation of converting str to lower case, you can simply use the built-in toLowerCase subroutine by saying: str = str.toLowerCase();)
You should write a descriptive comment before each subroutine, saying what it does.
Finally, write a main() routine that will read in a string from the user and determine whether or not it is a palindrome. The main routine should use The program should print the string converted to lower case and stripped of any non-letter characters. Then it should print the reverse string. Finally, it should say whether the string is a palindrome. (Use the two subroutines to process the user's string.) For example, if the user's input is "Hello World!", then the output might be:
Explanation / Answer
Please let me know in case of any issue.
import java.util.Scanner;
public class Palindrome {
public static String getReverse(String str){
String reverse = "";
str = str.toLowerCase();
for (int i = str.length() - 1; i >= 0; i--) {
if(str.charAt(i) >= 'a' && str.charAt(i) <= 'z')
reverse = reverse + str.charAt(i);
}
return reverse;
}
public static String getStripped(String str){
String res = "";
str = str.toLowerCase();
for (int i = 0 ; i <= str.length() - 1; i++) {
if(str.charAt(i) >= 'a' && str.charAt(i) <= 'z')
res = res + str.charAt(i);
}
return res;
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter a string: ");
String str = sc.nextLine();
String res = getStripped(str);
String reverse = getReverse(str);
if(res.equals(reverse))
System.out.println("This IS a palindrome.");
else
System.out.println("This IS NOT a palindrome.");
}
}
/*
Sample run:
Enter a string: Campus motto: Bottoms up, Mac!
This IS a palindrome.
*/
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.