Enums with Fields

Lab Java Intermediate 20 points

Create enum Planet with MERCURY(0.38), EARTH(1.0), MARS(0.38), JUPITER(2.53) gravity factors and a method weight(double earthWeight). Read an Earth weight and print the weight on every planet (1 decimal) using Planet.values().

Input

One decimal number.

Output

Four lines: PLANET weight

Example

Input

70

Output

MERCURY 26.6
EARTH 70.0
MARS 26.6
JUPITER 177.1
Sample input available Sample output available
Editor
Java

Hints

Enum constants can take constructor arguments.

values() returns all constants in order.

Solution explanation

Java enums are full classes: they can have fields, constructors and methods.

Show solution
import java.util.Scanner;

enum Planet {
    MERCURY(0.38), EARTH(1.0), MARS(0.38), JUPITER(2.53);

    private final double gravity;

    Planet(double gravity) {
        this.gravity = gravity;
    }

    double weight(double earthWeight) {
        return earthWeight * gravity;
    }
}

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        double w = sc.nextDouble();
        for (Planet p : Planet.values()) {
            System.out.printf("%s %.1f%n", p, p.weight(w));
        }
    }
}
Output
Answer
HOME LEARN COMMUNITY DASHBOARD