0

I have a class called Planet that creates Planet objects with some attributes. Can I have an enum (not used to them yet) that stores and creates the planets? For example something like:

public enum planetEnum{
    earth (250, 0, 100, "blue"),
    mars (360, 0, 80, "red");

    planetEnum(double distance, double angle, double diameter, String col){
        new Planet(distance, angle, diameter, col);
    }
}

Hoping that in the end I can refer to mars and earth as objects, i.e. : earth.getDistance()

I hope its clear :) Thanks in advance!

Edit: Thanks for your responses. I've found and read already the oracle tut on enums but I couldn't find what I was actually looking for. Because it's a task to learn the basics of inheritance, I need to keep the Planet class separate and to create Planet objects, so I cant actually put the constructor in the enum. Is there a way to refer to the Planet class and create from it, from inside of the enum?

Thanks again!

1
  • EDIT: instead of PlanetEnum having 4 fields, it should then have a single field of type Planet. Simple delegation. Commented Oct 19, 2014 at 21:06

3 Answers 3

2

Yes it is possible, take a look also to that link http://docs.oracle.com/javase/tutorial/java/javaOO/enum.html

public enum Planet {
    MERCURY (3.303e+23, 2.4397e6),
    VENUS   (4.869e+24, 6.0518e6),
    EARTH   (5.976e+24, 6.37814e6),
    MARS    (6.421e+23, 3.3972e6),
    JUPITER (1.9e+27,   7.1492e7),
    SATURN  (5.688e+26, 6.0268e7),
    URANUS  (8.686e+25, 2.5559e7),
    NEPTUNE (1.024e+26, 2.4746e7);

    private final double mass;   // in kilograms
    private final double radius; // in meters

    Planet(double mass, double radius) {
        this.mass = mass;
        this.radius = radius;
    }

    private double mass() { return mass; }
    private double radius() { return radius; }

    // universal gravitational constant  (m3 kg-1 s-2)
    public static final double G = 6.67300E-11;

    double surfaceGravity() {
        return G * mass / (radius * radius);
    }
    double surfaceWeight(double otherMass) {
        return otherMass * surfaceGravity();
   }
   public static void main(String[] args) {
       if (args.length != 1) {
            System.err.println("Usage: java Planet <earth_weight>");
            System.exit(-1);
       }
       double earthWeight = Double.parseDouble(args[0]);
       double mass = earthWeight/EARTH.surfaceGravity();
       for (Planet p : Planet.values())
           System.out.printf("Your weight on %s is %f%n",
                         p, p.surfaceWeight(mass));
    }
 }

If you run Planet.class from the command line with an argument of 175, you get this output:

$ java Planet 175
Your weight on MERCURY is 66.107583
Your weight on VENUS is 158.374842
Your weight on EARTH is 175.000000
Your weight on MARS is 66.279007
Your weight on JUPITER is 442.847567
Your weight on SATURN is 186.552719
Your weight on URANUS is 158.397260
Your weight on NEPTUNE is 199.207413

« Previou

Sign up to request clarification or add additional context in comments.

Comments

1

Yes, but you don't really need the Planet class because you can define all the same fields and methods inside PlanetEnum. The only difference is that an enum has a predefined set of instances and you can't create more.

Comments

0

Yes that is possible. Enums are great, because they are guaranteed to behave like singletons. However, you will have to make sure that you have all Planets in the list you want, because you won't be able to add any. Extra downside, you will have to hardcode all fixed numbers and properties like distance, color etc...

If you want to access you Planet-class objects them easily, you can make a Map and store all of them in there. Then you could do something like:

PLANETS.get("mars").getDistance();

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.