Jump to content

So I'm new to this whole coding thing, and I did something for a school project in java. I'm supposed to create a program where people can select a planet and view info about it. Here is my attempt, let me know if I did well and if I could improve in any way. Thanks!

 

 

import java.util.Scanner;

 
public class AstronomyHelper
{
 
   public static void main(String[] args)
   {
 
   //Declare a variable to hold the user's menu selection
   int menuSelection;
 
   //Declare a variable to hold the different planets
   string Mercury, Venus, Earth, Mars;
 
 
   //Create a scanner object for the keyboard input
   Scanner keyboard = new Scanner(System.in);
 
 
      do 
      {
         //Display the menu and get the user's selection
         displayMenu(menuSelection);
 
 
         //Display the information for the user's selection
         switch(menuSelection)
         {
 
            case 1: 
                  System.out.println("\t\t\t\t\t\t\t\t\t\t MERCURY");
                  System.out.println("_________________________________________________________");
 
                  System.out.println("Average Distance from the sun:  57.9 million kilomenters");
                  System.out.println("Mass:  3.31 x 10^23 kg");
                  System.out.println("Surface Temperature:  -173 to 430 degrees Celsius");
 
                  break;
 
 
           case 2: 
                  System.out.println("\t\t\t\t\t\t\t\t\t\t VENUS");
                  System.out.println("_________________________________________________________");
 
                  System.out.println("Average Distance from the sun:  108.2 million kilometers");
                  System.out.println("Mass:  4.87 x 10^24 kg");
                  System.out.println("Surface Temperature:  472 degrees Celsius");
 
                  break;
 
           case 3: 
                  System.out.println("\t\t\t\t\t\t\t\t\t\t EARTH");
                  System.out.println("_________________________________________________________");
 
                  System.out.println("Average Distance from the sun:  149.6 million kilometers");
                  System.out.println("Mass:  5.967 x 10^24 kg");
                  System.out.println("Surface Temperature:  -50 to 50 degrees Celsius");
 
                  break;
 
           case 4: 
                  System.out.println("\t\t\t\t\t\t\t\t\t\t MARS");
                  System.out.println("_________________________________________________________");
 
                  System.out.println("Average Distance from the sun:  227.9 million kiometers");
                  System.out.println("Mass:  0.6424 x 10^24 kg");
                  System.out.println("Surface Temperature:  -140 to 20 degrees Celsius");
 
                  break;
 
         }
 
       while(menuSelection !=5)
       }       
 
 
 
         //Call the displayMenu method to display the menu options and get the user's selection
         public displayMenu()
         {
         System.out.println("Please select a planet to view details about it's: ");
         System.out.println("average distance from the sun, mass, and surface temperature.");
         System.out.println("-------------------------------------------------------------");
         System.out.println("1. Mercury");
         System.out.println("2. Venus");
         System.out.println("3. Earth");
         System.out.println("4. Mars");
         System.out.println("5. EXIT the program");
         System.out.println("Enter your selection:  ");
 
         menuSelection = keyboard.nextInt();
 
 
            //Validate the menu selection
            while (menuSelection < 1 || menuSelection > 5)
            {
               System.out.print("This is an invalid selection.");
               System.out.print("Enter a selection from 1-5: ");
 
               menuSelection = keyboard.nextInt();
            }
          }
}
Link to comment
https://linustechtips.com/topic/175347-astronomy-helper-program/
Share on other sites

Link to post
Share on other sites

here, suggestions, take 'em!

 

- use the code tag

 

- does this even compile/work? you call displayMenu(menuSelection), but there is no displayMenu(int) definition in the class

also, i'd say that the selection should be the return value of the function

 

- instead of printing out the planet infos 'brutally', define a Planet class and create 4 planet objects, then just define a Planet.printInfo() method to display a planet's infos

 

- there are syntax errors/missing keywords, but the compiler tells you about them already

Link to comment
https://linustechtips.com/topic/175347-astronomy-helper-program/#findComment-2340857
Share on other sites

Link to post
Share on other sites

So I'm new to this whole coding thing, and I did something for a school project in java. I'm supposed to create a program where people can select a planet and view info about it. Here is my attempt, let me know if I did well and if I could improve in any way. Thanks!

 

 

 

I improved it for you.

 

If you are having to repeat the same logic or lines in your code thenyou are doing something wrong.

import java.util.ArrayList;import java.util.Scanner;public class PlanetsThing {    private static final String LINE = "_________________________________________________________";    private static final String INDENT = "\t\t\t\t\t\t\t\t\t\t ";    private ArrayList<Planet> planets = new ArrayList<Planet>();    private Scanner keyboard = new Scanner(System.in);    public static void main(String[] args){        ArrayList<Planet> planets = new ArrayList<Planet>();        Scanner keyboard = new Scanner(System.in);        int selection = -1;        planets.add(new Planet("Earth", "100", "200", "1231231"));        planets.add(new Planet("Mercury", "100", "200", "1231231"));        planets.add(new Planet("Mars", "100", "200", "1231231"));        planets.add(new Planet("Pluto", "100", "200", "1231231"));        planets.add(new Planet("Venus", "100", "200", "1231231"));        while (selection != 5){            printMenu();            selection = keyboard.nextInt();            if(selection > 0 & selection < 5){                printPlanet(planets.get(selection));            }        }        System.out.println("Exiting");    }    private static void printMenu(){        System.out.print("\n\nPlease select the planet you want to see information about" + "\n"  + LINE + "\n"  + "1. Mercury\n2. Venus\n3. Earth\n4. Mars\n5. Exit\n");    }    private static void printPlanet(Planet planet){        System.out.print("\n\n" + INDENT + "\n" + LINE + planet.getName() + "\nAverage distance from the Sun: " + planet.getDistanceFromSun() + "\nMass: " + planet.getMass() + "\nSurface Temperature: " + planet.getSurfaceTemp());    }    private static class Planet{        private String mass;        private String distanceFromSun;        private String surfaceTemp;        private String name;        public Planet(String name, String mass, String distanceFromSun, String surfaceTemp){            this.mass = mass;            this.distanceFromSun = distanceFromSun;            this.surfaceTemp = surfaceTemp;            this.name = name;        }        public String getMass() {            return mass;        }        public void setMass(String mass) {            this.mass = mass;        }        public String getDistanceFromSun() {            return distanceFromSun;        }        public void setDistanceFromSun(String distanceFromSun) {            this.distanceFromSun = distanceFromSun;        }        public String getSurfaceTemp() {            return surfaceTemp;        }        public void setSurfaceTemp(String surfaceTemp) {            this.surfaceTemp = surfaceTemp;        }        public String getName() {            return name;        }        public void setName(String name) {            this.name = name;        }    }}
Link to comment
https://linustechtips.com/topic/175347-astronomy-helper-program/#findComment-2341432
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×