CSE 201 - Lab 5 - Using Methods


Note: This lab assumes that you have completed lab 1, and that you know how to login to your account, and how to open, edit, save your files, as well as compile and run your programs. If you have forgotten any of these techniques, refer back to the lab 1 handout. Be sure to follow the directions very carefully. If you have any problems or questions, be sure to ask your instructor as soon as possible. Points will be deducted if the submitted programs do not have the appropriate comments included.


Table of Contents


Objective

The objective for this lab is to write a Java program to practice program design and decomposition using Java class (static) methods. Note that the problem to be solved in this lab is no more difficult than the one you solved in Lab 4. The point of this lab is to practice the use and implementation of class methods. You will not get full credit if you submit a monolithic solution that does not fully leverage the power of Java methods.


Overview

You will complete a Java program starting from a given skeleton. The program will allow the user to generate Hailstone series.


Materials Provided

For this lab you will need the following file:

which contains a skeleton of the solution. You should complete this skeleton (i.e., complete all the methods) without modifying what is already there.

By the way, the skeleton will appear to have syntax errors in it when you open it. The reason is that all Java functions (methods returning a value) are supposed to contain a return statement to return their result. The functions in the skeleton, however, do not. Once you have added appropriate return statements to all the functions, the errors will be gone.

(You are allowed to make changes to the given skeleton, if you feel that that is necessary. But if the resulting design is considered by your instructor to be inferior to the one provided in the skeleton, your grade will reflect that. And, in any case, you MUST make use of appropriate class methods to structure your solution.)


Set up

Start Eclipse and create a new project, Lab5. Import into your Lab5 project the following file from K:\CSE201\Lab5 (see Lab 1 for instructions on how to import files in Eclipse):

Complete the Lab5 program according to the description below. If you don't remember how to create a project in Eclipse, see Lab 1 for instructions.


Description

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 (forward for odd values and back 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

Note that if a Hailstone series ever reaches the value 1, then the next value generated is 4, the next is 2, and the next is 1 again. Thus, when a Hailstone series reaches 1, then 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 mathematical puzzle: Given an arbitrary positive integer as a starting value, will the resulting Hailstone series converge as just described? For all positive integers of size at most 1.2x1012, it is known that the series does converge. To date, however, there is no formal proof that all such series must converge. (There is an interesting and readable article about the Hailstone series in the January 1984 issue of Scientific American.)

For this assignment, you will be writing a program that allows users to enter a starting value from which the program computes the corresponding Hailstone series. Thus, you can explore for yourself the behavior of the Hailstone series.

Here is a sample interaction between a user and the program you will write in the Lab5 class (user inputs are in bold):

Would you like to compute a Hailstone series? y
Enter a positive integer: 17

Generated series for starting value 17
Series converged in 13 steps
Largest value generated is 52

Would you like to compute another series? y
Enter a positive integer: -12
Enter a positive integer: 0
Enter a positive integer: 21

Generated series for starting value 21
Series converged in 8 steps
Largest value generated is 64

Would you like to compute another series? n
Goodbye!

Your program should ask the user if he/she wants to have another Hailstone series computed. If so, the user should be prompted for a starting value, and the Hailstone series should be computed until it converges (the number 1 is generated). Your program should then report to the user the number of terms that were generated to reach the value 1 (counting the starting number as one of the terms generated) and the largest value that was generated while getting there. For instance, for the Hailstone series above, generated from starting value 17, the number of terms generated is 13, and the largest value generated is 52. The entire process should then be repeated until the user does not want to have the program compute another Hailstone series. Note that it is not necessary to output the whole series (though you may find it helpful while testing and debugging your solution).


Design

A fundamental requirement for this lab is the use of good program decomposition and the definition and implementation of appropriate class methods. Submitted solutions that do not exhibit a good design using appropriate methods will not receive full credit. The skeleton provided represents a reasonably good design for a solution.

You should read the comments in each method carefully and then fill in all the bodies making sure that your code implements the behavior described in the comments. You should also make sure that the only way information is passed around in your program from method to method is through each method's given arguments.

One last note: This design forces you to duplicate the series generating code in two different places (the method computing the length of the series, and the method computing the largest term in the series). The reason for this is a technical problem with the Java language. In Java there is no easy way to write a method that returns two (or more) values. So if we wanted to avoid the duplication, we would have to make some other compromise in the design.


Lab Submission

Make sure your program compiles and runs correctly before submitting. To submit, create a single zip file (lab5.zip) containing the Lab5.java file from the Lab5 project (the location of the project in the file system will be z:\eclipse\workspace\Lab5) and then upload that zip file to the Carmen dropbox for Lab 5. If you don't remember how to create a zip file see Lab 1 for instructions.


Extra Credit (up to 20%)

Determine the starting value less than or equal to 100000 that leads to the longest Hailstone series before convergence, and the starting value less than or equal to 100000 that leads to the largest peak value in a Hailstone series before convergence.

To determine these values, copy your class, Lab5, into a new class, Lab5Extra.

Modify this class to allow the user to enter the largest starting value to consider (100000), and to compute the required values by generating all Hailstone series with starting values between 1 and the value entered by the user. Finally, your new program should output the two starting values, the length of the longest series, and the value of the largest term generated.

This second program is expected to conform to the same high design standards expected of the original one. If you designed the original one as suggested, you will find that it is fairly easy to modify it to perform this new extended task (this is a good criterion to evaluate how good a design is). You will receive extra credit only if, in addition to producing the correct answers, your program is designed appropriately.

To get extra credit, email the four computed values to your instructor, and submit Lab5Extra.java as part of your zip file submission.