• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Paul Clapham
  • Ron McLeod
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Roland Mueller
  • Piet Souris
Bartenders:

Help with While Loops

 
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I need some help writing a program. My professor is the worst at explaining how to do these, and expects us know how to do this in a beginner java course

Write a Java application that prompts for the distance to be traveled in kilometers (you
will need to convert this to meters). Then, determine the number of burns required to
reach the destination within 35 meters. Output the burn number, how far the probe
traveled for the burn, and the remaining distance to reach its destination. You must use
a While loop in your script.

this is what the output should look like
Enter the distance to be traveled in kilometers:
50
Burn 1: Traveled 30000 meters
20000 meters to go
Burn 2: Traveled 12000 meters
8000 meters to go
Burn 3: Traveled 4800 meters
3200 meters to go
Burn 4: Traveled 1920 meters
1280 meters to go
Burn 5: Traveled 768 meters
512 meters to go
Burn 6: Traveled 307.2 meters
204.8 meters to go
Burn 7: Traveled 122.88 meters
81.92 meters to go
Burn 8: Traveled 49.152 meters
32.768 meters to go
You made it to Jupiter in 8 burns

additional info
determine the actual number of burns required for your spacecraft to travel from
Earth to Jupiter. Note that the average distance from Earth to Jupiter is 778412028 million km.
1. The Burn and distance traveled and the “meters to go” should appear on two
lines as shown in the sample output. Note that this print should be done within
the while loop.
2. “meters to go” on the second line of the burn information should be lined up with
“Traveled x meters” as shown in the Sample Output. To do this, you should use
the tab escape sequence, \t
3. For the distances, use a DecimalFormat object that prints only to three decimal
places.

Thanks for any feedback.
 
Marshal
Posts: 80874
506
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Welcome to the Ranch

So, he didn't explain that very well? Another thing I bet he didn't explain is that you have a piece of expensive hardware obstructing your progress with this assignment: your computer. Turn it off.
What you need is paper pencil and eraser; the last is probably the most important tool
Write down on the paper how you would do that exercise by hand. Once you have got that down to word of one syllable without using anything which looks like Java® code at all, you will be ready to leave the monastery, Grasshopper turn your computer back on.
I get the impression each burn takes your spaceship a certain distance, the power decreasing so the distance is 40% of the previous burn.
another thing: you mustn't try to do everything. You need to try a little bit and then try the next bit. You run and test your program at every stage.
 
Campbell Ritchie
Marshal
Posts: 80874
506
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Shaylen Patel wrote: . . . Note that the average distance from Earth to Jupiter is 778412028 million km. . . .

I would check that, if I were you. I think that is 1000000 times too high.
 
Ranch Hand
Posts: 48
Mac OS X Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Well, I actually attempted this in the 15 minutes I had during my break in my calculus lecture.

I AM STUMPED at why this gives me an infinite loop.. Anyone can clarify this ?

 
Campbell Ritchie
Marshal
Posts: 80874
506
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Never close a Scanner, Reader, etc pointing to System.in. Once you close System.in, you can never reopen it.
I presume you are repeatedly finding you have travelled the same distance, 60% of the original.
You also realise the test in the while loop can cause you to overshoot your target badly.
 
John Vorona
Ranch Hand
Posts: 48
Mac OS X Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Whats wrong with closing the scanner at the end of your program ?
And whats wrong with doing the calculations in the while loop? how else can it update the distance?
 
Campbell Ritchie
Marshal
Posts: 80874
506
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
At the end of the program it is unnecessary. Anywhere else it is dangerous. Don't get into bad habits and justify them by saying they do no harm at the end of the program.
 
Campbell Ritchie
Marshal
Posts: 80874
506
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
There is nothing wrong with doing the calculation in the loop. There is much wrong with doing the wrong calculation, however.
 
Campbell Ritchie
Marshal
Posts: 80874
506
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Go back and go through the first two or three iterations of the loop with a pencil and you will (i hope) see why you are getting an infinite loop.
 
John Vorona
Ranch Hand
Posts: 48
Mac OS X Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Silly me, long night :P.

Here is the working code;



Also, to the OP. Just modify my algorithm to get what you want. Also for the 3 decimal value, try using the "printf" function. Look it up on the java API if you're unsure of how to use it.

 
Campbell Ritchie
Marshal
Posts: 80874
506
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
That will work (well done ), but it is a different algorithm altogether from what OP is supposed to produce. The specification said NumberFormat, so he has to use NumberFormat, even though I think (as you probably do) that NumberFormat is more‑or‑less obsolete.
 
reply
    Bookmark Topic Watch Topic
  • New Topic