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 projectHailstoneRevisited. - Open the
srcfolder of this project and then open(default package). As a starting point you should useProgramWithIOAndStaticMethod.java. Rename itHailstone1and 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:/** * Generates and outputs the Hailstone series starting with the given * {@code NaturalNumber}. * * @param n * the starting natural number * @param out * the output stream * @updates out.content * @requires n > 0 and out.is_open * @ensures out.content = #out.content * [the Hailstone series starting with n] */ private static void generateSeries(NaturalNumber n, SimpleWriter out) {...}Note:
nis arestores-mode parameter (that's the default parameter mode when no explicit parameter mode is specified in the contract). That means that the value ofnat the end of the method must be the same as it was at the start. Add an output statement to yourmainprocedure to print the value of theNaturalNumberafter the call togenerateSeriesto confirm that the value was restored. -
Copy
Hailstone1.javato createHailstone2.java. ChangegenerateSeries(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 Javaintbecause 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.javato createHailstone3.java. ChangegenerateSeries(including its Javadoc comments) so that it also computes and outputs the maximum value of the series.
Additional Activities
- Copy
Hailstone3.javato createHailstone4.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
intoverflow, i.e., the series would contain a value that exceedsInteger.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?
- What is the smallest natural number for which the series
generation would result in Java