Docsity

Java Enumerations: Understanding and Using Java's Built-in Enum Class, Study notes of Computer Science

Toggle sidebar
bg1
U
UNIVERSITY OF
NIVERSITY OF M
MASSACHUSETTS
ASSACHUSETTS, A
, AMHERST
MHERST •
•Department of Computer Science
Department of Computer Science
Java Enumeration Types
CMPSCI 220 (291A)
Programming Methodology
Spring 2009
U
UNIVERSITY OF
NIVERSITY OF M
MASSACHUSETTS
ASSACHUSETTS, A
, AMHERST
MHERST •
•Department of Computer Science
Department of Computer Science 2
Plan
Tutorial on Java Enums
Mostly follows the one on the web
Quick introduction the Effective Java items
Should read that chapter! It has many examples
and details we won’t talk about here
After this presentation:
You should be able to understand and program
with Java enumerations
U
UNIVERSITY OF
NIVERSITY OF M
MASSACHUSETTS
ASSACHUSETTS, A
, AMHERST
MHERST •
•Department of Computer Science
Department of Computer Science 3
Motivation: A Little Example
public class Suit {
public final static int SPADES = 3;
public final static int HEARTS = 2;
public final static int DIAMONDS = 1;
public final static int CLUBS = 0;
private Suit() {} // no instances!
}
U
UNIVERSITY OF
NIVERSITY OF M
MASSACHUSETTS
ASSACHUSETTS, A
, AMHERST
MHERST •
•Department of Computer Science
Department of Computer Science 4
Some things not great about this:
No way to declare a variable of type Suit
No way to enforce a legal value
No way to prevent duplicate values
No fun converting to/from a name
(“Hearts”)
Note: C/C++ enums share some of these
defects!
U
UNIVERSITY OF
NIVERSITY OF M
MASSACHUSETTS
ASSACHUSETTS, A
, AMHERST
MHERST •
•Department of Computer Science
Department of Computer Science 5
Java 1.5 added enum classes:
public enum Suit {
SPADES, HEARTS,
DIAMONDS, CLUBS
}
Defines a subclass of java.lang.Enum
Defines four anonymous subclasses of Suit
SPADES, etc., is a singleton instance of its
anonymous subclass
U
UNIVERSITY OF
NIVERSITY OF M
MASSACHUSETTS
ASSACHUSETTS, A
, AMHERST
MHERST •
•Department of Computer Science
Department of Computer Science 6
Things you can do:
import static Suit.*;
Suit s = Enum.valueof(Suit.class,”CLUBS”);
switch (s) {
case SPADES:
case HEARTS:
System.out.println(“major”);
break;
case DIAMONDS:
case CLUBS:
System.out.println(“minor”);
break;
}
pf3
pf4

Partial preview of the text

Download Java Enumerations: Understanding and Using Java's Built-in Enum Class and more Study notes Computer Science in PDF only on Docsity!

UU NIVERSITY OFNIVERSITY OF MM ASSACHUSETTSASSACHUSETTS , A, A MHERSTMHERST •• Department of Computer ScienceDepartment of Computer Science

Java Enumeration Types

CMPSCI 220 (291A)

Programming Methodology

Spring 2009

U U NIVERSITY OFNIVERSITY OF MM ASSACHUSETTSASSACHUSETTS , A, A MHERST •MHERST• Department of Computer ScienceDepartment of Computer Science^2

Plan

„ Tutorial on Java Enums

„ Mostly follows the one on the web

„ Quick introduction the Effective Java items

„ Should read that chapter! It has many examples

and details we won’t talk about here

„ After this presentation:

„ You should be able to understand and program

with Java enumerations

UU NIVERSITY OFNIVERSITY OF MM ASSACHUSETTSASSACHUSETTS , A, A MHERSTMHERST •• Department of Computer ScienceDepartment of Computer Science^3

Motivation: A Little Example

