Activity 3. Recognizing Sentence Palindromes. In this activity, we expect you to
ID: 3669756 • Letter: A
Question
Activity 3. Recognizing Sentence Palindromes.
In this activity, we expect you to design a method that will be able to identify whether a given sentence is a sentence palindrome or not. In particular:
isSentencePalindrome
Your method is as follows:
Name: isSentencePalindrome
Parameter: a String, let’s call it str
Returns: a boolean – true if str is a sentence palindrome, false otherwise
How to use isSentencePalindrome?
From within the main (where it says Activity 3), here is what you have to implement:
1/ Ask the user for his/her name
2/ Ask the user, using his/her name, whether or not s/he wants to check whether a string is a sentence palindrome
3/ If the user wants to do it and if this user has not already used this method 4 times (you need to keep track of the number of times the user has checked sentence palindromes and keep asking until this number has been reached or the user decides not to check sentence palindromes anymore), then:
Ask the user for the name of a file where sentences are written; and then
Call the isSentencePalindrome method on each of the (3) strings in the file.
Keep asking if the user wants to keep using it.
4/ Otherwise, print “Thank you! Good bye!”.
Note: We provide the code for reading in a file.
A lot of code is already given to you. Please keep it as is and implement what is missing, where prompted.
Requirements:
1/ The method isSentencePalindrome should be implemented using a for loop.
4/ In the main method in the area reserved for Activity 3, use a while loop to keep asking the user if s/he wants to keep using the isSentencePalindrome method.
3/ Some code has been prepared for you, so you just have to complete it without modifying any of the code that is already in the file Lab5Spring16.java and do make sure that you write your code where prompted (carefully read all comments in the file).
Instructions:
Write the pseudocode of your algorithm in the docx file named “yourLastName-yourFirstName-lab5.docx” that you started for Activity 1.
Modify the code provided in Lab5Spring16.java where prompted (and only where prompted) to make sure that the code follows the above instructions.
Note: inability to follow instructions will be penalized by 10 points.
Now, here is what you have to turn in:
The modified java file called Lab5Spring16.java.
The docx file in which you have described your pseudocode (= your algorithm).
isSentencePalindrome
Your method is as follows:
Name: isSentencePalindrome
Parameter: a String, let’s call it str
Returns: a boolean – true if str is a sentence palindrome, false otherwise
How to use isSentencePalindrome?
From within the main (where it says Activity 3), here is what you have to implement:
1/ Ask the user for his/her name
2/ Ask the user, using his/her name, whether or not s/he wants to check whether a string is a sentence palindrome
3/ If the user wants to do it and if this user has not already used this method 4 times (you need to keep track of the number of times the user has checked sentence palindromes and keep asking until this number has been reached or the user decides not to check sentence palindromes anymore), then:
Ask the user for the name of a file where sentences are written; and then
Call the isSentencePalindrome method on each of the (3) strings in the file.
Keep asking if the user wants to keep using it.
4/ Otherwise, print “Thank you! Good bye!”.
Explanation / Answer
Lab5Spring16.java
package assign3;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.util.Scanner;
/**
* Lab5Spring16 is used to check the given sentences are palindrome or not
*
* @author yourName
*
*/
public class Lab5Spring16 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
Scanner myfile = null;
// Accepting user name from user
System.out.println("Please enter your name:");
String userName = sc.next();
String choice = "";
int track = 0;
do {
System.out.println("Hi " + userName
+ " you want to use isSentencePalindrome method");
choice = sc.next();
if (choice.equalsIgnoreCase("y") || choice.equalsIgnoreCase("yes")) {
try {
System.out.println("Please enter the file name:");
String fileName = sc.next();
// Please make sure you haven given the exact path and file
// of extension .txt
myfile = new Scanner(
new FileReader("C:\Users\Penchal\Desktop\"
+ fileName + ".txt"));
// Reading line by line form file
while (myfile.hasNext()) {
String sentense = myfile.nextLine();
String combined = sentense.replaceAll("[\W]", "");
combined = combined.toLowerCase();
// Checking whether user exceeded the limit or not
if (track < 4) {
boolean flag = isSentencePalindrome(combined);
if (flag) {
System.out.println("The sentence '" + sentense
+ "' in file is palindrome");
} else {
System.out.println("The sentence '" + sentense
+ "' in file is not a palindrome");
}
} else {
System.out.println("Hi " + userName
+ " you exceeded the limit " + track
+ " for using the method");
System.out.println("Thank you! Good bye!");
System.exit(0);
}
track++;
}
myfile.close();
} catch (FileNotFoundException fnfe) {
System.out.println("File Not Found:");
System.exit(0);
}
}
} while (choice.equalsIgnoreCase("y") || choice.equalsIgnoreCase("yes"));
System.out.println("Thank you! Good bye!");
sc.close();
}
/**
* Method to check weather the given string is palindrome or not
*
* @param str
* @return isPlaindrome
*/
public static boolean isSentencePalindrome(String str) {
boolean isPlaindrome = false;
String reverse = "";
int length = str.length();
// populating reverse string from combined
for (int i = length - 1; i >= 0; i--) {
reverse = reverse + str.charAt(i);
}
// Comparing the two strings
if (str.equals(reverse)) {
return isPlaindrome = true;
} else {
return isPlaindrome;
}
}
}
Output:
Please enter your name:
user
Hi user you want to use isSentencePalindrome method
y
Please enter the file name:
sentences
The sentence 'Straw arts' in file is palindrome
The sentence 'Liril' in file is palindrome
The sentence 'kumar' in file is not a palindrome
Hi user you want to use isSentencePalindrome method
y
Please enter the file name:
sentences
The sentence 'Straw arts' in file is palindrome
Hi user you exceeded the limit 4 for using the method
Thank you! Good bye!
Pseudocode:
Start
READ user name
DO
READ choice to use method or not
IF choice y or yes
READ the file name
WHILE file has next line
Remove all spaces in the sentence
IF track < 4
check palindrome or not
store the incoming string char by char in another tempstring
check the incomingstring and tempstring are equal
IF plaindrome
Display as palindrome
ELSE
Display as not palindrome
END IF
ELSE
Display as exceeded limit
ENDIF
END WHILE
ELSE exit
END IF
WHILE choice equal to yes or y
END DO WHILE
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.