Even to the moon!

  • 7
  • Locked
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.
You can't complete this task, because you're not signed in.
Comments (16)
  • Popular
  • New
  • Old
You must be signed in to leave a comment
Level 3 , San Francisco, Norway
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.
Level 4
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.
Jeff Kenney
Level 3 , United States
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"
Víctor Alfonso Martínez Gómez
Level 3 , Manizales, Colombia
10 August 2021, 21:40
Level 3 , Austin, United States
25 July 2021, 03:42
Am I the only one that's pretty far lost at this point?
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?
Level 41
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)
Bosch Testing Team
Level 12 , Karlsruhe, Germany
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
Yusuf el Turki
Level 5 , Rhein, Germany
18 May 2021, 13:47
Same problem.0.17 and *17 / 100 is same thing but the result is different. I also dont understand.
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.
Raunak Adhikari
Level 12 , India
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........
Level 20 , Budapest, Hungary
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;
Level 3 , United States
20 September 2019, 14:32
the static is already provided in the exercise
Level 20 , Budapest, Hungary
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.
Bryce Lindley
Level 9 , Olympia, United States
19 January 2019, 22:53
Please excuse my dear aunt sally