Question: You are to implement these four methods so that they behave exactly as specified. When you do this, you will pass the four tests contained
You are to implement these four methods so that they behave exactly as specified. When you do this, you will pass the four tests contained in PS5LibraryTests and my generateText method will work as specified. Do not deviate from the specifications; do not modify generateText; do not modify the four tests.
In addition to implementing the four methods, you are to add additional test methods to PS5LibraryTests. The tests that it currently contains are only a starting point. You goal should be to thoroughly exercise all aspects of each method's specification, including the cases where exceptions are thrown.
You will benefit from adding these test cases in two ways. By thoroughly testing your methods, you are more likely to pass the test cases.
public String scannerToString(Scanner s) {
String result = "";
while(s.hasNext()) {
result += s.nextLine() + " ";
}
return result;
}
String chooseSubstring(String text, int len, Random r) {
if(len < 0 || len > text.length()) {
throw new IllegalArgumentException("Invalid Length");
}
int startIndex = r.nextInt(text.length() - len + 1);
return text.substring(startIndex, startIndex + len);
}
ArrayList getCharsThatFollowPattern(String text, String pattern) {
ArrayList list = new ArrayList<>();
int start = 0;
while(true) {
int index = text.indexOf(pattern, start);
if(index == -1) {
break;
}
if(index + pattern.length() == text.length()) {
// tail sequence
break;
}
list.add(text.charAt(index + 1));
start = index + 1;
}
return list;
}
char pickCharThatFollowsPattern(String text, String pattern, Random r) {
ArrayList tailChars = getCharsThatFollowPattern(text, pattern);
return tailChars.get(r.nextInt(tailChars.size()));
}
/////test/// --- add test case ! should be to thoroughly exercise all aspects of each method's specification, including the cases where exceptions are thrown.import static org.junit.Assert.*; import java.util.ArrayList; import java.util.NoSuchElementException; import java.util.Random; import java.util.Scanner; import org.junit.Test;
public class PS5LibraryTests { /** * This checks that the last line of the result ends with a newline, even if the last line in the scanner didn't. */ @Test public void testScannerToString1 () { assertEquals("This is a test ", PS5Library.scannerToString(new Scanner("This is a test"))); }
/** * This illustrates how to test that an exception is thrown when one is supposed to be thrown. If * pickCharThatFollowsPattern doesn't thrown the right kind of exception, the test will fail. */ @Test(expected = NoSuchElementException.class) public void testPickCharThatFollowsPattern () { PS5Library.pickCharThatFollowsPattern("hello", "o", new Random()); }
/** * This illustrates a way to do tests of methods that have a randomized behavior. When we ask for a randomly chosen * substring of length 4 of "abcde", about half the time we should get "abcd" and about half the time we should get * "bcde". So we call chooseSubstring 1000 times and count how many times we get each possibility. (If we get * anything else back, we immediately fail the test case.) Then we assert that we got each "about" half the time. It * is possible for a correct implementation to fail this test if we get extremely unlucky, but that is extremely * unlikely. */ @Test public void testChooseSubstring () { Random rand = new Random(); int abcd = 0; int bcde = 0;
for (int i = 0; i < 1000; i++) { String substring = PS5Library.chooseSubstring("abcde", 4, rand); if (substring.equals("abcd")) { abcd++; } else if (substring.equals("bcde")) { bcde++; } else { fail(); } }
assertTrue(400 <= abcd && abcd <= 600 && 400 <= bcde && bcde <= 600); }
/** * This illustrates how to make assertions about ArrayLists. */ @Test public void testGetCharsThatFollowPattern () { ArrayList list = new ArrayList(); list.add('b'); list.add('b'); assertEquals(list, PS5Library.getCharsThatFollowPattern("abababa", "aba")); }
}
In the new project you will find a package named books containing the complete text of two 19th century novels, Pride and Prejudice and A Tale of Two Cities. You will also find a package called cs1410 containing PS5Library.java and PS5LibraryTests.java. The source files each contain compilation errors because neither is complete.
In the PS5Library class I have provided a method, generateText, that implements the randomized text generation algorithm described above. It takes three parameters: a Scanner that should contain the complete text of a book, an integer that is the level of the analysis you wish to perform, and an integer that is the amount of text that you want to generate. If all of the parameters are valid, it returns randomly generated text of the requested length. However, if the level is greater than or equal to the length of the text, or if the level or length is negative, the method throws an IllegalArgumentException.
In addition, I have provided a main method that makes use of generateText to produce output based on Pride and Prejudice. The purpose of the main method is to illustrate the use of generateText. It is not essential to completing the assignment, so feel free to play around with it.
In the PS5LibraryTests class I have given one test for each of the four methods that you will be implementing. You will also be adding more test cases to the class.
This assignment has two parts. Be sure to complete both of them!
Part 1
My generateText method depends on four public static methods that are currently missing from PS5Library. Your first job is to implement and test them. They are:
scannerToString, which takes a Scanner as its parameter and returns a String. The returned string consists of all the characters in the scanner in their original order, including the newlines. The last line (assuming there are any lines) should always end with a newline, even if it didn't in the original text. For example, scannerToString("This is a test") should return "This is a test ". A convenient way to implement this method is to build up the result by reading one line at a time from the Scanner. For much improved efficiency when dealing with Scanners containing complete books, consider using a StringBuilder object to construct the result. You can look at my generateText method for an example of its use.
chooseSubstring, which takes a String text, an int length, and a random number generator as its parameters. It should use the random number generator to return a randomly chosen substring of text that has the specified length. If length is either negative or greater than the length of text, the method should throw an IllegalArgumentException. For example, chooseSubstring("abcde", 4, new Random()) should return "abcd" about half the time and "bcde" about half the time. A string of length n will contain n+1-m different substrings of length m, assuming n m. These substrings start at indexes ranging from 0 to n-m. There is a convenient method provided by the Random parameter that you can use to choose a random index.
getCharsThatFollowPattern, which takes a String text and a String pattern as parameters, and returns an ArrayList
pickCharThatFollowsPattern, which takes a String text, a String pattern, and a random number generator as parameters. It should randomly choose a non-tail occurrence of the pattern in the text, returning the character that immediately follows that occurrence of the pattern. If there are no non-tail occurrences of the pattern in the text, the method should throw a NoSuchElementException. For example, pickCharThatFollowsPattern("They are here", "he") should return 'y' or 'r' with equal probability. You should use your getCharsThatFollowPattern method in your implementation of this method. If you don't, you'll be wasting a lot of time.
You are to implement these four methods so that they behave exactly as specified. When you do this, you will pass the four tests contained in PS5LibraryTests and my generateText method will work as specified. Do not deviate from the specifications; do not modify generateText; do not modify the four tests.
In addition to implementing the four methods, you are to add additional test methods to PS5LibraryTests. The tests that it currently contains are only a starting point. You goal should be to thoroughly exercise all aspects of each method's specification, including the cases where exceptions are thrown.
You will benefit from adding these test cases in two ways. First, part of your grade will be based on the test cases that you add. Second, by thoroughly testing your methods, you are more likely to pass the test cases that we will use when we grade.
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
