Question: Can I please get some help writing these methods import java.util.Scanner; import java.io.File; public class BookMain { /** * DO NOT MODIFY VARIABLE DECLARATIONS */
Can I please get some help writing these methods
import java.util.Scanner;
import java.io.File;
public class BookMain {
/**
* DO NOT MODIFY VARIABLE DECLARATIONS
*/
LetterTally letterData;
WordTally wordData;
SentenceTally sentenceData;
/**
* DO NOT MODIFY THIS METHOD.
*/
public BookMain() {
letterData = new LetterTally();
wordData = new WordTally();
sentenceData = new SentenceTally();
}
/**
* DO NOT MODIFY THIS METHOD.
*
* You are still allowed to change the input file name to see the output for
* different files.
*
* @param args
* @throws Exception
*/
public static void main(String[] args) throws Exception {
BookMain bm = new BookMain();
Scanner input = new Scanner(new File("PrideAndPrejudice.txt"));
bm.readHeader(input);
bm.analyzeBookText(input);
System.out.println(bm.statToString());
input.close();
}
/**
* This method analyzes the book text data. Complete this method.
*
* @param input
*/
public void analyzeBookText(Scanner input) {
//
// WRITE YOUR CODE HERE TO PROCESS THE BOOK DATA
//
}
/**
* DO NOT MODIFY THIS METHOD. YOU MAY USE IT TO SEE THE ANALYSIS RESULTS.
*
* @param letterData
* @param wordData
* @param sentenceData
*/
public String statToString() {
String s = "";
s += "Raw letter count: " + letterData.totalCount() + " ";
s += "Letter entropy: " + letterData.computeEntropy() + " ";
s += "Raw word count: " + wordData.getRawCount() + " ";
s += "Unique word count: " + wordData.getUniqueCount() + " ";
s += "Longest word: " + wordData.longestWord() + " ";
s += "Average word length: " + wordData.avgWordLength() + " ";
s += "Sentence count: " + sentenceData.getCount() + " ";
s += "Average sentence length: " + sentenceData.avgSentenceLength() + " ";
return s;
}
/**
* DO NOT MODIFY THIS METHOD. IT EATS UP THE HEADER LINES
*
* @param input
*/
private void readHeader(Scanner input) {
int nHeaderLine = 31; // a fixed number of header lines for the book.
int nCurLine = 0;
while (input.hasNext() && nCurLine < nHeaderLine) {
input.nextLine();
nCurLine++;
if (nCurLine == nHeaderLine)
break;
}
}
}
public class LetterTally {
/** Declare instance variables. */
/**
* Default constructor. Initialize the instance variables in this method.
*
*/
public LetterTally() {
throw new UnsupportedOperationException("replace with your implementation");
}
/**
* This method returns the number of total letters in the book.
*
* @return the total number of letters in the book
*/
public int totalCount() {
throw new UnsupportedOperationException("replace with your implementation");
}
/**
* This method returns the number of times a letter appear in the book
*
* @param ch
* - the letter of interest
*
* @return
*/
public int freqChar(char ch) {
throw new UnsupportedOperationException("replace with your implementation");
}
/**
* This method computes the entropy of the letters. To compute the entropy, for
* each letter, compute the ratio (say it p) of the frequency count of the
* letter to the total frequency count of all letters. Then, compute the
* following value: p * Math.log (1 / p). Return the sum of the values, rounded
* to the nearest integer value.
*
* @return
*/
public int computeEntropy() {
throw new UnsupportedOperationException("replace with your implementation");
}
}
import static org.junit.Assert.*;
import java.util.Scanner;
import org.junit.Before;
import org.junit.Test;
public class PublicTests {
BookMain bm;
@Before
public void testA() {
String s = "In addition, it made us tolerant of each other's " + "yarns--and even convictions. ";
bm = new BookMain();
Scanner input = new Scanner(s);
bm.analyzeBookText(input);
}
@Test
public void testTotalCount() {
int totalCount = bm.letterData.totalCount();
assertEquals(61, totalCount);
}
@Test
public void testFreqChar() {
assertEquals(4, bm.letterData.freqChar('s'));
}
@Test
public void testComputeEntropy() {
int entropy = bm.letterData.computeEntropy();
assertEquals(3, entropy);
}
@Test
public void testGetRawCount() {
int count = bm.wordData.getRawCount();
assertEquals(13, count);
}
@Test
public void testUniqueCount() {
int count = bm.wordData.getUniqueCount();
assertEquals(13, count);
}
@Test
public void testAvgWordLength() {
String avgWL = bm.wordData.avgWordLength();
assertEquals("4.69", avgWL);
}
@Test
public void testLongestWord() {
int lenLongestWord = bm.wordData.longestWord();
assertEquals(11, lenLongestWord);
}
@Test
public void testOneSentence() {
int nSentences = bm.sentenceData.getCount();
assertEquals(1, nSentences);
}
@Test
public void testMultipleSentences() {
BookMain bm2 = new BookMain();
String s =
"Theodor Seuss Geisel (March 2, 1904--September 24, 1991) was an American " +
"author, political cartoonist, poet, animator, book publisher, and artist, " +
"best known for authoring more than 60 children's books under the pen name" +
"Doctor Seuss. His work includes several of the most" +
"popular children's books of all time, selling over 600 million copies and " +
"being translated into more than 20 languages by the time of his death.";
Scanner input = new Scanner(s);
bm2.analyzeBookText(input);
assertEquals(2, bm2.sentenceData.getCount());
}
@Test
public void testAveSentenceLength() {
String nAvgSentenceLen = bm.sentenceData.avgSentenceLength();
assertEquals("12.00", nAvgSentenceLen);
}
@Test
public void testCleanWord() {
String a = "don't";
assertEquals("dont", WordLib.cleanWord(a));
String b = "single-dash";
assertEquals("single-dash", WordLib.cleanWord(b));
}
@Test
public void testSplitDash() {
String a = "yarns--and";
String[] arr = WordLib.splitDash(a);
assertEquals("yarns", arr[0]);
assertEquals("and", arr[1]);
String b = "single-dash";
String[] arr2 = WordLib.splitDash(b);
assertEquals("single-dash", arr2[0]);
assertEquals("", arr2[1]);
}
@Test
public void testFreqWord() {
assertEquals(1, bm.wordData.freqWord(new Word("each")));
assertEquals(0, bm.wordData.freqWord(new Word("there")));
}
}
public class SentenceTally {
/** Declare instance variables. */
/**
* Default constructor. Initialize the instance variables in this method.
*
*/
public SentenceTally() {
throw new UnsupportedOperationException("replace with your implementation");
}
/**
* This method returns the average number of words in a sentence. The value has
* to be formatted to a String with the following statement:
*
* Assuming that average is the variable that contains the average number of
* words in a sentence,
*
* String s = String.format("%.2f", average);
*
* @return the average number of words in the sentences in the book in a String
* format
*/
public String avgSentenceLength() {
throw new UnsupportedOperationException("replace with your implementation");
}
/**
* This method returns the number of sentences in the book.
*
* @param token
*
* @return the number of sentences in the book
*/
public int getCount() {
throw new UnsupportedOperationException("replace with your implementation");
}
}
public class Word {
/** Declare instance variables. */
/**
* Default constructor. Initialize the instance variables in this method.
*
*/
public Word(String w) {
throw new UnsupportedOperationException("replace with your implementation");
}
/** You may add more methods needed to complete the program */
}
public class WordLib {
/**
* This method takes a token (a space-separated letter sequence in the book
* data) and return a new string that does not contain any of the symbols listed
* below. That is, it removes the symbols from the word.
*
* Symbols to be removed: , . ? ; ! : ' " ( ) _ \u201C \u201D
*
* The \u201C and \u201D are the left and right double quotation marks in
* Unicode.
*
* @param w
* a token read from the book
* @return a string without the listed symbols
*/
public static String cleanWord(String w) {
throw new UnsupportedOperationException("replace with your implementation");
}
/**
* This method takes a token (a space-separated letter sequence in the book
* data) and return a new array of words when the token contains two words
* separated by double dashes. Assume that no token contains two double dashes
* (e.g., hello--world--houston) or has triple dashes.
*
* If "hello--world" is the input, a new array of length 2 must be returned
* where the first and the second element of the array are "hello" and "world",
* respectively. There are edge cases. For example, "Hello--" or "--World". For
* such cases, the first and/or the second element of the new array will have an
* empty string.
*
* If the input does not contain a double dash, the first array element will has
* the input token as-is and the second element will be set to an empty string.
*
* @param w
* a token read from the book
* @return an array of words
*/
public static String[] splitDash(String w) {
throw new UnsupportedOperationException("replace with your implementation");
}
}
public class WordTally {
/** Declare instance variables. */
/**
* Default constructor. Initialize the instance variables in this method.
*
*/
public WordTally() {
throw new UnsupportedOperationException("replace with your implementation");
}
/**
* This method returns the raw count of the words in the book. A word can be
* counted multiple times if it appears multiple times.
*
* @return the raw count of the words in the book
*/
public int getRawCount() {
throw new UnsupportedOperationException("replace with your implementation");
}
/**
* This method returns the unique count of the words in the book. A word must be
* counted only once when it appears multiple times.
*
* @return the unique count of the words in the book
*/
public int getUniqueCount() {
throw new UnsupportedOperationException("replace with your implementation");
}
/**
* This method returns the average length of the words used in the book. The
* average length has to be formatted in a String with the following statement:
*
* Assuming that averageLength is the variable that contains the average word
* length,
*
* String s = String.format("%.2f", averageLength);
*
* @return the average length of the words in the book in a String format
*/
public String avgWordLength() {
throw new UnsupportedOperationException("replace with your implementation");
}
/**
* This method returns the length of the longest word used in the book
*
* @return the length of the longest word in the book
*/
public int longestWord() {
throw new UnsupportedOperationException("replace with your implementation");
}
/**
* This method returns how many times a word appear in the book
*
* @param ow
* a Word object
* @return the number of occurrences of the input word
*/
public int freqWord(Word ow) {
throw new UnsupportedOperationException("replace with your implementation");
}
}
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
