Question: Halllo who can help me . Here they say that we need at least four assertions or exceptions Given a natural number n>0n>0, the next
Halllo who can help me .
Here they say that we need at least four assertions or exceptions
Given a natural number n>0n>0, the next element of the hailstone sequence is computed as follows:
n{n/2 if n is even
3n+1 otherwise
It is conjectured that the sequence will always eventually reach 1, regardless of which value for nn one starts with. Given a starting number, the following piece of Java code computes the number of steps required until the sequence does so:
public int hailstoneLength(int n) { return compute(n); } private int compute(int n) { if (n == 1) { return 0; } else if (n % 2 == 0) { return 1 + even(n); } else { return 1 + odd(n); } } private int even(int n) { return compute(n / 2); } private int odd(int n) { return compute(3 * n + 1); } Add at least four assertions or exceptions to the code (at least one of each). Add comments to explain why you added a particular exception or assertion. In particular, explain why an exception or an assertion was more appropriate.
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
