Background

Given any positive integer n, the hailstone sequence starting at n is obtained as follows. You write a sequence of numbers, one after another. Start by writing n. If n is even, then the next number is n/2. If n is odd, then the next number is 3n + 1. Continue in this way until you write the number 1.

For example, if you start at 7, then the next number is 22 (3 times 7 plus 1). The next number after 22 is 11. The hailstone sequence starting at 7 is

[7, 22, 11, 34, 17, 52, 26, 13, 40, 20, 10, 5, 16, 8, 4, 2, 1]

and it contains 17 numbers. The hailstone sequence starting at 6 is [6, 3, 10, 5, 16, 8, 4, 2, 1], and the hailstone sequence starting at 1 is [1].

Assignment Requirements

Functional Requirements

The functional requirements tell what the program is supposed to do when you run it.

Write a Java program that reads a number n from the user (after giving a suitable prompt) and then tells the user the following information:

  • the entire hailstone sequence starting at n, all on one line, with the numbers separated by spaces;
  • the length of the hailstone sequence that starts with n;
  • the largest number in the hailstone sequence that starts with n;
  • the largest length of any hailstone sequence starting with a number from 1 to n.

The output needs to be sensible and easy to read, not just numbers. For example, a session with this program might look as follows. Parts in black are written by the program. Parts in blue are typed by the user.

What number shall I start with? 7
The hailstone sequence starting at 7 is:
7 22 11 34 17 52 26 13 40 20 10 5 16 8 4 2 1
The length of the sequence is 17.
The largest number in the sequence is 52.
The longest hailstone sequence starting with a number up to 7 has length 17

Here is another example.

What number shall I start with? 1
The hailstone sequence starting at 1 is
1
The length of the sequence is 1.
The largest number in the sequence is 1.
The longest hailstone sequence starting with a number up to 1 has length 1

And here is another.

What number shall I start with? 8
The hailstone sequence starting at 8 is
8 4 2 1
The length of the sequence is 4.
The largest number in the sequence is 8.
The longest hailstone sequence starting with a number up to 8 has length 17

Nonfunctional Requirements

The nonfunctional requirements are additional requirements on how the program must be written, documented, etc., that are not directly concerned with what the program does when you run it.

The program is required to follow the coding standards for this course, which include the following.

  • You are required to start from the template that is provided. Be sure to put in your own name and the assignment number.
  • The program must use the correct file format. Be sure to indent well.
  • Every method is required to have a clear, concise and precise contract.
  • Use margin comments sparingly.
  • A method body must not change the value of a parameter. Pay attention to this one.
  • Make sure that each method does its whole job., not just part of it job.
  • Avoid code duplication.
  • End the last line of output.
  • Do not use any static variables for this assignment.
  • Every body of an if-statement, loop, etc. must be a compound statement.
  • If code is only performed at the end of the last iteration of a loop, then it should be written after the loop, not inside the loop.

Design issues and a development plan

The design of a program includes issues such as which methods, objects and classes it includes, how those components are organized into modules, and the purpose of each component.

This section both describes the required design and suggests how to write the program. You are required to follow the design.

For this program, use loops. Do not use recursion. Use type int for all of the integers.

  • Create a directory (folder) to hold assignment 1. Put all of the files for this assignment in that directory.
  • Create a file called hailstone.java. Copy and paste the template into it. Edit the template. If you will use tabs, say where the tab stops are. (If you don't know, type a line of about 8 x's. Then, in the line beneath that line, type a tab. How many x's were skipped over?)
  • Write a contract for a public static method that takes an integer value n and returns the number that follows n in the hailstone sequence. If you call this method next, then next(7) = 22 and next(22) = 11. Since there is no number that follows 1 in the hailstone sequence, this method requires its parameter n to be greater than 1. State that in the contract. You program must never compute next(1). Any time you want to get the next number in the sequence, you must use this method.
  • Now write a Java definition of the method that you have described in the preceding item. The Java definition is called the implementation of the method.
  • Write a main method that reads an integer n and shows the value of next(n). Test it on a few values to make sure that the next method works. Make sure that your tests, taken together, make use of every line that you have written.
  • Write a contract, then an implementation, of a public static method that takes an integer n and writes the entire hailstone sequence starting at n, on the standard output. This method has a void return type. Modify your main method to so that it no longer shows next(n, but shows the hailstone sequence starting at n. Test it.
  • Write a contract, then an implementation, of a public static method that takes an integer n and returns the length of the hailstone sequence starting at n. This method must not write anything. Modify your main method to so that it shows both the hailstone sequence and the length of the hailstone sequence starting at n Test it.
  • Write a contract, then an implementation, of a public static method that takes an integer n and returns the largest value in the hailstone sequence starting at n. This method must not write anything. Modify your main method to so that it also shows largest value in the sequence. Test it.
  • Write a contract, then an implementation, of a public static method that takes an integer n and returns the largest length of any hailstone sequence starting at a number from 1 to n. This method must not write anything. Modify your main method to so that it also shows the result of this method. Test it.
  • Submit your work.
Academic Honesty!
It is not our intention to break the school's academic policy. Posted solutions are meant to be used as a reference and should not be submitted as is. We are not held liable for any misuse of the solutions. Please see the frequently asked questions page for further questions and inquiries.
Kindly complete the form. Please provide a valid email address and we will get back to you within 24 hours. Payment is through PayPal, Buy me a Coffee or Cryptocurrency. We are a nonprofit organization however we need funds to keep this organization operating and to be able to complete our research and development projects.