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

For this week’s assignment, you will write a program class that has two subrouti

ID: 3856317 • Letter: F

Question

For this week’s assignment, you will 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 if you are using Netbeans.

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:

As part of your assignment, you must 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:

You must complete your program, test, debug, and execute it. You should test two different cases, one that is a palindrome and another one that is not a palindrome. You must submit your java code file (preferably by cut and paste the code into the assignment dialog box). The output of your program must be captured by either copying the content in the output window and pasting it into the assignment dialog box or by capturing the image of the screen which contains the output of your java program or the output and submitting this with your assignment as an attachment. In windows you can capture a screen shot with the Ctrl Alt and Print Screen key sequence. This image can then be pasted into a Word, WordPad, or OpenOffice document which can be submitted with your assignment.

Explanation / Answer

CheckPalindrome.java

import java.util.Scanner;

public class CheckPalindrome {

public static void main(String[] args) {

//Declaring variables
String str, stripped = "", reversed;
boolean bool;

// Scanner object is used to get the inputs entered by the user
Scanner sc = new Scanner(System.in);

//Getting the input entered by the user
System.out.print("Enter a String :");
str = sc.nextLine();

//Finding the Stripped String
for (int i = 0; i < str.length(); i++) {
if ((str.charAt(i) >= 'a' && str.charAt(i) <= 'z') || (str.charAt(i) >= 'A' && str.charAt(i) <= 'Z')) {
if (Character.isUpperCase(str.charAt(i))) {
stripped += (char)(str.charAt(i) + 32);
} else {
stripped += str.charAt(i);
}
}

}

//Displaying the Stripped String
System.out.println("Stripped :" + stripped);

//calling the method by passing the Stripped String as input
reversed = reverse(stripped);

//Displaying the Reversed String
System.out.println("Reversed :" + reversed);

// Calling the method by passing the Stripped string and Reversed String as input.
bool = testPalindrome(stripped, reversed);

// based on the bool value the corresponding message will be displayed
if (bool) {
System.out.println("This is a Palindrome.");
} else {
System.out.println("This is not a Palindrome.");
}

}

/* Method implementation which Reverse the Stripped String
* @Params : String
* @return : String
*/
private static String reverse(String stripped) {
String reverse = "";
for (int i = stripped.length() - 1; i >= 0; i--) {

reverse = reverse + stripped.charAt(i);

}
return reverse;
}

/*
* This method will check whether the string is palindrome or not If the
* String is Palindrome then it returns true If not,It returns false
* Param:string return: boolean
*/
private static boolean testPalindrome(String stripped, String reversed) {

/*
* This for loop will compare each character in the user entered string
* and the reversed string
*/
for (int i = 0; i < stripped.length(); i++) {
if (stripped.charAt(i) != reversed.charAt(i))
return false;
}
return true;
}

}

______________________

Output#1:

Enter a String :"Campus motto: Bottoms up, Mac!"
Stripped :campusmottobottomsupmac
Reversed :campusmottobottomsupmac
This is a Palindrome.

__________________________

Output#2:

Enter a String :helloworld
Stripped :helloworld
Reversed :dlrowolleh
This is not a Palindrome.

_____________Could you rate me well.Plz .Thank You

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote