Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

solve each parts please Start this lab by creating a NetBeans project named Lab_

ID: 3590698 • Letter: S

Question

solve each parts please

Start this lab by creating a NetBeans project named Lab_Project_11.

Once the project finishes loading, add an additional Java class named PermutationCounter.

Lab 11.1

The number of permutations of n items is computed as follows:

     n! = n * (n - 1)! for n > 0;

Here, the number of permutations of n items is written as n!, which is pronounced "n factorial".

It is easy to see why this formula is true. There are n choices for placing the first item. For each of these choices, there are n - 1 places left for the other items.

When n is 1, there is only one permutation of a single item. Hence

   1! = 1

It is also convenient to set

   0! = 1

From this definition, compute the value of 5! and show your work, in the space provided below.

Lab 11.2

Copy/paste the highlighted code below into your PermutationCounter.java source file:|

Complete the getCount method so that it counts the number of permutations for n items by recursively constructing a PermutationCounterobject to count the permutations of smaller number of items (i.e., n-1).

Lab 11.3

To test your PermutationCounter class, add the highlighted code below to your main method, replacing the output with your hand-computed value for 5! from Lab 11.1 where indicated:

public class Lab_Project_11
{
   public static void main(String[] args)
   {
      PermutationCounter counter = new PermutationCounter(5);
      System.out.println(counter.getCount());
      System.out.println("Expected: *** replace with the value calculated for 5! in lab 11.1 ***");
   } // end main

} // end class Lab_Project_11

Explanation / Answer

LAB 11.1:

Given that :-

To find:-

Using the above information we are required to compute the value of 5!.

Solution:-

Step 1: Substitute n = 5 in the formula n! = n*(n-1)!

5! = 5 * (5-1)!

5! = 5 * 4!

Step 2: Apply  n! = n*(n-1)! to get the value of 4! in the above expression

5! = 5 * 4 * (4-1)!

5! = 5 * 4 * 3!

Step 3: Apply n! = n*(n-1)! to get the value of 3! in the above expression

5! = 5 * 4 * 3 * (3-1)!

5! = 5 * 4 * 3 * 2!

Step 4: Apply n! = n*(n-1)! to get the value of 2! in the above expression

5! = 5 * 4 * 3 * 2 * (2-1)!

5! = 5 * 4 * 3 * 2 * 1!

Step 5: Apply n! = n*(n-1)! to get the value of 1! in the above expression (or you can directly use 1! = 1)

5! = 5 * 4 * 3 * 2 * 1 * (1-1)!

5! = 5 * 4 * 3 * 2 * 1 * 0!

Its given that 0! = 1. So, 5! = 5 * 4 * 3 * 2 * 1 * 1 = 120

LAB 11.2:

Given code:

To do: Write code for getCount() in the above snippet so that it finds the number of permutations for n items recursively.

Solution: Recursive objects have attributes of the same class to which they belong to.

From the computation of 5! above, it is evident that the factorial of a number can be calculated by repeatedly applying the same formula (or logic) to n-1 every time. Hence, recursion can be used to write code for this.

public long getCount() {

if (n == 1) {

return 1; // 1! = 1

}

else {

return n * (new PermutationCounter(n - 1).getCount()); // recursive construction of object

}

}

LAB 11.3:

Given code:

public class Lab_Project_11
{
   public static void main(String[] args)
   {
      PermutationCounter counter = new PermutationCounter(5);
      System.out.println(counter.getCount());
      System.out.println("Expected: *** replace with the value calculated for 5! in lab 11.1 ***");
   } // end main

} // end class Lab_Project_11

To do:

Add main method to the above code to test the PermutationCounter class.

Solution: The final goal is to be able to compare our hand computed 5! value with the output generated by the code. The code of the main mehod will be-

public class Lab_Project_11
{
   public static void main(String[] args)
   {
      PermutationCounter counter = new PermutationCounter(5);
      System.out.println(counter.getCount());
      System.out.println("Expected: 120"); // The value calculated in Lab 11.1
   } // end main

} // end class Lab_Project_11

The value printed shoulb be same as that of he expected.