1
\$\begingroup\$

I have been using this site for a couple of years for great answers and a ton of help. I wrote this program for my class project due 1/23. I am just wondering what you would recommend me changing or improving. I think I have some repetitive code, but I have not learned about defs in Java yet.

package project2;

import java.util.Scanner;

/**
 * @author Sartaj Singh
*/
public class Project2 {

    public static void main(String[] args) {

        int choice = 0;
        double weight;
        double earth = 9.81;
        double moon = 1.622;
        double mercury = 3.7;
        double venus = 8.87;
        double jupiter = 24.79;

        System.out.println("Welcome to Planetary Weight Calculator: ");
        Scanner input = new Scanner(System.in);
        System.out.print("Enter your weight in pounds (lb): ");
        weight = input.nextDouble();
        while (weight <= 0) {
            System.out.println("Invalid Input - Plase enter a value higher than 0 ");
            System.out.print("Enter your weight in pounds (lb): ");
            weight = input.nextDouble();
        }
        System.out.println("/////MENU/////");
        System.out.println("1 - Moon");
        System.out.println("2 - Mercury");
        System.out.println("3 - Venus");
        System.out.println("4 - Jupiter");
        System.out.print("Pick a number 1-4: ");
        choice = input.nextInt();
        while (choice <= 0 || choice >= 5) {
            System.out.println("/////MENU/////");
            System.out.println("1 - Moon");
            System.out.println("2 - Mercury");
            System.out.println("3 - Venus");
            System.out.println("4 - Jupiter");
            System.out.print("Pick a number 1-4: ");
            choice = input.nextInt();
        }
        switch (choice) {
            case 1:
                System.out.println("You selected Moon, your weight on Moon is: ");
                System.out.println(weight * (moon / earth) + " Lbs");
                break;
            case 2:
                System.out.println("You selected Mercury, your weight on Mercury is: ");
                System.out.println(weight * (mercury / earth) + " Lbs");
                break;
            case 3:
                System.out.println("You selected Venus, your weight on Venus is: ");
                System.out.println(weight * (venus / earth) + " Lbs");
                break;
            case 4:
                System.out.println("You selected Jupiter, your weight on Jupiter is: ");
                System.out.println(weight * (jupiter / earth) + " Lbs");
                break;
        }

        System.out.println("Thank you for using the Planetary Weight Calculator");
        System.out.println("Hope you will use it again! ");

    }

}
\$\endgroup\$
0

3 Answers 3

2
\$\begingroup\$

I would create a class for the Planets for a more scalable and Object Oriented design, as detailed below

  1. The class would store Earth's G in a constant, because it does not change in any planet's calculation.
  2. The constructor should require the planet's own G value and it's name (for the Print statements, discussed further below).
  3. You can also add a method to calculate the resultant weight based on a given weight value, to follow along with the Object Oriented approach.

    public class Planet {
    
    final double earthG = 9.81;
    double myG;
    public String planetName;
    
    public Planet(String planetName, double myG){
        this.myG = myG;
        this.planetName = planetName;
    }
    
    public double calculateWeight(double weight){
        return weight * myG/earthG;
    }
    }
    

In the main method I would create an array of Planets for readability,

Planet[] planets = new Planet[]{new Planet("Moon", 1.622),
                                new Planet("Mercury", 3.7),
                                new Planet("Venus", 8.87),
                                new Planet("Jupiter", 24.79)};

And use a Do-While loop to populate the loop instead of the While loop. The Do-While checks for the condition at the end, so the first print doesn't requires an input.

do{
   ...
  } while(expression);

You can also use the array to populate the menu itself using for loop. This is especially helpful if you have a large number of planets/items in the array.

do {
    System.out.println("/////MENU/////");
    for(int i=0;i<planets.length;i++){
        System.out.println(i + " - " + planets[i].planetName);
       }
    System.out.print("Pick a number 0 - " + planets.length + ": ");
    choice = input.nextInt();
    } while (choice <= 0 || choice >= 5);

Using the array has a massive advantage here for the output statement. Since the choice corresponds to the planet's position in the array, you can simply call the appropriate planet's method as planets[choice].calculateWeight(weight). Following two lines will suffice the purpose of the previous Switch-Case.

System.out.println("You selected " + planets[choice].planetName + ", your weight on " + planets[choice].planetName + " is: ");
System.out.println(planets[choice].calculateWeight(weight) + " Lbs");
\$\endgroup\$
1
  • \$\begingroup\$ Not to be pedantic but in java method names should start with a lower-case letter. \$\endgroup\$ Jan 16, 2016 at 19:59
1
\$\begingroup\$

Your menu is written twice in the code. To avoid the repetition, try using a do-while loop instead.

\$\endgroup\$
0
\$\begingroup\$

My take (not thoroughly tested):

package project2;

import java.util.Scanner;

/**
 * @author Sartaj Singh
 */
public class Project2 {

    public static void main(String[] args) {

        int choice = 0;
    // -1 to make sure its negative
        double weight = -1.;
    // the 0/"" is so I can match array indexes to numbers
    double[] gravs = {0, 3.7/9.81, 8.87/9.81, 1.622/9.81, 24.79/9.81};
    String[] names = {"", "Mercury", "Venus", "Moon", "Jupiter"};

        System.out.println("Welcome to Planetary Weight Calculator: ");
        Scanner input = new Scanner(System.in);

    do {
        System.out.print("Enter your weight in pounds (lb): ");
            weight = input.nextDouble();
        if (weight <= 0) {
        System.out.println("Invalid Input - Plase enter a value higher than 0 ");
        }
    } while (weight <= 0);

    do {
        System.out.println("/////MENU/////");
        for (int i=1; i<=4; i++) {
        System.out.format("%d %s\n", i, names[i]);
        }
        choice = input.nextInt();
    } while (choice <= 0 || choice >=5);

    System.out.format("You selected %s, your weight on %s is %f lbs\n", names[choice], names[choice], weight*gravs[choice]);
        System.out.println("Thank you for using the Planetary Weight Calculator");
        System.out.println("Hope you will use it again! ");
    }
}
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.