Lab: Hailstone Series
The Problem
A Hailstone series is defined as follows: start with any integer value greater than 0, say x. If x is even, then the next value in the series is x/2; if x is odd, then the next value in the series is 3x + 1. Now apply the same rules to create the next value in the series, and so on. The name Hailstone comes from the property that the values in such a series alternate between going up and down (up for odd values and down for even values.)
For instance, here is the Hailstone series generated from starting value 17:
17, 52, 26, 13, 40, 20, 10, 5, 16, 8, 4, 2, 1
If a Hailstone series ever reaches 1, the next value generated is 4, then 2, then 1 again. Thus, when a Hailstone series reaches 1, it has converged in the sense that the rest of the series is 4, 2, 1, 4, 2, 1, ... .
The Hailstone series is the subject of a long-standing open mathematical question: given an arbitrary positive integer as a starting value, will the resulting Hailstone series converge? For all positive integers up to 2.26x1021, it is known that the series does converge. To date, however, there is no proof that all such series converge! (Some recent progress on this problem is described in Quanta Magazine: "Mathematician Proves Huge Result on 'Dangerous' Problem".)
For this assignment, you will write a program to test the conjecture. Your program will take a starting value from the user and output the corresponding Hailstone series (stopping with 1 if it ever reaches 1). 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(if needed, see Creating a New Project from a Project Template for details). Name the new projectHailstone. - 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 (if needed, see Creating a Program from a Skeleton (also Renaming a Java Program) for details).
Method
-
Edit
Hailstone1.java(including updating comments appropriately) to ask the user for a positive integer 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 (copy the method header, including the documentation comment, from the browser and paste it into Eclipse, replacing only themyMethodmethod provided in the skeleton program):/** * Generates and outputs the Hailstone series starting with the given integer. * * @param n * the starting integer * @param out * the output stream */ private static void generateSeries(int n, SimpleWriter out) {...} -
Copy
Hailstone1.javato createHailstone2.java(right-click onHailstone1.javato get the contextual pop-up menu and choose Copy, then right-click on(default package)and choose Paste, providing the new nameHailstone2). ChangegenerateSeries(including its Javadoc comments) so that it also computes and outputs the length of the series. -
Copy
Hailstone2.javato createHailstone3.java. ChangegenerateSeries(including its Javadoc comments) so that it also computes and outputs the maximum value of the series. -
Copy
Hailstone3.javato createHailstone4.java. Change it so that it repeatedly asks the user whether they wish to calculate another series. If the response is "y", then the program should proceed; if it is anything else, then the program should quit.
Additional Activities
-
Copy
Hailstone4.javato createHailstone5.java. Change it so that it checks that the input provided by the user is a positive integer. You should implement a new static method declared as follows:/** * Repeatedly asks the user for a positive integer until the user enters * one. Returns the positive integer. * * @param in * the input stream * @param out * the output stream * @return a positive integer entered by the user */ private static int getPositiveInteger(SimpleReader in, SimpleWriter out) {...}Note that you cannot assume the user will provide a number; the user can type pretty much anything. So your method should read the input as a
String(useSimpleReader'snextLine()), then make sure that the input is an integer number (useFormatChecker.canParseInt()), then convert the string to an integer (useInteger.parseInt()), and finally check that the integer is positive.