Spring 2008 Mark Fontenot CSE Honors Principles of Computer Science I Note Set 17
Note Set 17 Overview Back To The … Enums
Before Enums: The enum “pattern” public class StudentType { public static final int FRESHMAN = 1; public static final int SOPHOMORE = 2; public static final int JUNIOR = 3; public static final int SENIOR = 4; public static final int GRADUATE = 5; } Problems?? Yes. 1.Not Typesafe = its just an int 2.Printed values will just be number, not a useful string representation 3.others…
What is it? Enumeration In simplest form: set of declared constants represented by identifiers Actually a special kind of class introduced with keyword enum enum Status {CONTINUE, WON, LOST}; enum Problem {ADDITION, SUBTRACTION, MULTIPLICATION, DIVISION}; enum Class {FRESHMAN, SOPHOMORE, JUNIOR, SENIOR};
StudentType /** * StudentType enum provides a type * where the valid range of values are only * those listed. */ public enum StudentType { FRESHMAN, SOPHOMORE, JUNIOR, SENIOR, GRADUATE; }
Student public class Student { //Student's name private String name; //This variable's type is StudentType. //So it can only hold a value from //the list declared in the StudentType Enum. private StudentType classification; public void setName(String s) { name = s; } public String getName() { return name; } public void setClassification (StudentType t) { classification = t; } public StudentType getClassification() { return classification; }
StudentTypeTest public class StudentTypeTest { public static void main (String[] args) { Student s1 = new Student(); Student s2 = new Student(); s1.setName("Sam"); s1.setClassification(StudentType.FRESHMAN); s2.setName("Bob"); s2.setClassification(StudentType.GRADUATE); print(s1); print(s2); }
StudentTypeTest public static void print(Student s) { System.out.println("Hi, my name is " + s.getName()); //call the accessor of the Student object to get the StudentType //swicth on this value returned switch(s.getClassification()) { //Case labels must be from the datatype that //we are switching on case FRESHMAN: System.out.println(" I'm just starting."); break; case SOPHOMORE: System.out.println(" 1 Down, 2 to go!"); break; case JUNIOR: System.out.println(" Yay, I\"m and upperclassperson"); break; case SENIOR: System.out.println(" I can see the light...Whew"); break; case GRADUATE: System.out.println(" I've been here too long..."); }
Treating as a class Because an enum is a type, it can have data and behavior. Each enumerated value can have data associated with it. public enum Planet { MERCURY (3.303e+23, e6), VENUS (4.869e+24, e6), EARTH (5.976e+24, e6), MARS (6.421e+23, e6), JUPITER (1.9e+27, e7), SATURN (5.688e+26, e7), URANUS (8.686e+25, e7), NEPTUNE (1.024e+26, e7), PLUTO (1.27e+22, 1.137e6); private final double mass; // in kilograms private final double radius; // in meters Planet(double mass, double radius) { this.mass = mass; this.radius = radius; } public double mass() { return mass; } public double radius() { return radius; } // universal gravitational constant (m3 kg-1 s-2) public static final double G = E-11; public double surfaceGravity() { return G * mass / (radius * radius); } public double surfaceWeight(double otherMass) { return otherMass * surfaceGravity(); }