Compiled By: Umm-e-Laila
Lecture # 4
1
Enumeration
Course Books
 Text Book:
 Herbert Schildt, Java: The Complete Reference, McGraw-Hill
Education, Eleventh Edition
 Craig Larman, Applying UML & patterns, 2 edition
 Reference Books:
 Cay S. Horstmann, Big Java: Early Objects, Wiley, 7th Edition
 Herbert Schildt, Java: A Beginner's Guide, McGraw-Hill Education,
Eighth Edition
2
Course Instructors
 Umm-e-Laila ulaila@ssuet.edu.pk
Assistant Professor, CED
Room Number: BS-04
Tel: 111-994-994, Ext. 536
 Aneeta Siddiqui aarshad@ssuet.edu.pk
Assistant Professor, CED
Room Number: BS-03
Tel: 111-994-994,
Course Website
 http://sites.google.com/site/ulaila206
4
Enumeration
6
Introduction
 Enumeration means a list of named constant
 Sometimes you want a variable that can take on
only a certain listed (enumerated) set of values
 Examples:
 dayOfWeek: SUNDAY, MONDAY, TUESDAY, …
 month: JAN, FEB, MAR, APR, …
 gender: MALE, FEMALE
 Enum in java is a data type that contains fixed set of
constants.
 It can be thought of as a class having fixed set of
constants.
Introduction cont
 The java enum constants are static and final implicitly. It
is available from JDK 1.5.
 It can be used to declare days of the week, Months in a
Year etc.
 An Enumeration can have constructors, methods and
instance variables. It is created using enum keyword.
 Each enumeration constant is public, static and final by
default.
 Even though enumeration defines a class type and have
constructors, you do not instantiate an enum using new.
 Enumeration variables are used and declared in much a
same way as you do a primitive variable.
Advantages of Enum
 Enums provide compile-time type safety
 Enums can be easily used in switch
 Enums can be traversed
 Enums can have fields, constructors and methods
 Enums may implement many interfaces but cannot
extend any class because it internally extends Enum
class
 Enums provide compile-time type safety
 Enums provide a proper name space for the enumerated
type
Advantages of the Enum cont
 Enums are robust
 If you add, remove, or reorder constants, you must recompile, and
then everything is OK again
 Because enums are objects, you can put them in
collections
Enumerated types
 enum: A type of objects with a fixed set of constant
values.
public enum Name {
VALUE, VALUE, ..., VALUE
}
 Usually placed into its own .java file.
 C has enums that are really ints; Java's are objects.
enum Days {
MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY,
SATURDAY, SUNDAY
}
 Note:Use enums instead of int constants. "The
advantages of enum types over int constants are compelling.
Enums are far more readable, safer, and more powerful."
Example 1
public enum Days {
MONDAY, TUESDAY, WEDNESDAY, THURSDAY,
FRIDAY, SATURDAY, SUNDAY
}
// create another file for test class
Class TestDays
public static void main(String[] args) {
Days today = Days.Wednesday;
Days holiday = Days.Sunday;
System.out.println("Today = " + today);
System.out.println(holiday+ " is holiday");
}}
Output: Today = WEDNESDAY
SUNDAY is holiday
Enum methods
method description
int compareTo(E) all enum types are Comparable by order of
declaration
boolean equals(o) not needed; can just use ==
String name() equivalent to toString
int ordinal() returns an enum's 0-based number by order
of declaration (first is 0, then 1, then 2, ...)
method Description
static E valueOf(s) converts a string into an enum value
static E[] values() an array of all values of your enumeration
Traversing Enum
public class WeekDaysList {
public static void main(String[] args) {
System.out.println("Days of week:");
for (Days day : Days.values()) {
System.out.printf("%s ", day);
}
System.out.println();
}}
Output: Days of week:
MONDAY TUESDAY WEDNESDAY THURSDAY FRIDAY SATURDAY SUNDAY
 The values method returns an array that contains a
list of enumeration constants. When we print the
element day, its toString method is called by default.
Example 2:
 Override toString method to prints each
constant in lower case.
public enum Days {
MONDAY,
TUESDAY,WEDNESDAY,THURSDAY,FRIDAY,SATURDAY,SUNDA
Y;
public String toString() {
// All constant into small case
String s = super.toString();
return s.toLowerCase();
}}
Output: Days of week:
monday tuesday wednesday thursday friday saturday sunday
Ordinal() value of enum constant
 Iterate/Traversed Days enum and print out the