public class Suit {

public final static int SPADES = 3;

public final static int HEARTS = 2;

public final static int DIAMONDS = 1;

public final static int CLUBS = 0;

private Suit() {} // no instances!

U U NIVERSITY OFNIVERSITY OF MM ASSACHUSETTSASSACHUSETTS , A, A MHERST •MHERST• Department of Computer ScienceDepartment of Computer Science^4

Some things not great about this:

„ No way to declare a variable of type Suit

„ No way to enforce a legal value

„ No way to prevent duplicate values

„ No fun converting to/from a name

(“Hearts”)

„ Note: C/C++ enums share some of these

defects!

UU NIVERSITY OFNIVERSITY OF MM ASSACHUSETTSASSACHUSETTS , A, A MHERSTMHERST •• Department of Computer ScienceDepartment of Computer Science^5

Java 1.5 added enum classes:

public enum Suit {

SPADES, HEARTS,

DIAMONDS, CLUBS

„ Defines a subclass of java.lang.Enum

„ Defines four anonymous subclasses of Suit

„ SPADES, etc., is a singleton instance of its

anonymous subclass

U U NIVERSITY OFNIVERSITY OF MM ASSACHUSETTSASSACHUSETTS , A, A MHERST •MHERST• Department of Computer ScienceDepartment of Computer Science^6

Things you can do:

import static Suit.*;

Suit s = Enum.valueof(Suit.class,”CLUBS”);

switch (s) {

case SPADES:

case HEARTS:

System.out.println(“major”);

break;

case DIAMONDS:

case CLUBS:

System.out.println(“minor”);

break;

UU NIVERSITY OFNIVERSITY OF MM ASSACHUSETTSASSACHUSETTS , A, A MHERSTMHERST •• Department of Computer ScienceDepartment of Computer Science^7

More things you can do:

Suit.values()

// array of Suit values in order

s.ordinal()

// an int: 0, 1, 2, 3 as in C

s.toString(), s.name()

// the name as declared

s1.compareTo(s2)

// compare using declared order

s1 == s

// true only if the same value

U U NIVERSITY OFNIVERSITY OF MM ASSACHUSETTSASSACHUSETTS , A, A MHERST •MHERST• Department of Computer ScienceDepartment of Computer Science^8

Maybe more surprising:

public enum Planet { MERCURY (3.303e+23, 2.4397e6), VENUS (4.869e+24, 6.0518e6), EARTH (5.976e+24, 6.37814e6), … ; // note semicolon here private final double mass; // in kgs 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; } … }

UU NIVERSITY OFNIVERSITY OF MM ASSACHUSETTSASSACHUSETTS , A, A MHERSTMHERST •• Department of Computer ScienceDepartment of Computer Science^9

The way they are built:

public enum Planet {

MERCURY (3.303e+23, 2.4397e6),

VENUS (4.869e+24, 6.0518e6),

EARTH (5.976e+24, 6.37814e6),

// constructor args must be given

// here, for building singletons

U U NIVERSITY OFNIVERSITY OF MM ASSACHUSETTSASSACHUSETTS , A, A MHERST •MHERST• Department of Computer ScienceDepartment of Computer Science^10

Something else to note:

public enum Planet {

// no visibility attribute on

// constructor: always private

Planet(double mass, double radius)

this.mass = mass;

this.radius = radius;

UU NIVERSITY OFNIVERSITY OF MM ASSACHUSETTSASSACHUSETTS , A, A MHERSTMHERST •• Department of Computer ScienceDepartment of Computer Science^11

Computing weight on other planets

// 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();

U U NIVERSITY OFNIVERSITY OF MM ASSACHUSETTSASSACHUSETTS , A, A MHERST •MHERST• Department of Computer ScienceDepartment of Computer Science^12

Printing a table of them

public static void main (String[] args) {

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));

UU NIVERSITY OFNIVERSITY OF MM ASSACHUSETTSASSACHUSETTS , A, A MHERSTMHERST •• Department of Computer ScienceDepartment of Computer Science^19

EJ Item 33: Use EnumMap vs

indexing by ordinals

public enum Phase { SOLID, LIQUID, GAS;

public enum Trans {

MELT, FREEZE, BOIL,

CONDENSE, SUBLIME, DEPOSIT;

private static final Trans[][] Ts = {

{ null , MELT , SUBLIME },

{ FREEZE , null , BOIL },

{ DEPOSIT, CONDENSE, null }

public static Trans from(Phase p1,Phase p2) {

return Ts[p1.ordinal()][p2.ordinal()];

U U NIVERSITY OFNIVERSITY OF MM ASSACHUSETTSASSACHUSETTS , A, A MHERST •MHERST• Department of Computer ScienceDepartment of Computer Science^20

Better approach

public enum Phase { SOLID, LIQUID, GAS; public enum Trans { MELT(SOLID,LIQUID), FREEZE(LIQUID,SOLID), BOIL(LIQUID,GAS), CONDENSE(GAS,LIQUID), SUBLIME(SOLID,GAS), DEPOSIT(GAS,SOLID); private final Phase p1; private final Phase p2; Trans(Phase p1, Phase p2) { this.p1 = p1; this.p2 = p2; } private static final Map<Phase, Map<Phase,Trans>> m = new EnumMap<Phase, Map<Phase,Trans>>(Phase.class); … } }

UU NIVERSITY OFNIVERSITY OF MM ASSACHUSETTSASSACHUSETTS , A, A MHERSTMHERST •• Department of Computer ScienceDepartment of Computer Science^21

Better approach concluded

static {

for (Phase p : Phase.values()) {

m.put(p,

new EnumMap<Phase,Trans>(Phase.class)

for (Trans t : Trans.values()) {

m.get(t.p1).put(t.p2, t);

public static Trans from(Phase p1, Phase p2) {

return m.get(p1).get(p2);

U U NIVERSITY OFNIVERSITY OF MM ASSACHUSETTSASSACHUSETTS , A, A MHERST •MHERST• Department of Computer ScienceDepartment of Computer Science^22

Item 34: Emulate extensible enums

with interfaces

Read the item ☺ …