Answered step by step
Verified Expert Solution
Question
1 Approved Answer
Please write in JAVA import static org.junit.Assert.*; import org.junit.After; import org.junit.Before; import org.junit.Test; import java.util.Random; import java.io.*; import java.util.*; import java.util.zip.CRC32; public class P2J3Test {
Please write in JAVA
import static org.junit.Assert.*; | |
import org.junit.After; | |
import org.junit.Before; | |
import org.junit.Test; | |
import java.util.Random; | |
import java.io.*; | |
import java.util.*; | |
import java.util.zip.CRC32; | |
public class P2J3Test { | |
private static final int SEED = 12345; | |
private static final int ROUNDS = 1000; | |
@Test public void testPancakeScramble() throws IOException { | |
// Explicit test cases | |
assertEquals("", P2J3.pancakeScramble("")); | |
assertEquals("alu", P2J3.pancakeScramble("lua")); | |
assertEquals("heya", P2J3.pancakeScramble("yeah")); | |
assertEquals("eoeawsm", P2J3.pancakeScramble("awesome")); | |
assertEquals("enisrtpocmue cec", P2J3.pancakeScramble("computer science")); | |
// Testing with War and Peace | |
CRC32 check = new CRC32(); | |
BufferedReader fr = new BufferedReader( | |
new InputStreamReader(new FileInputStream("warandpeace.txt"), "UTF-8") | |
); | |
String line = fr.readLine(); | |
while(line != null) { | |
String result = P2J3.pancakeScramble(line); | |
check.update(result.getBytes()); | |
line = fr.readLine(); | |
} | |
fr.close(); | |
assertEquals(1606800991L, check.getValue()); | |
} | |
@Test public void testReverseVowels() throws IOException { | |
// Explicit test cases | |
assertEquals("", P2J3.reverseVowels("")); | |
assertEquals("X", P2J3.reverseVowels("X")); | |
assertEquals("Au", P2J3.reverseVowels("Ua")); | |
assertEquals("cDfghklM", P2J3.reverseVowels("cDfghklM")); | |
assertEquals("LOL", P2J3.reverseVowels("LOL")); | |
assertEquals("Jova, Pythan, C", P2J3.reverseVowels("Java, Python, C")); | |
assertEquals("Wuuleemoolaa", P2J3.reverseVowels("Waaloomeeluu")); | |
assertEquals("Ent, uat, boa, oka", P2J3.reverseVowels("Ant, oat, boa, uke")); | |
assertEquals("Stix nix hix pix", P2J3.reverseVowels("Stix nix hix pix")); | |
assertEquals("UoIeAxxxuOiEa", P2J3.reverseVowels("AeIoUxxxaEiOu")); | |
assertEquals("Lettor Y as not i vewel", P2J3.reverseVowels("Letter Y is not a vowel")); | |
// Testing with War and Peace | |
CRC32 check = new CRC32(); | |
BufferedReader fr = new BufferedReader( | |
new InputStreamReader(new FileInputStream("warandpeace.txt"), "UTF-8") | |
); | |
String line = fr.readLine(); | |
while(line != null) { | |
String result = P2J3.reverseVowels(line); | |
check.update(result.getBytes()); | |
line = fr.readLine(); | |
} | |
fr.close(); | |
assertEquals(3844894811L, check.getValue()); | |
} | |
} |
make sure the code can pass the test, will give up the vote. Thanks
One last batch of transitional problems taken from the instructor's Python graded labs. Only three methods to write this time, though. The JUnit test for these labs uses the warandpeace.txt text file as source of data, so please ensure that this text file has been properly copied into your course labs project folder. This text file, same as any other data files you would use, does not show up anywhere in the Blue) project screen, even though it is part of the project directory that contains the Java source code for the solutions and the unit tests. public static String pancakeScramble(String text) This nifty little problem is taken from the excellent Wolfram Challenges problem site where you can also see examples of what the result should be for various arguments. Given a text string, construct a new string by reversing its first two characters, then reversing the first three characters of that, and so on, until the last round where you reverse your entire current string. This problem is an exercise in Java string manipulation. For some mysterious reason, the Java String type does not come with a reverse method. The canonical way to reverse a Java string str is to first convert it to mutable StringBuilder, reverse its contents, and convert the result back to an immutable string, that is, str = new StringBuilder(str).reverse().toString(); A bit convoluted, but does what is needed without fuss or muss. Maybe one day the Java strings will come with the reverse method built in, just like the string data types of all sensible programming languages. Or at least have that available as a utility somewhere in the standard library. public static String reverseVowels (String text) Given a text string, create and return a new string of same length where all vowels have been reversed, and all other characters are kept as they were. For simplicity, in this problem only the characters aeiouAEIOU are considered vowels, and y is never a vowel. For example, given the text string "computer science", this method would return "cempetir sceunco". Furthermore, to make this problem more interesting and the result look more palatable, this method must maintain the capitalization of vowels based on the vowel character that was originally in the position that each new vowel character is moved into. For example, "Ilkka Markus" should become "Ulkka Markis" instead of "ulkka MarkIs". Use the handy utility methods in the Character wrapper class to determine whether some particular character is in upper- or lowercase, and convert a character to upper- or lowercase as neededStep by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started