我考试题目啊 不然我会死的 只求代码
Assessment Description - Overview
Two dierent methods for generating random permutations of the numbers < 1; 2; : : : ; n >
are given in the module lecture notes. For this assessment you should implement both of
these in Java and present some ndings about their behaviour.
Assessment Description Details
The implementation should be given as a Java class,
public class Permutation
Class Fields
Type Description
private int [] permOne The permutation of < 1; 2; : : : ; n > produced
by Method 1.
private int [] permTwo The permutation of < 1; 2; : : : ; n > produced
by Method 2.
private Random randomSequence The random number sequence for this instance.
private int size The value of n for this instance.
private int counter1 Will hold the number of calls of the method
randomSequence.nextInt() made by Method 1.
private int counter2 Will hold the number of calls of the method
randomSequence.nextInt() made by Method 2.
Constructors
The class has a single constructor,
Permutation(int n)
The statement P = new Permutation(n) will create an instance (P) of the Permutation class to
be used to produce a random permutation n of the numbers < 1; 2; : : : ; n >. The constructor
must carry out the following:
a. Set size = n.
b. Instantiate the arrays PermOne and PermTwo as new int[n] with PermOne[i]=i+1 and
PermTwo[i]=i+1.
c. Create instance of the random number sequence via RandomSequence=new Random().
d. Set both counter1 and counter2 to be 0.
Instance Methods
Name Description
public void randomPermOne() Forms a random permutation of < 1; 2; : : : ; n >
in PermOne[].
Count number of calls to randomSequence.nextInt()
in the variable counter1.
This should use Method 1 of the lecture notes.
public void randomPermTwo() Forms a random permutation of < 1; 2; : : : ; n >
in PermTwo[].
Count the number of calls to randomSequence.nextInt()
in the variable counter2.
This should use Method 2 of the lecture notes.
public int[ ] getPermOne() Returns content of PermOne[]
public int[ ] getPermTwo() Returns content of PermTwo[]
public int getCountPermOne() Returns the value stored in counter1
after RandomPermOne() has been used.
public int getCountPermTwo() Returns the value stored in counter2
after RandomPermTwo() has been used.
Hints and Suggestions
Reminder - The Permutation Generation Methods
Note: The "easiest" way to record that k has been chosen (using the method described) is
to set TempPerm[k] = 0: in subsequent stages "k has not been chosen before" is the same as
"TempPerm[k]! = 0".
Algorithm 1 Method 1 for Generating Random Permutations of < 1; 2; : : : ; size >
nextPlace = 0
int[ ] tempPerm = new int[size]
for i = 0 to size