Question

So i need help with this program, This program is supposed to ask the user to input their weight on earth so I would put "140" then it would ask "enter planet name, Min, Max or all" and if the user typed in all it would display all the planets and your weight on them. Right now Im suck on the part where im required to do a switch statement with a linear search. Below this im going to put what the output should look like when complete.

.jGRASP exec: java PlanetWeight Please enter a weight on Earth (0 to quit): 14 Please enter a planet name or Max, Min, All: al
import java.util.Scanner;
import java.util.Arrays;

public class PlanetWeight {
public static String[] planets = {"Mercury", "Venus", "Earth", "Mars", "Jupiter", "Saturn", "Uranus", "Neptune"};
public static double[] gravity = {0.39, 0.91, 1.00, 0.38, 2.87, 1.32, 0.93, 1.23};

public static int findByPlanet(String planet) {
for (int i = 0; i < planets.length; i++) {
if (planets[i].equalsIgnoreCase(planet)) {
return i;
}
}
return -1;
}

public static int findByGravity(double grvty) {
for (int i = 0; i < gravity.length; i++) {
if (gravity[i] == grvty) {
return i;
}
}
return -1;
}

public static double weightOnPlanet(double weight, int planet) {
return weight * gravity[planet];
}

public static void main(String[] args) {
Scanner in = new Scanner(System.in);
System.out.print("Enter your weight: ");
String planet = in.nextLine();
int index = findByGravity(grvty);
switch (planet)
{
case "Mercury": break;
case "Venus": break;
case "Earth": break;
case "Mars": break;
case "Jupiter": break;
case "Saturn": break;
case "Uranus": break;
case "Neptune": break;
default: break;
}
  
}
}

0 0
Add a comment Improve this question Transcribed image text
Answer #1

CODE:

import java.util.Scanner;
import java.util.Arrays;

public class PlanetWeight {
   public static String[] planets = {"Mercury", "Venus", "Earth", "Mars", "Jupiter", "Saturn", "Uranus", "Neptune"};
   public static double[] gravity = {0.39, 0.91, 1.00, 0.38, 2.87, 1.32, 0.93, 1.23};

   public static int findByPlanet(String planet) {
       for (int i = 0; i < planets.length; i++) {
           if (planets[i].equalsIgnoreCase(planet)) {
               return i;
           }
       }
       return -1;
   }

   public static int findByGravity(double grvty) {
       for (int i = 0; i < gravity.length; i++) {
           if (gravity[i] == grvty) {
               return i;
           }
       }
       return -1;
   }

   public static double weightOnPlanet(double weight, int planet) {
   return weight * gravity[planet];
   }

   public static void main(String[] args) {
       Scanner in = new Scanner(System.in);
       double weight;
       String opt="";
       System.out.print("Enter your weight on Earth(0 to quit): ");
       weight = in.nextDouble();     //taking weight from user
       in.nextLine();
       int planet,index;
       while(weight>0)
       {
           System.out.print("enter planet name, min, max or all: ");
           opt=in.nextLine();    //taking option from user
           switch (opt)
           {

               case "Mercury":
                   planet=findByPlanet("Mercury");
                   System.out.println(weight+" pounds is "+weightOnPlanet(weight,planet)+"on Mercury");
                   break;
               case "Venus":
                   planet=findByPlanet("Venus");
                   System.out.println(weight+" pounds is "+weightOnPlanet(weight,planet)+"on Venus");
                   break;
               case "Earth":
                   planet=findByPlanet("Earth");
                   System.out.println(weight+" pounds is "+weightOnPlanet(weight,planet)+"on Earth");
                   break;
               case "Mars":
                   planet=findByPlanet("Mars");
                   System.out.println(weight+"pounds is "+weightOnPlanet(weight,planet)+"on Mars");
                   break;
               case "Jupiter":
                   planet=findByPlanet("Jupiter");
                   System.out.println(weight+" pounds is "+weightOnPlanet(weight,planet)+"on Jupiter");
                   break;
               case "Saturn":
                   planet=findByPlanet("Saturn");
                   System.out.println(weight+" pounds is "+weightOnPlanet(weight,planet)+"on Saturn");
                   break;
               case "Uranus":
                   planet=findByPlanet("Uranus");
                   System.out.println(weight+" pounds is "+weightOnPlanet(weight,planet)+"on Uranus");
                   break;
               case "Neptune":
                   planet=findByPlanet("Neptune");
                   System.out.println(weight+"pounds is "+weightOnPlanet(weight,planet)+"on Neptune");
                   break;
               case "all":   //if user enters all then finding weights of all planets
                   for(int i=0;i<planets.length;i++)
                   {
                       planet=findByPlanet(planets[i]);
                       System.out.println(planets[i]+"\t"+weightOnPlanet(weight,planet));
                   }  
                   break;
               case "max":   //if user enters max
                   double max_gravity=gravity[0];
                   for(int i=0;i<gravity.length;i++)       //finding maximum gravity in gravity array
                   {
                       if(gravity[i]>max_gravity)
                       {
                           max_gravity=gravity[i];
                       }
                   }
                   index = findByGravity(max_gravity);
                   System.out.println(weight+" pounds is the maximum of "+weightOnPlanet(weight,index)+" on "+planets[index]);
                   break;
               case "min":   //if user enters min
                   double min_gravity=gravity[0];
                   for(int i=0;i<gravity.length;i++)    //finding minimum gravity in gravity array
                   {
                       if(gravity[i]<min_gravity)
                       {
                           min_gravity=gravity[i];
                       }
                   }
                   index = findByGravity(min_gravity);
                   System.out.println(weight+" pounds is the minimum of "+weightOnPlanet(weight,index)+" on "+planets[index]);
                   break;                              
               default:
                   break;
           }
           System.out.print("Enter your weight on Earth(0 to quit): ");
           weight = in.nextDouble();
           in.nextLine();
       }  

   }
}


