Question: import java.util.Random; public class Main { public static void main ( String [ ] args ) { / / Generate a random gravity value between

import java.util.Random;
public class Main {
public static void main(String[] args){
// Generate a random gravity value between 1.0 and 10.5
double randomGravity =1.0+(10.5-1.0)* new Random().nextDouble()
// Define the gravity values for each planet
double earthGravity =9.795;
double moonGravity =1.623;
double marsGravity =3.711;
// Determine the planet based on the random gravity value
String planet;
if (Math.abs(randomGravity - earthGravity)=0.25){
planet = "Earth";
} else if (Math.abs(randomGravity - moonGravity)=0.25){
planet = "Moon";
} else if (Math.abs(randomGravity - marsGravity)=0.25){
planet = "Mars";
} else {
planet = "Unknown"; // If the value doesn't match any planet with
}
// Output the result
System.out.println("Random Gravity Value: "+ randomGravity);
System.out.println("Craft is on "+ planet);
}
}
How would you write this code without adding "String planet;" on line 14?
import java.util.Random; public class Main {

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!