enum value and its corresponding integer value.
public class WeekDaysList {
public static void main(String[] args)
System.out.println("Days of week:'");
for (Days day : Days.values()) {
System.out.printf("%s int value=%d", day,
day.ordinal());
System.out.println();
}}}
Output:
monday int value=0 tuesday int value=1 wednesday int
value=2 thursday int value=3 friday int value=4 saturday
int value=5 sunday int value=6
ValueOf()
 static enum-type valueOf(String s) returns the
enumerated object whose name is s
public class WeekDaysList {
public static void main(String[] args)
System.out.println("Days of week:'");
for (Days day : Days.values()) {
System.out.println(""+day);
Days c = day.valueOf("SUNDAY");
System.out.println("I Like " + c);
}}}
Output:
Monday I Like sunday
Tuesday I Like Sunday
……….
compareTo() Enum Object
 int compareTo(E o) compares this enum with the specified object for
order; returns a negative integer, zero, or a positive integer as this
object is less than, equal to, or greater than the specified object
public class TestCompare {
public static void main(String[] args) Days
d1,d2;
d1=Days.FRIDAY;
d2=Days.SUNDAY;
//Comparing using compareTo() method
if(d1.compareTo(d2) > 0) {
System.out.println(d2 + " comes after " +d1);}
if(d1.compareTo(d2) < 0) {
System.out.println(d1 + " comes before " + d2);}
if(d1.compareTo(d2) == 0) {
System.out.println(d1 + " equals" +d2);
}}}}
Output:
friday comes before sunday
Enum Constructor
 Enum constructorsare implicitly private.
 In case there is no access modifier defined
for an enum constructor the compiler
implicitly assumes it to be private.
 Also, note that defining an enum
