Enumeration • Skandal att detta inte fanns i Java 1.0 • Uppräkningsbar typ Enumeration • Man räknar upp alla värden som typen ska ha • Tidigare innan detta dök upp i java användes heltalskonstanter ofta för uppgifter där man nu använder enums Jan Erik Moström Johan Eliasson Innan enum public class Navigate { final public static final public static final public static final public static Med enum int int int int public class Navigate { public enum Direction { NORTH, SOUTH, EAST, WEST }; NORTH = 1; SOUTH = 2; EAST = 3; WEST = 4; void go( Direction dir ) { switch( dir ){ case NORTH: // ... break; case SOUTH: // ... break; case WEST: // ... break; case EAST: // ... break; } } void go( int dir ) { switch( dir ){ case NORTH: // ... break; case SOUTH: // ... break; case WEST: // ... break; case EAST: // ... break; default: // ... } } } } Johan Eliasson Johan Eliasson package cards; public class Card { public enum Rank { DEUCE, THREE, FOUR, FIVE, SIX, SEVEN, EIGHT, NINE, TEN, JACK, QUEEN, KING, ACE } public enum Suit { CLUBS, DIAMONS, HEARTS, SPADES } Kortleksexempel import java.util.Scanner; import cards.Deck; import cards.Card; public class CardDemo { public static void main( String[] args ) { Scanner in=new Scanner(System.in); int numHands = in.nextInt(); int handSize = in.nextInt(); Deck d = new Deck(); for( int i = 0; i < numHands; i++ ){ System.out.println( d.getHand( handSize) ); } } } private final Rank rankValue; private final Suit suitValue; Card( Rank rankValue, Suit suitValue ) { this.rankValue = rankValue; this.suitValue = suitValue; } public Rank rank( ) { return rankValue; } public Suit suit( ) { return suitValue; } 4 5 [QUEEN of CLUBS, SIX of DIAMONS, JACK of HEARTS, FOUR of SPADES, FIVE of HEARTS] [EIGHT of DIAMONS, JACK of DIAMONS, FIVE of CLUBS, NINE of SPADES, THREE of CLUBS] [ACE of HEARTS, SEVEN of HEARTS, TEN of HEARTS, THREE of HEARTS, TEN of DIAMONS] [KING of SPADES, NINE of DIAMONS, FOUR of CLUBS, SEVEN of DIAMONS, DEUCE of DIAMONS] Johan Eliasson public String toString( ) { return rankValue + " of " + suitValue; } } Johan Eliasson package cards; import java.util.List; import java.util.ArrayList; package cards; import java.util.ArrayList; import java.util.Collections; import java.util.List; public class Deck { private List<Card> myDeck; public class Hand { private List<Card> hand; public Hand( List<Card> content ) { hand = content; } public Deck( ) { myDeck = new ArrayList<Card>( ); for( Card.Suit suit : Card.Suit.values( ) ){ for( Card.Rank rank : Card.Rank.values( ) ){ myDeck.add( new Card( rank, suit ) ); } } Collections.shuffle( myDeck ); } public String toString( ) { return hand.toString(); } public Hand getHand( int nrOfCards ) { List<Card> handCards = myDeck.subList( 0, nrOfCards ); Hand hand = new Hand( new ArrayList<Card>(handCards) ); handCards.clear(); return hand; } } } Johan Eliasson public enum { MERCURY VENUS EARTH MARS JUPITER SATURN URANUS NEPTUNE PLUTO Inte en klass utan en enum!!! Planet (3.303e+23, (4.869e+24, (5.976e+24, (6.421e+23, (1.9e+27, (5.688e+26, (8.686e+25, (1.024e+26, (1.27e+22, Johan Eliasson 2.4397e6), 6.0518e6), 6.37814e6), 3.3972e6), 7.1492e7), 6.0268e7), 2.5559e7), 2.4746e7), 1.137e6); public class WeightDemo { public static void main(String[] args) { Scanner in=new Scanner(System.in); System.out.println(“Enter your earth weight”); double earthWeight = in.nextDouble(); double mass = earthWeight/Planet.EARTH.surfaceGravity(); for( Planet p : Planet.values() ) System.out.println("Your weight on ” + p + “ is " + p.surfaceWeight(mass)); } 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 = 6.67300E-11; public double surfaceGravity() { return G * mass / (radius * radius); } public double surfaceWeight(double otherMass) { return otherMass * surfaceGravity(); } } Johan Eliasson Johan Eliasson