OUTPUT:

Enter your weight on Earth(0 to quit): 14 enter planet name, min, max or all: al1 ercury 5.46 enus 12.74 Earth 14.0 Mars 5.32

Add a comment
Know the answer?
Add Answer to:
So i need help with this program, This program is supposed to ask the user to...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • I want to have a code that can pick whichever two planets in the solar system and find the distance of them no matter where in their orbit they are, but I keep getting errors. Can someone please help...

    I want to have a code that can pick whichever two planets in the solar system and find the distance of them no matter where in their orbit they are, but I keep getting errors. Can someone please help me fix it? f rom scipy import exp, pi, absolute, linspace import matplotlib. Ryplot as plt planet-I input ('Which planet do you want to pick for planet 1?") planet_2 input ('which planet do you want to pick for planet 27') distance...

  • Which pair of planets have semimajor axen that are the most dissimilar? O Earth and Mars...

    Which pair of planets have semimajor axen that are the most dissimilar? O Earth and Mars O Mercury and Earth O Venus and Mars O Mars and Jupiter QUESTION 2 Which object has the largest angular size as seen from Earth? O Sun Mercury O Venus O Mars O Jupiter O Saturn O Uranus O Neptune QUESTION 3 If you were flying through the solar system and your guidance system failed with your spacecraft moving in a random direction, what...

  • Can someone help me with this question, I do not know how to do the long-distance...

    Can someone help me with this question, I do not know how to do the long-distance in excel. If you could show me the Excel data as well that would be great. retention 10.18 The following are the average distances of the planets in the solar system from the 25 Distance (millions of miles) 47.163 67.235 92.960 141.61 313.00 483.60 886.70 1783.0 2794.0 3666.0 Planet No. Planet Mercury Venus Earth Mars Asteroids Jupiter Saturn Uranus Neptune Pluto 4 10 (a)...

  • acair ay four significant digits 5-1. Verify the value given in Table 5-3-1 for rsoi of...

    acair ay four significant digits 5-1. Verify the value given in Table 5-3-1 for rsoi of the following planets with respect to the Sun (a) Mercury (b) Neptune (c) Jupiter -(d) Venus TABLE 5-3-1 RADIUS OF SPHERE OF INFLUENCE (SOI) Planet AU Meters Nautical miles Mercury 0.000747 .117x 10* 6.163 x 108 5.781 108 5.627 x 10° 8.664 x 1010 60,710 334,900 502,500 314,200 x I01 26,200,000 30,580,000 5.172 x 10°28,110,000 47,090,000 1,804,000 0.00411 Venus Earth Mars Jupiter Saturn Uranus...

  • VISUAL STUDIO - WINDOWS FORM APP (C#) - PLEASE MAKE SURE TO SHOW ALL CODE &...

    VISUAL STUDIO - WINDOWS FORM APP (C#) - PLEASE MAKE SURE TO SHOW ALL CODE & ANSWER/FOLLOW ALL THE QUESTIONS IN THE ASSIGNMENT! Planet Info Help When planning this project, review the concept of accessing object properties on another form. Form2.IblMessage.Text = "1122.25" or you can put a string or object instead of numbers You can then create one form for the Planets information and set all the properties before you display the Planet Form For the pictures of the...

  • for future views please dont use it for spring 2020 Write a Perl program to accomplish...

    for future views please dont use it for spring 2020 Write a Perl program to accomplish each of the following on the file solar.txt (see link at the class homepage) 1. Print all records that do not list a discoverer in the eighth field. 2. Print every record after erasing the second field. Note: It would be better to say "print every record" omitting the second field . 3. Print the records for satellites that have negative orbital periods. (A...

  • Page 3 of 6 10. Which of the following is correct? a) The rotation axes of...

    Page 3 of 6 10. Which of the following is correct? a) The rotation axes of planets are always perpendicular to their orbital plane (the ecliptic). b) No planet's orbital inclination is more than 20 degrees from the ecliptic. c) Planets orbits are circular. d) Planets always rotate and revolve in the same direction. 11. Which of the following is not a real difference between asteroids and comets? a) Asteroids are made mostly of rock and comets are made mostly...

  • In the below box, we have the planets_df data from some data camp exercises. Use the...

    In the below box, we have the planets_df data from some data camp exercises. Use the data below to answer the following questions. (a)Write a line of code that gives me all the information about “Venus”. (b) Write a line of code that gives me the ‘diameter’ variable. (c) What would this line of code do: planets_df$diameter < 1? (d) What would this line of code give us: planets_df$planets[planets_df$type == “Gas giant”]? (6) In the below box, we have the...

  • Note: It should be the same in figure2 (Yellow Page). roblem find the weight of an...

    Note: It should be the same in figure2 (Yellow Page). roblem find the weight of an object/imatter on different planets it is important first to explore the To difference between mass and weight. Mass is a measurement o is the force generated by the gravitational attraction of one o change when we change planets. f the amount it contains and weight This mass does not However, if we went from Earth to Mars, weight would change less gravity than Earth....

  • Programming in C with comments/steps 1. Overview The purpose of this assignment is to give you...

    Programming in C with comments/steps 1. Overview The purpose of this assignment is to give you some experience with writing functions that take in arguments and return values, as well as writing a program that consists of multiple files. This assignment also requires the use of a switch statement, in addition to more practice with printf& scanf declaring variables, using loops, and using logical expressions. For this assignment, you will prompt the user in the main function to enter their...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT