JAVA QUESTION: The grade level is calculated with the following formula: 0.39 (t
ID: 3822615 • Letter: J
Question
JAVA QUESTION:
The grade level is calculated with the following formula: 0.39 (total words/total sentence) + 11.8 (total syllables/total words) - 15.59 Problem. Using this formula and your knowledge of how to input and output text files, write a program that will take in a text file and return the number of words in the text, the number of sentences in the text, the number of syllables in the text, and the Flesch-Kincaid Grade Level. Please use the three files provided (gettysburg.txt, declaration.txt, and green_eggs_and_ham.txt. Assumption: the number of syllables for each word is the number of letters in the word divided by three. Use integer math to calculate the number of syllables for each word. Constraint: This program requires two classes: GradeLevel and GradeLevelTest. GradeLevel will be the repository for all methods, other than main, used in the grade level calculations. Examples: processLine, wordCount, sentenceCount, numberSyllables, , , . GradeLevelTest will have the main method. Additional Requirement: Use at least one Exception Handling Mechanism.Explanation / Answer
The following files accept as input the filenames. The files must be present in the same directory as the java files.
To run save GradeLevel.java and GradeLevelTest.java in your desired directory. Save the three provided files in the same directory. And then run the following commands
$ javac GradeLevelTest.java
$ javac GradeLevel.java
$ java GradeLevelTest
Sample output:
Enter test file name: gettysburg.txt
Test file: gettysburg.txt
------------------------------------
Total Sentences :1
Total Words :10
Total Syllables :10
Grade Level :0.11000000000000121
GradeLevel.java:
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.io.FileNotFoundException;
public class GradeLevel {
private
String filename;
int wordCount;
int sentenceCount;
int syllableCount;
double gradeLevel;
public
GradeLevel(String f) {
filename = f;
wordCount=sentenceCount=syllableCount=-1;
try {
BufferedReader br = new BufferedReader(new FileReader(f));
String line;
wordCount=sentenceCount=syllableCount=0;
while ((line = br.readLine()) != null) {
String[] sentences = line.split("\.");
sentenceCount += sentences.length;
for(int i=0; i<sentences.length; i++) {
String[] words = sentences[i].split("\s+");
wordCount += words.length;
for(int j=0; j<words.length; j++) {
syllableCount += words[j].length()/3;
}
}
}
gradeLevel = 0.39 * (double)wordCount/(double)sentenceCount + 11.8 * (double)syllableCount/(double)wordCount - 15.59;
} catch(FileNotFoundException fnfe) {
System.out.println("The file " + f + " does not exist.");
} catch(IOException ioe) {
System.out.println("Exception reading file");
ioe.printStackTrace();
}
}
int getWordCount() {
return wordCount;
}
int getSentenceCount() {
return sentenceCount;
}
int getSyllableCount() {
return syllableCount;
}
double getGradeLevel() {
return gradeLevel;
}
}
GradeLevelTest.java:
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class GradeLevelTest {
public static void main(String []args){
try {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
System.out.print("Enter test file name: ");
String filename = br.readLine();
GradeLevel grade_level = new GradeLevel(filename);
if(grade_level.getSentenceCount() > -1) {
System.out.println("Test file: " + filename);
System.out.println("------------------------------------");
System.out.println("Total Sentences : " + grade_level.getSentenceCount());
System.out.println("Total Words : " + grade_level.getWordCount());
System.out.println("Total Syllables : " + grade_level.getSyllableCount());
System.out.println("Grade Level : " + grade_level.getGradeLevel());
}
} catch(Exception e) {
e.printStackTrace();
}
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.