Question: 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 P2J6Test {

Please write in JAVAPlease write in JAVA import static org.junit.Assert.*; import org.junit.After; import org.junit.Before; import

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 P2J6Test {
private static final int SEED = 12345;
@Test public void testSumOfDistinctCubes() {
// Explicit test cases
String b1 = "[4, 3]";
assertEquals(b1, P2J6.sumOfDistinctCubes(91).toString());
String b2 = "[5]";
assertEquals(b2, P2J6.sumOfDistinctCubes(125).toString());
String b3 = "[4, 3, 2]";
assertEquals(b3, P2J6.sumOfDistinctCubes(99).toString());
String b4 = "[7, 2]";
assertEquals(b4, P2J6.sumOfDistinctCubes(351).toString());
String b5 = "[11, 4]";
assertEquals(b5, P2J6.sumOfDistinctCubes(1395).toString());
String b6 = "[]";
assertEquals(b6, P2J6.sumOfDistinctCubes(2020).toString());
// Pseudorandom fuzz tester
CRC32 check = new CRC32();
Random rng = new Random(SEED);
int c = 1, step = 2, next = 10;
while(c > 0) {
List result = P2J6.sumOfDistinctCubes(c);
check.update(result.toString().getBytes());
c += rng.nextInt(step) + 1;
if(c > next) {
next = 2 * next;
step = 2 * step;
}
}
assertEquals(4219145223L, check.getValue());
}
private String createString(String alphabet, Random rng, int n) {
String result = "";
for(int i = 0; i
result += alphabet.charAt(rng.nextInt(alphabet.length()));
}
return result;
}
}

make sure the code can pass the test, will give up the vote. Thanks

In most of the mainstream computer science education at university level, teaching of recursion tends to nerf down this mighty blade to cut through exponential possibilities. Recursion is used mainly as a toy to simulate some linear loop to compute factorials or Fibonacci numbers. This gains nothing over the everyday alternative of ordinary for-loops, the way that any competent coder would have automatically done it anyway. This also tends to leave the students dazed and confused about the general usefulness of recursion, since from their point of view, recursion only ever seems to be used to solve toy problems in a needlessly mathematical and highfaluting manner. However, as ought to become amply evident somewhere in the third or fourth year, the power of recursion lies in its ability to branch to multiple directions one at a time, which allows a recursive method to explore a potentially exponentially large branching tree of possibilities and backtrack on failure to previous choice point until it either finds one branch that works, or alternatively collect the results from all of the working branches. In this spirit, this last transition lab uses branching recursions to solve two interesting combinatorial problems. So without any further ado, create a new class P2J6 to write the two methods in. public static List sumofDistinctCubes (int n) Determine whether the given positive integer n can be expressed as a sum of cubes of positive integers greater than zero so that all these integers are distinct. For example, n=1456 can be expressed as sum of two cubes 113 + 53. This method should return the list of these distinct integers as an object of some subtype of List that prints out as [11, 5], the elements listed in descending order. If there is no way to break the given integer n into a sum of distinct cubes, this method should return the empty list. Many integers can be broken down into a sum of cubes in several different ways. This method must always return the breakdown that is lexicographically highest, that is, starts with the largest possible working value for the first element, and then follows the same principle for the remaining elements to break down the rest of the number into a list of distinct cubes. For example, when called with n=1072, this method must return the list [10, 4, 2] instead of the list [9, 7], even though 93 + 73 = 1072 just as well. This constraint may initially look and sound scary, but you can just fuhgettaboutit, since its enforcement is trivial by arranging the loop inside the recursive method to scan the current possibilities from highest to lowest. You should again use a private helper method that accepts additional parameters that were not present in the public method. sumofDistinctCubes (int n, int C, private static boolean LinkedList soFar) This helper method receives the two extra recursion parameters c and soFar from the original method. The parameter c contains the highest integer that you are still allowed to use. Initially this should equal the largest possible integer whose cube is less than or equal to n, easily found with a while-loop. The parameter soFar contains the list of numbers that have already been taken into the list of cubes so that they will be available once the recursion has returned. The recursion has two base cases, one for success and one for failure. If n==0, the problem is solved and you can just return true. If c==0, there are no numbers remaining that you could use, so you simply return false. Otherwise, try taking c into the sum, remembering also to add c to soFar, and recursively solve the problem for new parameters n-c*c*c and c-1. If that one was not a success, remove c from soFar, and try to recursively solve the problem without using C, which makes the parameters of the second recursive call to be n and c-1. Note how, since this method is supposed to find only one solution, once the recursive call returns true, its caller can also immediately return true without exploring the remaining possibilities. The first success will therefore cause the entire recursion to immediately roll back all the way to the top level, where the breakdown of n into distinct cubes can be read from the list soFar

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!