constructor with public or protected access
would result in a compiler error
Example 3: JacketSzie Enum
public enum JacketSize {
small(36), medium(40), large(42),
extra_large(46), extra_extra_large(48);
// Constructor
JacketSize(int chestSize){
this.chestSize = chestSize;}
public int chestSize() {
return chestSize;
}
private int chestSize;
}
.
Example 3: JacketSzie Enum cont
public class TestJacket {
public static void main(String[] args) {
for(JacketSize j:JacketSize.values()){
System.out.println("JacketSize type: "+j+"
Jacket Size: "+j.chestSize());
} }}
Output:
JacketSize type: small Jacket Size: 36
JacketSize type: medium Jacket Size: 40
JacketSize type: large Jacket Size: 42
JacketSize type: extra_large Jacket Size: 46
JacketSize type: extra_extra_large Jacket Size: 48
Enums and the switch statement
 switch statements can now work with enums
 The switch variable evaluates to some enum value
 The values for each case must (as always) be
constants
 switch (variable) { case constant: …; }
 In the switch constants, do not give the class name—
that is, you must say case SUNDAY:, not case
Days.SUNDAY
 It’s still a very good idea to include a default
case
Example 4: enum with switch
public class TestJacket {
public static void main(String[] args) {
for(JacketSize j:JacketSize.values()){
switch (j) {
case small: System.out.println("S");
break;
case medium: System.out.println("M");
break;
case large: System.out.println("L");
break;
case extra_large: System.out.println("XL");
break;
case extra_extra_large: System.out.println("XXL");
break; } }
Output:
S M L XL XXL
Example 5: More complex enums
 An enumerated type can have fields, methods, and
constructors:
public enum Coin {
PENNY(1), NICKEL(5), DIME(10), QUARTER(25);
private int cents;
private Coin(int cents) {
this.cents = cents;
}
public int getCents() { return cents; }
public int perDollar() { return 100 / cents; }
public String toString() { // "NICKEL (5c)"
return super.toString() + " (" + cents + "c)";
}
}
Example 6: Card Game
public enum Suit {
CLUBS, DIAMONDS, HEARTS, SPADES }
public enum rank {
Two,Three,Four,Five,Six,Seven,Eight,Nine,Ten, Jack,
Queen, King, Ace }
public class Cards {
// String[] suit = { "Clubs", "Diamonds", "Hearts",
"Spades" };
// String[] rank = { "2", "3", "4", "5", "6", "7", "8",
"9", "10", "Jack", "Queen", "King", "Ace" };
// String R;// String S;
suit S;
rank R;
public void SetRank(rank r){
R=r;
}
public void SetSuit(suit s){
S=s;
}
Card game cont
public void Print(){
System.out.print(S +" of "+R+"t ");}
public String toString()
{ return R + " of " + S; }
}
public boolean Compare(Cards d)
{ if(this.R.equals(d.R)) return true;
else return false; }
public boolean isGreater(Cards d)
{ if(this.R.compareTo(d.R)>0) return true;
else return false;
}
Card game cont
public class Deck {
public static final int NCARDS = 52;
Cards card[]=new Cards[NCARDS];
int currentCard=0;
public void generate(){
int i = 0;
for( rank r:rank.values()){
for(suit s:suit.values()){
card[i]=new Cards();
card[i].SetRank(r);
card[i].SetSuit(s);
i++;
}
}
}
Card game cont
public void Print(){
int end=1;
System.out.println("nn***************** The
DECK************************************");
for ( int i = 0; i < card.length; i++ )
{card[i].Print();
if(end==3) {
System.out.println("n");
end=0;
} end++; }
public Cards Pick()
{if ( currentCard < NCARDS ) {
return ( card[ currentCard++ ] );
}
else
{ System.out.println("Out of cards error");
return ( null ); // Error;
} }
Card game cont
public Cards PickRandom()
{
int random=(int)(Math.random()*NCARDS);
return ( card[ currentCard++ ] );
}
public void Shuffle(int n){
for (int i = 0; i < n; i++) {
int r = (int) (Math.random() * (NCARDS));
int s = (int) (Math.random() * (NCARDS));
Cards t = card[r];
card[r] = card[s];
card[s] = t; } } }
Card game cont
public class CardGame {
public static void main(String[] args) {
Deck deck=new Deck();
deck.generate();
deck.Print();
deck.Shuffle(5);
deck.Print();
Cards c=deck.Pick();
Cards c1=deck.PickRandom();
System.out.println("nCard1 "+c);
System.out.println("nCard2 "+c1);
System.out.println("n"+ c.Compare(c1));
System.out.println("n"+ c.isGreater(c1));
}
}
Self Study: EnumSet
 class EnumSet from java.util represents a set of
enum values and has useful methods for manipulating
enums:
Set<Coin> coins = EnumSet.range(Coin.NICKEL, Coin.QUARTER);
for (coin c : coins) {
System.out.println(c); // see also: EnumMap
}
static EnumSet<E> allOf(Type) a set of all values of the type
static EnumSet<E> complementOf(set) a set of all enum values other
than the ones in the given set
static EnumSet<E> noneOf(Type) an empty set of the given type
static EnumSet<E> of(...) a set holding the given values
static EnumSet<E> range(from, to) set of all enum values declared
between from and to
Example 7: EnumSet
public class EnumsetExample {
public static void main(String[] args)
{EnumSet<Coin> set1, set2, set3, set4;
set1 = EnumSet.of(Coin.DIME,Coin.NICKEL);
set2 = EnumSet.complementOf(set1);
set3 = EnumSet.allOf(Coin.class);
set4 = EnumSet.range(Coin.PENNY, Coin.DIME);
System.out.println("Set 1: " + set1);
System.out.println("Set 2: " + set2);
System.out.println("Set 3: " + set3);
System.out.println("Set 4: " + set4);}}
Output:
Set 1: [NICKEL, DIME]
Set 2: [PENNY, QUARTER]
Set 3: [PENNY, NICKEL, DIME, QUARTER]
Set 4: [PENNY, NICKEL, DIME]
Tasks
1. Override toString of Example 2 method to prints each
constant last letter into small case.
2. Create enum for departments in an Organization – HUMAN
RESOURCES, MARKETING, IT, OPERATIONS are all
departments of an organization. Use the department enum
in employee class.
3. Traversed the department enum and prints the oridinal
values.
4. Enhanced enum with a new private instance variable
deptCode holding the department code, a getter method for
deptCode named getDeptCode(), and a constructor which
accepts the department code at the time of enum creation
Tasks
1. Override toString of Example 2 method to prints each
constant last letter into small case.
2. Create enum for departments in an Organization –
HUMAN RESOURCES, MARKETING, IT, OPERATIONS
are all departments of an organization. Use the department
enum in employee class.
3. Traversed the department enum and prints the oridinal
values.
4. Enhanced enum with a new private instance variable
deptCode holding the department code, a getter method
for deptCode named getDeptCode(), and a constructor
which accepts the department code at the time of enum
creation

Lecture 6 Enumeration in java ADVANCE.pptx

  • 1.
  • 2.
    Course Books  TextBook:  Herbert Schildt, Java: The Complete Reference, McGraw-Hill Education, Eleventh Edition  Craig Larman, Applying UML & patterns, 2 edition  Reference Books:  Cay S. Horstmann, Big Java: Early Objects, Wiley, 7th Edition  Herbert Schildt, Java: A Beginner's Guide, McGraw-Hill Education, Eighth Edition 2
  • 3.
    Course Instructors  Umm-e-Laila[email protected] Assistant Professor, CED Room Number: BS-04 Tel: 111-994-994, Ext. 536  Aneeta Siddiqui [email protected] Assistant Professor, CED Room Number: BS-03 Tel: 111-994-994,
  • 4.
  • 5.
  • 6.
    6 Introduction  Enumeration meansa list of named constant  Sometimes you want a variable that can take on only a certain listed (enumerated) set of values  Examples:  dayOfWeek: SUNDAY, MONDAY, TUESDAY, …  month: JAN, FEB, MAR, APR, …  gender: MALE, FEMALE  Enum in java is a data type that contains fixed set of constants.  It can be thought of as a class having fixed set of constants.
  • 7.
    Introduction cont  Thejava enum constants are static and final implicitly. It is available from JDK 1.5.  It can be used to declare days of the week, Months in a Year etc.  An Enumeration can have constructors, methods and instance variables. It is created using enum keyword.  Each enumeration constant is public, static and final by default.  Even though enumeration defines a class type and have constructors, you do not instantiate an enum using new.  Enumeration variables are used and declared in much a same way as you do a primitive variable.
  • 8.
    Advantages of Enum Enums provide compile-time type safety  Enums can be easily used in switch  Enums can be traversed  Enums can have fields, constructors and methods  Enums may implement many interfaces but cannot extend any class because it internally extends Enum class  Enums provide compile-time type safety  Enums provide a proper name space for the enumerated type
  • 9.
    Advantages of theEnum cont  Enums are robust  If you add, remove, or reorder constants, you must recompile, and then everything is OK again  Because enums are objects, you can put them in collections
  • 10.
    Enumerated types  enum:A type of objects with a fixed set of constant values. public enum Name { VALUE, VALUE, ..., VALUE }  Usually placed into its own .java file.  C has enums that are really ints; Java's are objects. enum Days { MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY, SATURDAY, SUNDAY }  Note:Use enums instead of int constants. "The advantages of enum types over int constants are compelling. Enums are far more readable, safer, and more powerful."
  • 11.
    Example 1 public enumDays { MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY, SATURDAY, SUNDAY } // create another file for test class Class TestDays public static void main(String[] args) { Days today = Days.Wednesday; Days holiday = Days.Sunday; System.out.println("Today = " + today); System.out.println(holiday+ " is holiday"); }} Output: Today = WEDNESDAY SUNDAY is holiday
  • 12.
    Enum methods method description intcompareTo(E) all enum types are Comparable by order of declaration boolean equals(o) not needed; can just use == String name() equivalent to toString int ordinal() returns an enum's 0-based number by order of declaration (first is 0, then 1, then 2, ...) method Description static E valueOf(s) converts a string into an enum value static E[] values() an array of all values of your enumeration
  • 13.
    Traversing Enum public classWeekDaysList { public static void main(String[] args) { System.out.println("Days of week:"); for (Days day : Days.values()) { System.out.printf("%s ", day); } System.out.println(); }} Output: Days of week: MONDAY TUESDAY WEDNESDAY THURSDAY FRIDAY SATURDAY SUNDAY  The values method returns an array that contains a list of enumeration constants. When we print the element day, its toString method is called by default.
  • 14.
    Example 2:  OverridetoString method to prints each constant in lower case. public enum Days { MONDAY, TUESDAY,WEDNESDAY,THURSDAY,FRIDAY,SATURDAY,SUNDA Y; public String toString() { // All constant into small case String s = super.toString(); return s.toLowerCase(); }} Output: Days of week: monday tuesday wednesday thursday friday saturday sunday
  • 15.
    Ordinal() value ofenum constant  Iterate/Traversed Days enum and print out the enum value and its corresponding integer value. public class WeekDaysList { public static void main(String[] args) System.out.println("Days of week:'"); for (Days day : Days.values()) { System.out.printf("%s int value=%d", day, day.ordinal()); System.out.println(); }}} Output: monday int value=0 tuesday int value=1 wednesday int value=2 thursday int value=3 friday int value=4 saturday int value=5 sunday int value=6
  • 16.
    ValueOf()  static enum-typevalueOf(String s) returns the enumerated object whose name is s public class WeekDaysList { public static void main(String[] args) System.out.println("Days of week:'"); for (Days day : Days.values()) { System.out.println(""+day); Days c = day.valueOf("SUNDAY"); System.out.println("I Like " + c); }}} Output: Monday I Like sunday Tuesday I Like Sunday ……….
  • 17.
    compareTo() Enum Object int compareTo(E o) compares this enum with the specified object for order; returns a negative integer, zero, or a positive integer as this object is less than, equal to, or greater than the specified object public class TestCompare { public static void main(String[] args) Days d1,d2; d1=Days.FRIDAY; d2=Days.SUNDAY; //Comparing using compareTo() method if(d1.compareTo(d2) > 0) { System.out.println(d2 + " comes after " +d1);} if(d1.compareTo(d2) < 0) { System.out.println(d1 + " comes before " + d2);} if(d1.compareTo(d2) == 0) { System.out.println(d1 + " equals" +d2); }}}} Output: friday comes before sunday
  • 18.
    Enum Constructor  Enumconstructorsare implicitly private.  In case there is no access modifier defined for an enum constructor the compiler implicitly assumes it to be private.  Also, note that defining an enum constructor with public or protected access would result in a compiler error
  • 19.
    Example 3: JacketSzieEnum public enum JacketSize { small(36), medium(40), large(42), extra_large(46), extra_extra_large(48); // Constructor JacketSize(int chestSize){ this.chestSize = chestSize;} public int chestSize() { return chestSize; } private int chestSize; } .
  • 20.
    Example 3: JacketSzieEnum cont public class TestJacket { public static void main(String[] args) { for(JacketSize j:JacketSize.values()){ System.out.println("JacketSize type: "+j+" Jacket Size: "+j.chestSize()); } }} Output: JacketSize type: small Jacket Size: 36 JacketSize type: medium Jacket Size: 40 JacketSize type: large Jacket Size: 42 JacketSize type: extra_large Jacket Size: 46 JacketSize type: extra_extra_large Jacket Size: 48
  • 21.
    Enums and theswitch statement  switch statements can now work with enums  The switch variable evaluates to some enum value  The values for each case must (as always) be constants  switch (variable) { case constant: …; }  In the switch constants, do not give the class name— that is, you must say case SUNDAY:, not case Days.SUNDAY  It’s still a very good idea to include a default case
  • 22.
    Example 4: enumwith switch public class TestJacket { public static void main(String[] args) { for(JacketSize j:JacketSize.values()){ switch (j) { case small: System.out.println("S"); break; case medium: System.out.println("M"); break; case large: System.out.println("L"); break; case extra_large: System.out.println("XL"); break; case extra_extra_large: System.out.println("XXL"); break; } } Output: S M L XL XXL
  • 23.
    Example 5: Morecomplex enums  An enumerated type can have fields, methods, and constructors: public enum Coin { PENNY(1), NICKEL(5), DIME(10), QUARTER(25); private int cents; private Coin(int cents) { this.cents = cents; } public int getCents() { return cents; } public int perDollar() { return 100 / cents; } public String toString() { // "NICKEL (5c)" return super.toString() + " (" + cents + "c)"; } }
  • 24.
    Example 6: CardGame public enum Suit { CLUBS, DIAMONDS, HEARTS, SPADES } public enum rank { Two,Three,Four,Five,Six,Seven,Eight,Nine,Ten, Jack, Queen, King, Ace } public class Cards { // String[] suit = { "Clubs", "Diamonds", "Hearts", "Spades" }; // String[] rank = { "2", "3", "4", "5", "6", "7", "8", "9", "10", "Jack", "Queen", "King", "Ace" }; // String R;// String S; suit S; rank R; public void SetRank(rank r){ R=r; } public void SetSuit(suit s){ S=s; }
  • 25.
    Card game cont publicvoid Print(){ System.out.print(S +" of "+R+"t ");} public String toString() { return R + " of " + S; } } public boolean Compare(Cards d) { if(this.R.equals(d.R)) return true; else return false; } public boolean isGreater(Cards d) { if(this.R.compareTo(d.R)>0) return true; else return false; }
  • 26.
    Card game cont publicclass Deck { public static final int NCARDS = 52; Cards card[]=new Cards[NCARDS]; int currentCard=0; public void generate(){ int i = 0; for( rank r:rank.values()){ for(suit s:suit.values()){ card[i]=new Cards(); card[i].SetRank(r); card[i].SetSuit(s); i++; } } }
  • 27.
    Card game cont publicvoid Print(){ int end=1; System.out.println("nn***************** The DECK************************************"); for ( int i = 0; i < card.length; i++ ) {card[i].Print(); if(end==3) { System.out.println("n"); end=0; } end++; } public Cards Pick() {if ( currentCard < NCARDS ) { return ( card[ currentCard++ ] ); } else { System.out.println("Out of cards error"); return ( null ); // Error; } }
  • 28.
    Card game cont publicCards PickRandom() { int random=(int)(Math.random()*NCARDS); return ( card[ currentCard++ ] ); } public void Shuffle(int n){ for (int i = 0; i < n; i++) { int r = (int) (Math.random() * (NCARDS)); int s = (int) (Math.random() * (NCARDS)); Cards t = card[r]; card[r] = card[s]; card[s] = t; } } }
  • 29.
    Card game cont publicclass CardGame { public static void main(String[] args) { Deck deck=new Deck(); deck.generate(); deck.Print(); deck.Shuffle(5); deck.Print(); Cards c=deck.Pick(); Cards c1=deck.PickRandom(); System.out.println("nCard1 "+c); System.out.println("nCard2 "+c1); System.out.println("n"+ c.Compare(c1)); System.out.println("n"+ c.isGreater(c1)); } }
  • 30.
    Self Study: EnumSet class EnumSet from java.util represents a set of enum values and has useful methods for manipulating enums: Set<Coin> coins = EnumSet.range(Coin.NICKEL, Coin.QUARTER); for (coin c : coins) { System.out.println(c); // see also: EnumMap } static EnumSet<E> allOf(Type) a set of all values of the type static EnumSet<E> complementOf(set) a set of all enum values other than the ones in the given set static EnumSet<E> noneOf(Type) an empty set of the given type static EnumSet<E> of(...) a set holding the given values static EnumSet<E> range(from, to) set of all enum values declared between from and to
  • 31.
    Example 7: EnumSet publicclass EnumsetExample { public static void main(String[] args) {EnumSet<Coin> set1, set2, set3, set4; set1 = EnumSet.of(Coin.DIME,Coin.NICKEL); set2 = EnumSet.complementOf(set1); set3 = EnumSet.allOf(Coin.class); set4 = EnumSet.range(Coin.PENNY, Coin.DIME); System.out.println("Set 1: " + set1); System.out.println("Set 2: " + set2); System.out.println("Set 3: " + set3); System.out.println("Set 4: " + set4);}} Output: Set 1: [NICKEL, DIME] Set 2: [PENNY, QUARTER] Set 3: [PENNY, NICKEL, DIME, QUARTER] Set 4: [PENNY, NICKEL, DIME]
  • 32.
    Tasks 1. Override toStringof Example 2 method to prints each constant last letter into small case. 2. Create enum for departments in an Organization – HUMAN RESOURCES, MARKETING, IT, OPERATIONS are all departments of an organization. Use the department enum in employee class. 3. Traversed the department enum and prints the oridinal values. 4. Enhanced enum with a new private instance variable deptCode holding the department code, a getter method for deptCode named getDeptCode(), and a constructor which accepts the department code at the time of enum creation
  • 33.
    Tasks 1. Override toStringof Example 2 method to prints each constant last letter into small case. 2. Create enum for departments in an Organization – HUMAN RESOURCES, MARKETING, IT, OPERATIONS are all departments of an organization. Use the department enum in employee class. 3. Traversed the department enum and prints the oridinal values. 4. Enhanced enum with a new private instance variable deptCode holding the department code, a getter method for deptCode named getDeptCode(), and a constructor which accepts the department code at the time of enum creation