Programming exercise 8

Phase 1

Let n be an integer greater than 0. The factorial of n, sometimes denoted as n!, is defined by 1 × 2 × ... × n. Complete the following recursive method

/**
   Returns the factorial of the given number.

   @param n an integer.
   @pre. n > 0.
*/
public static int factorial(int n)
and put it in a class named PEx08.

Write another class to test the method (this class need not be submitted).

Phase 2

Let n be an integer greater than 0. The elements of the set { 1, 2, ..., n } can be ordered in n! different ways. A sequence of the elements of the set { 1, 2, ..., n }, where each element of the set occurs exactly once in the sequence, is called a permutation of the set { 1, 2, ..., n }. For example, the set { 1, 2, 3 } has the following permutations:
[1, 2, 3]
[1, 3, 2]
[2, 1, 3]
[2, 3, 1]
[3, 1, 2]
[3, 2, 1]
A permutation can be represented as a List of Integer objects. Complete the following recursive method

/**
   Returns the collection of all permutations of the set { 1, ..., n }.

   @param n an integer.
   @pre. n > 0.
   @return the collection of all permutations of the set { 1, ..., n }.
*/
public static Set<List<Integer>> permutations(int n)
and add it in the class named PEx08.

Write another class to test the method (this class need not be submitted).

Phase 3

Once you have tested both methods, submit your class electronically before the deadline of this programming exercise using the submit command:

submit 1030 PEx08 PEx08.java
You may submit your solution more than once. Additional documentation about the submit command can be viewed by typing man submit.