Presentation is loading. Please wait.

Presentation is loading. Please wait.

Spring 2008 Mark Fontenot CSE 1341 - Honors Principles of Computer Science I Note Set 17.

Similar presentations

Presentation on theme: "Spring 2008 Mark Fontenot CSE 1341 - Honors Principles of Computer Science I Note Set 17."— Presentation transcript:

1 Spring 2008 Mark Fontenot CSE 1341 - Honors Principles of Computer Science I Note Set 17

2 Note Set 17 Overview Back To The … Enums

3 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…

4 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};

5 StudentType /** * StudentType enum provides a type * where the valid range of values are only * those listed. */ public enum StudentType { FRESHMAN, SOPHOMORE, JUNIOR, SENIOR, GRADUATE; }

6 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; }

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

8 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..."); }

9 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, 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), 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 = 6.67300E-11; public double surfaceGravity() { return G * mass / (radius * radius); } public double surfaceWeight(double otherMass) { return otherMass * surfaceGravity(); }

Download ppt "Spring 2008 Mark Fontenot CSE 1341 - Honors Principles of Computer Science I Note Set 17."

Similar presentations

Ads by Google