Skip to Main Content

New to Java

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

need some help with hailstone sequence

807599Apr 3 2007 — edited Apr 3 2007
Hey i just need some help with this assignment i have... i dont want u guys to do the homework for me... i just want you guys to explain to me what is it that i have wrong.... and what am i missing.... im a slow learner so please be patient.... thank you.

heres what im supposed to do:


I. The Hailstone Series

An interesting sequence of integers known as the hailstone series works like this:

1. Pick any starting number.
2. Now, repeat the following...

If the number is odd, triple it and add one.
Otherwise (it's even), divide it by two.

...until the number equals 4.

The sequence is called the hailstone series because although the numbers bob up and down, they eventually reach a repeating "ground" state of 4, 2, 1, 4, 2, 1, ...

For example, the series beginning at 13 has eight terms:

13 40 20 10 5 16 8 4


II. The HailstoneSeries Class
Write a HailstoneSeries class with the following methods:

1. A constructor that creates a HailstoneSeries object with a given starting value passed as a ?construction parameter?
2. A method that generates and prints each value in the series
3. A ?get? method that returns the largest number of terms in any series
4. A ?get? method that returns the starting value of the series with the largest number of terms
5. A ?get? method that returns the highest value reached in any series
6. A ?get? method that returns the starting value of the series that reached the highest value
 For more information on these methods, see part IV.


III. The HailstoneTester Class

Write a test class that accepts two integers from the user. Then, for each integer between the lower input and the higher input, inclusive, creates a Hailstone object and generates the series beginning with that number.

For example, if the user enters 5 and 10, your test class will create 6 Hailstone objects and generate 6 series: one each beginning at 5, 6, 7, 8, 9, and 10.

Finally, your test class will print the information returned by the 4 ?get? methods listed above.


IV. Static Class Variables and Methods

Remember that every object created has its own copy of all the instance variables of the class. But what if we want only one "class-wide" variable to be shared by all objects of the class?

For example, since we need to know the highest value reached in any sequence, we need one variable to store this value, no matter how many objects are created (i.e., no matter how many sequences are generated)

The solution is to create a "static" variable in the Hailstone class:
private static int highestValue ;

? The keyword static indicates that only one such variable will exist -- to be shared by all objects of the class -- no matter how many objects are created. For this reason, static variables are commonly called class variables and not instance variables (recall that an object is an "instance" of a class).

? To access a static variable, a static method is used:

public static int getHighestValue()
{
return highestValue ;
}

? As we have seen with static methods from Java's Math class (e.g., sqrt) and Integer class (e.g., parseInt), static methods are called using the class name, instead of an object name ( e.g., Math.sqrt(arg) )


V. Additional Program Specifications

1. In the run you hand in, have the user enter 100 for the low value and 1000 for the high (i.e., 901 series will be created - one beginning at each value from 100 to 1000)

2. In counting the number of terms in a sequence, count the starting number as the first term and the 4 as the last (as shown in I., above)


VI. Program Testing Tips

1. To verify that each sequence is being generated correctly, insert a temporary print statement to display each term.

2. To verify that your program works correctly, test it with values of low and high that are relatively close together (say, 5 and 9 ? a total of 5 series), so that you can easily check the results by hand.


and heres what i have so far
public class HailstoneSeries     
{
  
  private static int highestValue;
  private int i;
  private int max;
  private int count;
  private int n;
 
 
	public static int getHighestValue()
	{
		return highestValue ;
	}
	
	HailstoneSeries()
	{
		i=0;
		max=0;
		count=0;
		n=0;
 
 
 
 
		for(i=;i<1000000;i++)
			{
				n=i;
				count=0;
			
				while(n!=0)
				{
					if(n%2==0)
					{
					n=n/2;
					}
						else
						{
							n=3*n+1;
						}
				count++;
				}

				if(count>max)
					{
						max=count;
						highestValue=i;
						System.out.println("Value = " + highestValue ) ;
					}
			
			}
			
	}

}
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on May 1 2007
Added on Apr 3 2007
6 comments
341 views