Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 6 of 6

Thread: looping problem in Hailstrom program

  1. #1
    Junior Member
    Join Date
    May 2009
    Posts
    3
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default looping problem in Hailstrom program

    Hello, I just started doing an online tutorial to learn some java and I'm stumped on solving this Hailstorm Problem. The problem is an old math phenomenon where no matter what number you start with you will end at the number 1. If the number is even you divide it by two and if its odd you compute 3n + 1 (n being the number) and you continue to do it until you reach 1. So here was my attempt at solving it:

    import acm.program.*;
     
    public class Hailstone extends ConsoleProgram {
     
    	public void run() {
    		int n = readInt ("Enter a number: ");
    		int turns = 1;
    		while (n != 1) {
    			if ((n % 2) == 0){
    				numEven(n);	
    			} else {
    				numOdd(n);
    			}
    			turns++;
    		}
    		println ("The process took " + turns + " to reach 1.");
    	}
     
    	// numEven computes the answer if n is even.
    	private int numEven (int n) {
    		int j = n/2;
    		println (n + " is even so I take half: " + j);
    		return j;
    	}
     
    	// numOdd computes the answer if n is odd.
    	private int numOdd (int n) {
    		int k = (int)(n * 3) + 1;
    		println (n + " is odd, so I make 3n + 1: "+ k);
    		return k;
    	}
     
    }

    The problem being that its looping indefinitely after it solves the first even or odd computation and doesn't put the returned value back into the original n for the second loop. Let me know if you think of anything to solve this. Thanks.


  2. #2
    mmm.. coffee JavaPF's Avatar
    Join Date
    May 2008
    Location
    United Kingdom
    Posts
    3,336
    My Mood
    Mellow
    Thanks
    258
    Thanked 294 Times in 227 Posts
    Blog Entries
    4

    Default Re: Hailstorm problem from a new programmer.

    Hello JonnyBoy and welcome to the Java Programming Forums

    I need to be able to compile this code to test it out. Could you please include the ConsoleProgram class?
    Please use [highlight=Java] code [/highlight] tags when posting your code.
    Forum Tip: Add to peoples reputation by clicking the button on their useful posts.

  3. #3
    Junior Member
    Join Date
    May 2009
    Posts
    3
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Hailstorm problem from a new programmer.

    So, basically I looked in the other folder that came with the assignment, and ConsoleProgram is a class that has a bunch of other things stringed into it that I think are too complicated to try to give to you. Instead, if you still wanted to try to compile it go here: CS106A Programming Methodology and download the second assignment, import it as a project, and copy my code into the Hailstorm file.
    This got a little more complicated than I had expected so if its too much work don't sweat it. Unfortunately I don't actually go to stanford, but am just taking the class on itunes otherwise i'd just ask the teacher for help .

  4. #4
    Senile Half-Wit Freaky Chris's Avatar
    Join Date
    Mar 2009
    Posts
    834
    My Mood
    Cynical
    Thanks
    7
    Thanked 105 Times in 90 Posts

    Default Re: Hailstorm problem from a new programmer.

    numOdd(n);
    should become
    n = numOdd(n);

    and the same for even,

    Regards,
    Chris

  5. The Following User Says Thank You to Freaky Chris For This Useful Post:

    JonnyBoy (May 8th, 2009)

  6. #5
    Junior Member
    Join Date
    May 2009
    Posts
    3
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Hailstorm problem from a new programmer.

    Sweet, that's the ticket! Thanks a bunch Chris.

  7. #6
    Senile Half-Wit Freaky Chris's Avatar
    Join Date
    Mar 2009
    Posts
    834
    My Mood
    Cynical
    Thanks
    7
    Thanked 105 Times in 90 Posts

    Default Re: Hailstorm problem from a new programmer.

    Your welcome, glad I could help.

    Regards,
    Chris

Similar Threads

  1. Programmer for a Java based game project
    By Takkun in forum Project Collaboration
    Replies: 4
    Last Post: June 14th, 2010, 05:47 PM
  2. Calculator application using java
    By fabolous04 in forum Paid Java Projects
    Replies: 4
    Last Post: March 25th, 2009, 11:29 AM
  3. Java program to write game
    By GrosslyMisinformed in forum Paid Java Projects
    Replies: 3
    Last Post: January 27th, 2009, 03:33 PM