In: Computer Science
Hi all! Looking for good explanations as to how this problem is solved without the constructor.
1. In this problem, we will write and use a static class (remember static classes do not operate on an object.) Your weight is actually the amount of gravitational attraction exerted on you by the
Earth. Since the Moon’s gravity is only one-sixth of the Earth’s gravity, on the Moon you would weigh only one-sixth of the Earth’s gravity. Write an application that inputs the user’s Earth weight as an integer and outputs her/his weight on the Moon, Mercury, Venus, Jupiter or Saturn. Use the values from this table.
Planet/Moon |
Multiply the Earth Weight by |
Moon |
0.1666 |
Mercury |
0.4 |
Venus |
0.9 |
Jupiter |
2.5 |
Saturn |
1.1 |
You will have a tester class and a planet weight calculation class.
Create a static class for planet weight calculation. (A class is considered static if all of its
methods are static.)
o Youdonotwriteaconstructor(thinkaboutwhy).
o Write a different method for each of the planets. Each method
will accept as input the
“earth” weight. It will return the calculated planet weight. Each method should multiply the “earth” weight supplied as an argument by corresponding planet conversion factor to get the equivalent “planet” weight.
o Use constants for your weight conversion factors (the table, above). • Create a tester class
o Get user input with a scanner object
o Ask the user to enter the “earth” weight.
o Call each of the methods in the static class and print the
results.
SOLUTION-
I have solve the problem in Java code with comments and screenshot
for easy understanding :)
CODE-
PlanetWeight.java
//static class definition
public class PlanetWeight {
//static function to calculate corresponding
value on the planet
public static double wieghtOnMoon(double
weight){
//returning calculated
value on the planet
return
weight*0.1666;
}
//static function to calculate corresponding
value on the planet
public static double wieghtOnMercury(double
weight){
//returning calculated
value on the planet
return weight*0.4;
}
//static function to calculate corresponding
value on the planet
public static double wieghtOnVenus(double
weight){
//returning calculated
value on the planet
return weight*0.9;
}
//static function to calculate corresponding
value on the planet
public static double wieghtOnJupiter(double
weight){
//returning calculated
value on the planet
return weight*2.5;
}
//static function to calculate corresponding
value on the planet
public static double wieghtOnSaturn(double
weight){
//returning calculated
value on the planet
return weight*1.1;
}
}
-----------------------------------------------------
PlanetWeightTester.java
//importing required class
import java.util.Scanner;
//tester class definition
public class PlanetWeightTester {
//defining main method
public static void main(String[] args) {
//creating scanner
object to get user input
Scanner input=new
Scanner(System.in);
//variable to store the
intermediate values
double
weightOnEarth,weightOnPlanet;
//asking user tot enter
the weight
System.out.print("Enter
the weight on Earth: ");
//storing the user input
into the variable
weightOnEarth=input.nextFloat();
//calling function to
get corresponding weight on the planet
weightOnPlanet=PlanetWeight.wieghtOnMoon(weightOnEarth);
//printing the message
for the user with calculated value
System.out.println("Weight "+weightOnEarth+" on Earth is equal to
"+weightOnPlanet+" on Moon");
//calling function to
get corresponding weight on the planet
weightOnPlanet=PlanetWeight.wieghtOnMercury(weightOnEarth);
//printing the message
for the user with calculated value
System.out.println("Weight "+weightOnEarth+" on Earth is equal to
"+weightOnPlanet+" on Mercury");
//calling function to
get corresponding weight on the planet
weightOnPlanet=PlanetWeight.wieghtOnVenus(weightOnEarth);
//printing the message
for the user with calculated value
System.out.println("Weight "+weightOnEarth+" on Earth is equal to
"+weightOnPlanet+" on Venus");
//calling function to
get corresponding weight on the planet
weightOnPlanet=PlanetWeight.wieghtOnJupiter(weightOnEarth);
//printing the message
for the user with calculated value
System.out.println("Weight "+weightOnEarth+" on Earth is equal to
"+weightOnPlanet+" on Jupiter");
//calling function to
get corresponding weight on the planet
weightOnPlanet=PlanetWeight.wieghtOnSaturn(weightOnEarth);
//printing the message
for the user with calculated value
System.out.println("Weight "+weightOnEarth+" on Earth is equal to
"+weightOnPlanet+" on Saturn");
}
}
SCREENSHOT -
IF YOU HAVE ANY DOUBT PLEASE COMMENT DOWN BELOW I
WILL SOLVE IT FOR YOU:)
----------------PLEASE RATE THE ANSWER-----------THANK
YOU!!!!!!!!----------