Programmers often move from place to place, but only if they want to. That's why they love converters so much. Well, maybe that's not why. Still, we will practice writing them. Now imagine that you have to work at a particular location: the Moon. Let's implement a method that converts your Earth weight to your lunar weight.
Even to the moon!
- 7
Locked
Comments (16)
- Popular
- New
- Old
You must be signed in to leave a comment
NightroWave
16 March, 12:15
To people saying "I got it on my second try": ok genius but people don't come here just to read how smart you are.
This exercise is short, not easy though. It shouldn't be tagged as easy since there are some elements not explained previously.
The key is "The method should return a double"
So now, you know we need two elements.
1. double, which works like int adding the use of decimals.
2. return. We've seen these function before appearing in some exercise, probably you didn't know what is it for though. In this case, it works exactly as a System.out.println().
The last line of the example could be distracting, just avoid it.
Let's focus on the exercise instructions.
1. "To avoid having to manually calculate it each time, implement a getWeight(int) method ..."
✔️This is already done in the second block: public static double getWeight(int earthWeight) {
2. "...returns the weight of that person on the moon (in newtons). The method should return a double.". So, create a double with a return.
Note: You will need to use the 17% at the begining of the exercise here.
double _____ = (numbers) * earthWeight; //very careful with capitals in earthWeight
return _____;
I didn't get it on my second try. I needed further more tries. Hope these steps help you.
+1
Lunita
23 December 2022, 00:56
I got it on my second try. Just had to add .0 to the 100 for the result to be type DOUBLE.
+1
Jeff Kenney
14 December 2021, 06:23
Okay, so I'm really getting the hang of the conversion program formula:
Basically, all you need for ANY simple conversion program (lbs to kg...in to cm...feet to meters...)
IS:
A) One line of code to define a conversion formula (you only need one)
Example: double VariableOne = 1/2*VariableTwo+37;
B) State which variable to calculate for with the "return"
0
Víctor Alfonso Martínez Gómez
10 August 2021, 21:40
Ready
0
Paul.ATX
25 July 2021, 03:42
Am I the only one that's pretty far lost at this point?
+6
Anonymous #10681117
16 December 2020, 09:32
When I googled the formula it gives a different value. Why do we calculate by the hint and not the actual formula for a precise result?
+1
Roman
18 December 2020, 07:18
Yes, you are right. Wiki says :
"The acceleration due to gravity on the surface of the Moon is about 1.625 m/s2, about 16.6% that on Earth's surface or 0.166 ɡ."
We just rounded it up)
+5
Bosch Testing Team
14 November 2020, 18:44
Hi guys, can some one help me understand why code on the left returns correct decimal value but code on the right does not ? Tnx

+4
Yusuf el Turki
18 May 2021, 13:47
Same problem.0.17 and *17 / 100 is same thing but the result is different. I also dont understand.
0
David Cáceres
10 August 2021, 07:48
I doubt this person is gonna read this 2 years later but I'll answer it anyway for people that may have the same issue now. even if you use the "double" command, if You dont input any decimals on any number of the calculation, java is gonna treat it like and "int" anyway. So if you input *17.0/100 instead for example, it would work properly.
+3
Raunak Adhikari
15 August 2020, 10:23
take double lunar weight and gravity....assign the value of gravity to 0.17..then lunarweight = gravity multiplied by earth weight.....then return the lunar weight........
+1
Dusernajder
14 April 2019, 19:23
So you already taught us how to use static variables, but if I try to use it in the task it thorws me an error, I think it would be better than magic numbers.
I've tried to use this:
public static final double LUNAR_GRAVITY = 0.17;
0
WILLIAM WILSON
20 September 2019, 14:32
the static is already provided in the exercise
+2
Dusernajder
22 September 2019, 11:55
What do you mean?
I can not see any static variable declared in the source.
I just mentioned that it would be more convenient to use a final declaration.
0
Bryce Lindley
19 January 2019, 22:53
Please excuse my dear aunt sally
+3