Answered step by step
Verified Expert Solution
Question
1 Approved Answer
I need to write some Methods ( See TODO ) in a junit program that tests another class. import java.util.LinkedList; import java.util.Arrays; import static org.junit.jupiter.api.Assertions.
I need to write some Methods See TODO in a junit program that tests another class.
import java.util.LinkedList;
import java.util.Arrays;
import static org.junit.jupiter.api.Assertions.;
import org.junit.jupiter.api.Test;
class PermutationTest
PermutationVariation p;
PermutationVariation p;
public int n;
public int n;
int cases;
void initialize
n;
n;
Cases c new Cases;
p cswitchforTestingcases n;
p cswitchforTestingcases n;
private int factorialint n
int result ;
for int i ; i n; i
result i;
return result;
TODO test the constructor of the permutation class, the variable original needs to be initialized in the constructor with the given length, and no number is allowed to be used twice. allDerangements needs to be initialized with an empty list
@Test
void testPermutation
initialize;
assertNotNullporiginal;
assertEqualsn poriginal.length;
assertTrueArraysstreamporiginaldistinctcount n;
assertNotNullpallDerangements;
assertTruepallDerangements.isEmpty;
TODO needs to test the derangement method of the permutation class, this method developes all the fixed point free permutations and saves them in allDerangements. This test meeds tp determine whether the number of derangements is correct and if they're actually derangements if its actually permutations in the next one
@Test
void testDerangements
initialize;
fixConstructor;
pderangements;
assertNotNullpallDerangements;
assertTruepallDerangements.isEmpty;
Check if all are derangements
for int arr : pallDerangements
for int i ; i arr.length; i
assertNotEqualsi arri;
Check the count of derangements
double expectedCount factorialn Math.E;
assertEqualsexpectedCount pallDerangements.size;
TODO test whether all of the in the derangement method developed sequences are actually permutations of the original array "orriginal", if there were no permutations calculated, this test should not work
@Test
void testsameElements
initialize;
fixConstructor;
pderangements;
for int derangement : pallDerangements
int sortedDerangement Arrays.copyOfderangement derangement.length;
Arrays.sortsortedDerangement;
assertArrayEqualsporiginal, sortedDerangement;
void setCasesint c
this.casesc;
public void fixConstructor
in case there is something wrong with the constructor
pallDerangementsnew LinkedList;
forint i;i;
forint i;i;
public void derangements
backtrackingnew LinkedList;
public void backtrackingLinkedList candidate
int othis.original;
if candidatesize olength
int finalistnew intolength;
forint i;i
Step 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