• Log in or sign up for an account here: https://www.sythe.org/login

Permutation Project

jo3s

Forum Addict
Joined
Aug 15, 2010
Sythe Gold
0
Messages
407
Write a Java project that produces random permutations of the numbers 1 to 10. To generate a random permutation, you need to fill an array with the numbers 1 to 10 so that no two entries of the array have the same contents. You could do this by brute force, by calling Math.random() until it produces a value that is not yet in the array. An alternative would be to create a second array and fill it with the numbers 1 to 10. Pick one of those numbers at random, remove it from the array, and append it to the permutation array.

In developing this project, you should create a class called PermutationGenerator. It should have one instance field, an integer array called permutation. It should have a default constructor that constructs the permutation array with 10 memory addresses (if you did not do this when you declared it as an instance field) and calls a mutator method setPermutation() which will fill the array. This class also needs an accessor method getPermutation().

This project also requires a driver class. This driver should print to the console window 10 permutations of the numbers 1 to 10. The output should look something like this:

4 6 10 7 8 3 9 5 2 1

5 4 7 2 8 10 1 6 9 3

1 4 3 5 8 7 6 2 10 9

3 10 2 8 1 4 5 6 9 7

6 1 9 5 3 10 7 4 8 2

3 1 10 8 5 4 6 2 7 9

7 8 2 6 1 3 10 4 9 5

6 9 5 7 4 2 1 3 10 8

5 6 8 1 2 9 4 10 7 3

7 6 8 2 3 5 9 4 10 1

To produce this output, you will have to construct an object of type PermutationGenerator inside a for loop, use this object to call the accessor method, and print the contents of the returned array. Use the enhanced for loop to print the contents of each array.


To submit your solution for credit, follow the instructions on the assignments page. Be sure to include a picture of your output and your code. If you finish early, feel free to help other students in the lab complete their assignment.


All help appreciated
 
Do you need help with any particular part?
 
Well this is how I'd start it (Providing you are stuck on arrays!)

Make the PermutationGenerator class

Make an instance variable "currentIndex"
Make the array
*private int[] array;*

Constructor
Set the currentIndex to 0
array = new array[10];


setPermutation() method
*{
array[currentIndex] = new array(Math.random(), ....*10 spaces*...., Math.random() );
currentIndex++;
}*


I don't know if this will be any help, you weren't specific enough with what you were stuck on.
 
Runescape Gamblingrunescape gambling
Back
Top