aspose file tools*
The moose likes Beginning Java and the fly likes Is there any one who can help me solve this? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Is there any one who can help me solve this?" Watch "Is there any one who can help me solve this?" New topic
Author

Is there any one who can help me solve this?

Namhoon Lee
Greenhorn

Joined: Sep 16, 2004
Posts: 5
Hi.
I'm taking AP Computer Scinece AB in high school now.
I've been assigned many programs so far. But.. I can't figure them out.
There are two programs I need your help. So..Please!!! Help Me!!!
Please!! Enjoy helping me!


<Program #1>
It's divided into three small programs and I've figured out one of them, which was the easiest one..You will be able to see the programs if you click on the link below.

(page1)
http://iboard3.superboard.dreamwiz.com/board.cgi?db=90_darkoo&idx=2&page=1

(page2)
http://iboard3.superboard.dreamwiz.com/board.cgi?db=90_darkoo&idx=3&page=1


<Program #2>
http://iboard3.superboard.dreamwiz.com/board.cgi?db=90_darkoo&idx=4&page=1

---------------------------------------------------------

The way I figured out first part of program #1 was like this :

===================================================

From there, for the second and third programs of program #1, I tried to figure it out by using 'for' statement instead of 'while', but I couldn't.
So..Please help me out.. Thank you

or you can e-mail me too. It would be great too.
add. : darkoo311@yahoo.com

[ edited to preserve formatting using the [code] and [/code] UBB tags -ds ]
[ September 22, 2004: Message edited by: Dirk Schreckmann ]
Nigel Browne
Ranch Hand

Joined: May 15, 2001
Posts: 673
If you want help with your homework, try posting what you have done for the 2nd and 3rd tasks and people will be able to point out where you are going wrong. You wont learn unless you do as much as your own work as possible.
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11318
    
  16

i don't think you should use a 'for' statement INSTEAD of a while statement.

i think you should use them BOTH... In fact, i wouldn't change any part of your while statement at all...


There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
Namhoon Lee
Greenhorn

Joined: Sep 16, 2004
Posts: 5
Oh..sure=)
Thank you for advice.
And I used both 'for' and 'while' statement this time.
It works ok when I use 'javac'in MS-DOS to operate it. But when I use 'java' to get the answer, it just stops there. It doesn't show any error messages and doesn't show any answers either. What do you think
its problem is? (It's second one in problem #1)
-----------------------------------------------------------------------
public class ThreeX2
{
public static void main (String[] org )

{
System.out.println ();
int sum=0;
int count = 0;

for (int startValue =2; startValue <=99; startValue ++)
while (startValue >1)
{
count ++;
if (startValue % 2 == 0)
startValue = startValue/2;
else
startValue = 3*startValue +1;
sum += sum + count;
}
System.out.println ( count );
}
}
------------------------------------------------------------------
I just don't get how I'm supposed to use 'for' statement in this case.
Am I using 'for' right way?
-------------------------------------------------------------------
And for third one in program #1, Would you give me any idea what kind of method I should use? I can't think of anything to start with.
Dirk Schreckmann
Sheriff

Joined: Dec 10, 2001
Posts: 7023
When posting code, please be sure to surround the code with the [code] and [/code] UBB Tags. This will help to preserve the formatting of the code, thus making it easier to read and understand.


[How To Ask Good Questions] [JavaRanch FAQ Wiki] [JavaRanch Radio]
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11318
    
  16

i think your for statement is set up right, but the problem is what you are using to control your loop. you want your for loop to iterate from 2 to 99. i think you have that setup correctly. but look what happens to the value of startValue WITHIN the while loop - you're changing it. in fact, i think what happens is each time you exit the while-loop, you've reset startvalue to 1, it gets incremented to 2 in the for condition, then you re-enter the while loop which resets startvalue back to 1...

what i would do is have a different variable as the control of the for loop, and use it to seed the variable you use in your while loop.

as a general note, i'd reccomend always using curly braces, even if there is only one line after a for, a while, or all if-else if-else statements like this:.


i've seen (and made) countless errors because the braces weren't there, i add a line, and everything doesn't get executed how i think it should.

Don't worry about part III until you get part II working. if you do that, you'll never get anythign done. it's best to focus on one part, get it working correctly, test it like crazy, and only when you are SURE it's perfect, start thinking about the next step.

at least, that's my advice.
[ September 22, 2004: Message edited by: fred rosenberger ]
Nigel Browne
Ranch Hand

Joined: May 15, 2001
Posts: 673
Your very close, but are getting yourself into an endless loop by using the startValue int as the test for both your loops. I won't give you the answer but will comment your code so that you can put in the required fixes.
Namhoon Lee
Greenhorn

Joined: Sep 16, 2004
Posts: 5
Sorry about cross-posting agian. And Thank you all for reading and answering my question. It really helped me a lot.
I think I'm gonna have to bother you guys often =) Thank you again~!

c.f> I will cross-post this so that I can thank everyone.
 
Consider Paul's rocket mass heater.
 
subject: Is there any one who can help me solve this?