Go Back   Java Programming Forums > Java Standard Edition Programming Help > Algorithms & Recursion


Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 06-05-2009, 08:48 PM
Junior Member
 

Join Date: May 2009
Posts: 3
Thanks: 1
Thanked 0 Times in 0 Posts
JonnyBoy is on a distinguished road
Default Hailstorm problem from a new programmer.

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:

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



Reply With Quote Share this thread on Facebook
Sponsored Links
Java Training from DevelopIntelligence
  #2 (permalink)  
Old 07-05-2009, 09:38 AM
JavaPF's Avatar
mmm.. coffee
 
7 Highscores

Join Date: May 2008
Location: United Kingdom
Posts: 1,583
Thanks: 104
Thanked 93 Times in 86 Posts
JavaPF is someone you want to know!JavaPF is someone you want to know!JavaPF is someone you want to know!

I'm feeling Mellow
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?
__________________
Don't forget to add syntax highlighted code tags around your code: [highlight=Java] code here [/highlight]

Forum Tip: Add to peoples reputation () by clicking the button on their useful posts.
Reply With Quote
  #3 (permalink)  
Old 07-05-2009, 10:02 PM
Junior Member
 

Join Date: May 2009
Posts: 3
Thanks: 1
Thanked 0 Times in 0 Posts
JonnyBoy is on a distinguished road
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 .
Reply With Quote
  #4 (permalink)  
Old 08-05-2009, 06:43 AM
Freaky Chris's Avatar
Senile Half-Wit
 

Join Date: Mar 2009
Location: UK
Posts: 621
Thanks: 2
Thanked 64 Times in 60 Posts
Freaky Chris will become famous soon enoughFreaky Chris will become famous soon enoughFreaky Chris will become famous soon enough

I'm feeling Cheerful
Default Re: Hailstorm problem from a new programmer.

Java Code
numOdd(n);
should become
Java Code
n = numOdd(n);
and the same for even,

Regards,
Chris
Reply With Quote
The Following User Says Thank You to Freaky Chris For This Useful Post:
JonnyBoy (09-05-2009)
  #5 (permalink)  
Old 09-05-2009, 12:58 AM
Junior Member
 

Join Date: May 2009
Posts: 3
Thanks: 1
Thanked 0 Times in 0 Posts
JonnyBoy is on a distinguished road
Default Re: Hailstorm problem from a new programmer.

Sweet, that's the ticket! Thanks a bunch Chris.
Reply With Quote
  #6 (permalink)  
Old 09-05-2009, 05:58 AM
Freaky Chris's Avatar
Senile Half-Wit
 

Join Date: Mar 2009
Location: UK
Posts: 621
Thanks: 2
Thanked 64 Times in 60 Posts
Freaky Chris will become famous soon enoughFreaky Chris will become famous soon enoughFreaky Chris will become famous soon enough

I'm feeling Cheerful
Default Re: Hailstorm problem from a new programmer.

Your welcome, glad I could help.

Regards,
Chris
Reply With Quote
Reply

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



Similar Threads
Thread Thread Starter Forum Replies Last Post
Java Game Project--Need a Programmer Takkun Project Collaboration 4 14-06-2010 10:47 PM
need a programmer ..calculator app fabolous04 Paid Java Projects 4 25-03-2009 03:29 PM
Looking for a programmer to make a simple program. GrosslyMisinformed Paid Java Projects 3 27-01-2009 07:33 PM


100 most searched terms
Search Cloud
2d arraylist java actionlistener actionlistener in java actionlistener java addactionlistener addactionlistener in java addactionlistener java applications of oops could not create java virtual machine xp double format java double to int java double to integer in java double to integer java eclipse shortcut keys eclipse tutorial for beginners exception in thread "awt-eventqueue-0" java.lang.outofmemoryerror: java heap space exception in thread "main" java.lang.nullpointerexception exception in thread "main" java.lang.outofmemoryerror: java heap space format double java get mouse position java http://www.javaprogrammingforums.com/object-oriented-programming/3713-limiting-decimal-places-double.html java 2d arraylist java actionlistener java addactionlistener java double format java double to int java double to integer java format double java forum java forums java get mouse position java list to map java mouse position java programmers forum java programming forum java programming forums java programming help java project ideas java sendkeys java two dimensional arraylist java.lang.classformaterror: truncated class file java.lang.outofmemoryerror: java heap space java.util.arraylist jbutton actionlistener jtextarea font jtextarea font color jtextfield font size jxl.read.biff.biffexception: unable to recognize ole stream two dimensional arraylist java writing ipod apps

All times are GMT. The time now is 12:07 AM.
Powered by vBulletin® Copyright ©2000-2009, Jelsoft Enterprises Ltd.