Computer Science 3200
Fall 2015
Section 001
Programming Assignment 1

Assigned: Tuesday, September 8
Due: Friday, September 18, 11:59pm

Table of contents

  1. Note about this assignment
  2. Background
  3. Assignment requirements
  4. Design issues and a development plan
  5. Submitting your work


Note about This Assignment

This assignment is intended as a review of Java, using loops, static methods and contracts.

It is important that you follow the instructions. Do not try to improve on the design described here. Read the entire assignment before you start working on it.


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:

  1. the entire hailstone sequence starting at n, all on one line, with the numbers separated by spaces;
  2. the length of the hailstone sequence that starts with n;
  3. the largest number in the hailstone sequence that starts with n;
  4. 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.


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.

  1. Create a directory (folder) to hold assignment 1. Put all of the files for this assignment in that directory.


  2. 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?)


  3. 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.


  4. 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.


  5. 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.


  6. 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.


  7. 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.


  8. 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.


  9. 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.


  10. Submit your work.


Submitting Your Work

You must submit your program using the following method. Email submissions will not be accepted. An excuse that you do not know how to use Linux will not be accepted.

To turn in assignment 1, change your directory to the one that holds assignment 1 and do the following command.

  ~abrahamsonk/3200/bin/submit 1 hailstone.java
After submitting, you should receive confirmation that the submission was successful. If you do not receive confirmation, assume that the submission did not work. Command
  ~abrahamsonk/3200/bin/submit 1
will show you what you have submitted for assignment 1.

You can do repeated submissions. New submissions will replace old ones.

Late submissions

Late submissions will be accepted for one day after the due date. If you miss a late submission deadline by a microsecond, your work will not be accepted.