Introduction

In this project, you’ll write a unit test that tests the behavior of a class using the test after development approach. An assessment rubric is provided to give you an indication of whether the principles taught in this section have been applied.

Self-assessment rubric

I have… Emergent Satisfactory Excellent
Used parameterized tests Did not use parameterized tests Partially used parameterized tests Used parameterized tests for test methods, wherever appropriate
Used a setup method No setup method used N/A Setup method used
Pitched the tests at the right level Inconsistent test approach—a combination of high-level and detailed approaches used N/A Consistent test approach—either a high-level or detailed approach adopted

Quick dive into probability before unit testing

The challenge requires us to write code that calculates factorials, permutations, and combinations. Below is a brief description of each.

Factorial

The following is an outline of the factorial operation.

Calculation and example

n!n! is the product of all positive integers less than or equal to nn, n!=n×(n1)×(n2)×...×3×2×1n! = n{\times}(n-1){\times}(n-2){\times}...{\times}3{\times}2{\times}1

For example: 5!=5×4×3×2×1=1205! = 5{\times}4{\times}3{\times}2{\times}1 = 120

Meaning of factorial

This operation is used in probability calculations where we want to find out the number of different ways to arrange nn distinct objects into a sequence. For instance, if we have three books on a bookshelf and we want to count the number of ways to arrange these books, we would calculate 3!=63! = 6.

What is Permutation?

The following is an outline of the permutation operation.

Calculation and example

nPk^nP_k is a permutation that is calculated using factorials as follows:

nPk=n!(nk)!^nP_k=\frac{n!}{(n-k)!}

For example:

5P3=5!(53)!=60^5P_3 = \frac{5!}{(5-3)!} = 60

Meaning of permutation

This operation is used in probability calculations where we want to find out the number of possible arrangements in a set when the order matters.

For instance, if we have eight people and we would like to pick a gold, silver, and bronze medal winner, the calculation to use would then be as follows:

8P3=8!(83)!=336^8P_3 = \frac{8!}{(8-3)!} = 336

Combination

The following is an outline of the combination operation.

Notation, calculations, and example

nCk^nC_k is a combination that is calculated using factorials as follows:

nCk=n!k!(nk)!^nC_k=\frac{n!}{k!(n-k)!}

For example:

5C3=5!3!(53)!=10^5C_3 = \frac{5!}{3!(5-3)!} = 10

What is a combination?

This operation is used in probability calculations where we want to find out the number of possible arrangements in a set when the order does not matter.

For instance, if we have eight people and we would like to pick three people, the calculation to use would then be as follows:

8C3=8!3!(83)!=56^8C_3 = \frac{8!}{3!(8-3)!} = 56

Project requirement

Write the application code that contains a calculator class, which has three public methods:

  • The first method calculates the factorial of a number.
  • The second method calculates the permutation of two numbers.
  • The third method calculates the combination of two numbers.

Ensure that all exceptions are taken into account when designing the class:

  • Factorials can only be taken on non-negative numbers.
  • For permutations and combinations, nn must be equal to, or larger than, kk.
  • For permutations and combinations, nn and kk must be non-negative.

After completing the application code, write the test code that tests these three operations. Remember to ensure that the test code not only tests normal operation but exceptions as well.

Get hands-on with 1200+ tech skills courses.