Lab: Hailstone Series Revisited
The Problem
Recall the Hailstone series from a previous lab. For this assignment, you will re-write the program
that allows users to to enter a starting value from which the
program computes and outputs the corresponding Hailstone series. But
instead of computing with Java primitive ints, you
will use
NaturalNumber
objects. After the initial program works, there are a number of
other requirements to change it slightly, one step at a time, as
explained below.
Setup
Follow these steps to set up a project for this lab.
- Create a new Eclipse project by copying ProjectTemplate.
Name the new project HailstoneRevisited.
- Open the src folder of this project and then open
(default package). As a starting point you should use ProgramWithIOAndStaticMethod.java.
Rename it Hailstone1 and delete the other files from the
project.
Method
- Edit Hailstone1.java (including updating comments
appropriately) to ask the user for a positive integer (i.e., a
natural number greater than zero) and then compute and output the
corresponding Hailstone series. The generation and output of the
series should be done in a static method declared as follows:
Note: n is a restores-mode parameter
(that's the default parameter mode when no explicit parameter mode is
specificed in the contract). That means that the value of n
at the end of the method must be the same as it was at the start.
Add an output statement to your main procedure
to print the value of the NaturalNumber after the
call to generateSeries to confirm that the value was restored.
- Copy Hailstone1.java to create Hailstone2.java.
Change generateSeries (including its Javadoc comments) so
that it also computes and outputs the length of the series. You
can keep track of the length of the sequence with a Java int
because it is unlikely any of us would have enough time to
run the program on inputs that require more than 231–1
steps.
- Copy Hailstone2.java to create Hailstone3.java.
Change generateSeries (including its Javadoc comments) so
that it also computes and outputs the maximum value of the series.
Additional Activities
- Copy Hailstone3.java to create Hailstone4.java.
Change the program so that it satisfies these requirements:
- it can be used to generate multiple series with
different starting points, e.g., between 1 and a positive
integer entered by the user;
- it keeps track of the largest value generated across
all series and for what smallest starting number that
occurred;
- it keeps track of the length of the longest series and
the corresponding smallest starting number.
- Use your program to answer the following questions:
- What is the smallest natural number for which the
series generation would result in Java int
overflow, i.e., the series would contain a value that exceeds
Integer.MAX_VALUE (i.e., 231–1)?
- What is the smallest natural number for which the
series length reaches (or exceeds) 500 steps and what is the
actual length of the generated series?
- How long did your program have to run to find the
answers to the previous questions?