Enumerated Types Worksheet

Complete the following worksheet in groups of 2-3. Put all group members' names at the top of this sheet and submit your solutions at the end of class.

Creatures

  1. Look at the following code for Creature.java.
    // Declare the Creature enumerated type.
    public enum Creature { HOBBIT, ELF, DRAGON }
    This is the entire contents of the file Creature.java.
  2. What will be produced when this file compiles?
  3. Notice that there is no visibility modifier on the enum values. What do you think the visibility is?
  4. How many Creature objects are there?
  5. Can you make any more Creature objects by using this class? Why / why not?

Using Creatures

Look at the code for CreatureDemo.java:
/**
   This program demonstrates an enumerated type.
*/
public class CreatureDemo
{
    public static void main(String[] args)
        {
            // Declare a Creature variable and assign it a value.
            Creature good;
            Creature bad;
            Creature small;
            good = Creature.HOBBIT;
            bad = Creature.DRAGON;
            small = Creature.ELF;

            // The following statement displays HOBBIT.
            System.out.println("I am a(n) " + good);

            // "Built in" methods
            System.out.println("I am a(n) " + bad.name());
            System.out.println("Is " + good + " the same as " +
                               bad + "? " + good.equals(bad) );

            if(good.compareTo(small) > 0)
                System.out.println(good + " is better than " + small + ".");
            else if (good.compareTo(small) < 0)
                System.out.println(small + " is better than " + good + ".");
            else
                System.out.println(small + " and " + good + " are the same.");

            System.out.println("The position corresponding to bad is " +
                               bad.ordinal());

            System.out.println("I will now display the Creature objects.");
            for (Creature c : Creature.values())
                System.out.println("\t" + c);
        }
}
  1. What will be printed with this code executes?
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
  2. Write a method named greetCreature that takes a Creature object as an argument, and returns a string containing an appropriate greeting:
    • Hobbit: "Would you like some breakfast?"
    • Elf: "My, what pointy ears you have."
    • Dragon: "Aaaahahhraaag!!! My hair is in fire!"
    • Anything else: "What are you?"
    Your method should use a switch statement to determine which message will be returned.
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    

Enum Types are Classes

Enum types are specialized classes and may include user defined instance variables and methods. Look at the following Planets example from the Java Tutorial.
/*Copyright 2008, 2010 Oracle and/or its affiliates. 
 See html source for complete license information. 
*/
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);

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

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

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

    public double surfaceGravity() {
        return G * mass / (radius * radius);
    }

    public double surfaceWeight(double otherMass) {
        return otherMass * surfaceGravity();
    }

    public static void main(String[] args) {
        if (args.length != 1) {
            System.err.println("Usage: java Planet ");
            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));
    }
}

  1. How many Planet objects are defined by this class?
  2. What data do these objects contain?
  3. Does this class contain a constructor?
  4. Can this constructor be called by outside classes to build their own Planet?
  5. Which Planet has the lowest ordinal value? The highest?
  6. What methods do Planet objects provide? (Include both built-in and user-defined methods.)
  7. In words, describe what the main method does.
  8. Specifically, what do you think the role of the for loop is? Think about what the enhanced for loop does?

Designing with Enumerated Types

  1. In what general circumstances would enumerated types have some advantages over other kinds of classes we have studied.
  2. Think about our work in 139 (or your own equivalent) and the work thus far in 239. Is there any application that might have been able to use an enumerated type to limit the set of values with which we could work?
  3. Write the code below for a simple playing card class. Use enumerated types as appropriate.
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    

These exercises are based on a worksheet designed by Nancy Harris.