What\'s in a word? Use your favorite search engine to research the definition of
ID: 3902569 • Letter: W
Question
What's in a word? Use your favorite search engine to research the definition of a palindrome. For this assignment, develop a program that determines if words are palindromes' You must use the following package name for developing the java program: package biclyelerentalstorepackage There must be a java source code files that has the following names: . PalindromeFileExceptionMainClass.java Contains the main PalindromeFileExceptionMainClass. Input File This assignment requires you to develop a program that reads a file that contains candidate palindrome words. Your program must read a word at a time from the file, display the word and whether or not the word is a palindrome. See the attached output that you must match exactly regarding the format of the output and the information displayed. For this assignment do not worry about error checking any of the inputs from the file (like whether or not the line contains a number. The programmer will create the file that contains candidate words with a text editor Each line will have one candididate word. The words in the file may or may not be palindromes. The file must contain test words, including a trivial case words that verify that your program is working correctly. Not using words that verifying that all the program is working correctly will cost you points In a sense, the driver concept for testing the program is the words you create in the words.txt file that the progra m reads. So the words.txt file will be a part of the grading evaluaion as to whether the words.txt tested the program adequately. Use google to look up palindromes to put in the file and also inchde non-palindrome words in the file. The file must contain at least 5 palindromes and at least 5 non-palindromes.Explanation / Answer
Here is the completed code for this problem. Comments are included, go through it, learn how things work and let me know if you have any doubts. Thanks
// PalindromeFileExceptionMainClass.java
package bicyclerentalstorepackage;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.logging.Level;
import java.util.logging.Logger;
public class PalindromeFileExceptionMainClass {
public static void main(String[] args) {
/**
* make sure you have words.txt file in a folder named words in the same
* project directory, if not detecting, append a File.seperator before
* "words", if still not detecting, provide complete file path (like
* C://Documents//words//words.txt)
*/
File fileSafePathfileName = new File("words" + File.separator
+ "words.txt");
FileInputStream fileInputStreamObj = null;
BufferedReader bufferedReaderObj = null;
try {
fileInputStreamObj = new FileInputStream(fileSafePathfileName);
bufferedReaderObj = new BufferedReader(new InputStreamReader(
fileInputStreamObj));
String wordStrObj;
// required counter variables
int palindromeCount = 0;
int nonPalindromeCount = 0;
do {
wordStrObj = bufferedReaderObj.readLine();
// breaking the loop if the wordStrObj is null
if (wordStrObj == null) {
break;
}
/**
* setting up a loop, which checks each character pair from
* start and end one by one and determine if it is palindrome or
* not
*/
int startIndex = 0;
int endIndex = wordStrObj.length() - 1;
String tempWord = wordStrObj.toLowerCase();
boolean isPalindrome = true;//assuming word is palindrome
while (startIndex <= endIndex) {
if (tempWord.charAt(startIndex) != tempWord
.charAt(endIndex)) {
//mismatch found, not a palindrome
isPalindrome = false;
break;//breaking inner loop
}
//moving start index to next character
startIndex++;
//moving end index to previous character
endIndex--;
}
if (isPalindrome) {
//palindrome
palindromeCount++;
System.out.println(wordStrObj + " is a palindrome");
} else {
//not a palindrome
nonPalindromeCount++;
System.out.println(wordStrObj + " is not a palindrome");
}
} while (true);
/**
* Displaying counts, in a properly formatted output
*/
System.out.printf("%20s : %d ", "Palindrome Count",
palindromeCount);
System.out.printf("%20s : %d ", "Non-Palindrome Count",
nonPalindromeCount);
} catch (FileNotFoundException e) {
Logger.getLogger(PalindromeFileExceptionMainClass.class.getName())
.log(Level.SEVERE, null, e);
} catch (IOException e) {
Logger.getLogger(PalindromeFileExceptionMainClass.class.getName())
.log(Level.SEVERE, null, e);
} finally {
try {
bufferedReaderObj.close();
fileInputStreamObj.close();
} catch (IOException e) {
Logger.getLogger(
PalindromeFileExceptionMainClass.class.getName()).log(
Level.SEVERE, null, e);
}
}
}
}
/*words.txt*/
hello
world
race
malayalam
mom
dad
I
heh
dEeD
/*OUTPUT*/
hello is not a palindrome
world is not a palindrome
race is not a palindrome
malayalam is a palindrome
mom is a palindrome
dad is a palindrome
I is a palindrome
heh is a palindrome
dEeD is a palindrome
Palindrome Count : 6
Non-Palindrome Count : 3
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.