Question: Create a Main class named PrimesMain with a main method that instantiates this class calls the go method. Add the following lines of code following

Create a Main class named PrimesMain with a main method that instantiates this class calls the go method.

Add the following lines of code following the class header: List list = new ArrayList<>();

Click on the Main class name, press Ctrl/Cmd-n, and then click on Test.

Make sure Testing library: JUnit5 and Class name: PrimesMainTest If Fix it appears, click on it.

Click on OK button. If the Assertions class is undefined, click on the red light bulb and click on Add library JUnit 5.8.1 to classpath.

On the line after class PrimesMainTest, enter final static List FIFTEEN_PRIME_BOOLEANS = List.of(true,true,false,true,false,true,false,false,false,true,false,true,false,false,false,false); final static List PRIMES15 = List.of 2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47); final static PrimesMain main = new PrimesMain(); Enter this code in the isPrimeTest method: for (int i = 2; i < FIFTEEN_PRIME_BOOLEANS.size(); i++) { boolean actual = main.isPrime(i); System.out.printf("%d: %b, ", i, actual); assertEquals(FIFTEEN_PRIME_BOOLEANS.get(i - 2), actual); } System.out.println(); }

All imports for assert methods should reference the org.junit.jupiter.api.Assertions class.

The reference to isPrime is undefined. Switch to the main class and enter the following code: boolean isPrime(Integer number) { return IntStream.rangeClosed(2, number / 2).noneMatch(i -> number % i == 0); }

This code creates a Stream of sequential integers starting with 2 and ending with the last integer less than the number to be tested divided by 2, tests each of those numbers to see if its equally divisible by 2, and stops when it finds one. If one is found, it returns true, else returns false.

Run the test until it runs successfully.

Next enter the following test: Void findNPrimesTest() { List actual = main.findNPrimes(15); System.out.println(actual); assertIterableEquals(PRIMES15, actual); }

The reference to findNPrimes is undefined. Switch to the main class and enter the following code: List findNPrimes(Integer n) { return Stream.iterate(2, e -> e + 1) .filter(this::isPrime) .limit(n) .toList(); } This code uses the IntStream generate function to load a list with n primes.

Now the main and test classes should compile. Click on the small green triangle to run this test. The test will pass.

This process is called Test-Driven Development cycle should o Add a test. o The test will not compile. o Write the simplest main class code that passes the test. o Rerun all tests. o Repeat this cycle adding additional test to verify the one method being tested.

Now run all tests and they should pass. Again, test-driven development cycle is: o Write a test o Write the simplest code so the test will pass. o Add additional tests to test edge cases and exceptions.

Now, create another class names GradesMain and an associated test class named GradesMainTest.

In the test class, enter the following code: private final static GradesMain GRADES_MAIN = new GradesMain(); record Student(String name, List grades){} private final static List results = List.of( new Result("Tom", 89.5, "A"), new Result("Ted", 89.25, "B"), new Result("Bill", 100.0, "A"), new Result("Phil", 10.0, "F"), new Result("Lisa", 84.5, "B"), new Result("John", 87.5, "B"), new Result("Joe", 94.75, "A"), new Result("Jason", 85.75, "B") ); Result findResult( String name) { return results.stream() .filter(result -> result.name().equals(name)) // Search results list for matching name .findFirst().orElse( null); // Return match or null } This code creates a table of the students average and letter grade.

Now enter the following code to test the methods that calculates the average of each students grades and the associated letter grade. @Test void calcAverageAndFindLetterGradeTest() { for ( GradesMain.Student student : GRADES_MAIN.students ) { double average = GRADES_MAIN.calcAverage(student); // Calculate a student's average String letterGrade = GRADES_MAIN.findLetterGrade(average); // Convert average grade to a letter grade Result result = findResult(student.name()); // Look up the expected student grade results System.out.println(result); if (result != null) { assertEquals(result.average, average); // Test the average is as expected assertEquals(result.grade, letterGrade); // Test the letter grade is as expected } else { fail(); } } }

This code tests both the average and letter grade methods in the main class. I recommend you comment out the letter grade code until you get the average code working. In the main class, enter record Student(String name, List grades){} public List students; public GradesMain() { students = List.of( new Student("Tom", List.of(95, 88, 83, 92)), // Test that 89.5 rounds to an A new Student("Ted", List.of(95, 88, 82, 92)), // Tests that 89.25 rounds down to B new Student("Bill", List.of(100, 100, 100, 100)), // Tests 3-digit average new Student("Phil", List.of(10, 10, 10, 10)), // Tests 10 is not confused with 100 new Student("Lisa", List.of(90, 85, 66, 97)), new Student("John", List.of(89, 88, 87, 86)), new Student("Joe", List.of(95, 98, 94, 92)), new Student("Jason", List.of(82, 88, 84, 89)) ); }

This code creates a list of students and their list of grades.

Now enter the code to be tested double calcAverage( Student student ) { return student.grades().stream() .reduce(0, Integer::sum) * 1.0 // Sum a student's grade / student.grades().size(); // Divide by number of grades for student }

This code creates a stream of a students grades and returns the average.

Test and correct your code until it works correctly.

Uncomment the test code which tests the code that determines the letter grade.

Enter this code in the main class: String findLetterGrade( double average) { return switch (( int ) (Math.round(average) / 10)) { case 9, 10 -> "A"; case 8 -> "B"; case 7 -> "C"; case 6 -> "D"; default -> "F"; }; }

This code uses an enhanced switch lookup the corresponding letter grade.

Repeat the tests until they pass. In the test class, enter this Java method to the creation of the final grade summary: @Test void finalGradeSummaryTest() { Map summary = GRADES_MAIN.calcFinalGradeSummary(); // Summarize letter grades System.out.println(summary); assertEquals("{A=3, B=4, F=1}", summary.toString()); // Summary as expected }

Since calcFinalGradeSummary does not compile, enter this code in the main class: Map calcFinalGradeSummary() { TreeMap summary = new TreeMap<>(); students.stream() .map(this :: calcAverage) // Calculate the average of each student's grades .map(this :: findLetterGrade) // Find each student's letter grade .toList().forEach(g -> summary.put(g, summary.getOrDefault(g, 0) + 1)); // Summarize by letter grade return summary; }

This code uses a TreeMap to summarize the letter grades.

Repeat all tests until they all pass.

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!