--Your friends at LectureNotes

Note for Advanced Java Programming - AJP By vtu rangers

  • Advanced Java Programming - AJP
  • Note
  • Visvesvaraya Technological University Regional Center - VTU
  • 10 Topics
  • 793 Offline Downloads
  • Uploaded 1 year ago
0 User(s)
Download PDFOrder Printed Copy

Share it with your friends

Leave your Comments

Text from page-2

Advance Java and J2EE –Module 1 break; case RedDel: System.out.println("Red Delicious is red."); break; case Winesap: System.out.println("Winesap is red."); break; case Cortland: System.out.println("Cortland is red."); break; } } } The values( ) and valueOf( ) Methods All enumerations automatically contain two predefined methods: values( ) and valueOf( ). Their general forms are shown here: public static enum-type[ ] values( ) public static enum-type valueOf(String str) The values( ) method returns an array that contains a list of the enumeration constants. The valueOf( ) method returns the enumeration constant whose value corresponds to the string passed in str. Example Code-2: enum Season { WINTER, SPRING, SUMMER, FALL } ; class EnumExample1{ public static void main(String[] args) { for (Season s : Season.values()) System.out.println(s); Season s = Season.valueOf("WINTER"); System.out.println("S contains " + s); } }

Text from page-3

Advance Java and J2EE –Module 1 Example Code-3 class EnumExample5{ enum Day{ SUNDAY, MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDA Y, SATURDAY} public static void main(String args[]){ Day day=Day.MONDAY; switch(day){ case SUNDAY: System.out.println("sunday"); break; case MONDAY: System.out.println("monday"); break; default: System.out.println("other day"); } }} Class Type Enumeration enum Apple { Jonathan(10), GoldenDel(9), RedDel(12), Winesap(15), Cortland(8); private int price; Apple(int p) { price = p; } int getPrice() { return price; } }

Text from page-4

Advance Java and J2EE –Module 1 class EnumDemo3 { public static void main(String args[]) { Apple ap; System.out.println("Winesap costs " + Apple.Winesap.getPrice() + " cents.\n"); System.out.println("All apple prices:"); for(Apple a : Apple.values()) System.out.println(a + " costs " + a.getPrice() + " cents."); } } The Class type enumeration contains three things The first is the instance variable price, which is used to hold the price of each variety of apple. The second is the Apple constructor, which is passed the price of an apple. The third is the method getPrice( ), which returns the value of price. When the variable ap is declared in main( ), the constructor for Apple is called once for each constant that is specified. the arguments to the constructor are specified, by putting them inside parentheses after each constant, as shown here: Jonathan(10), GoldenDel(9), RedDel(12), Winesap(15), Cortland(8); These values are passed to the parameter of Apple(),which then assigns this value to price. The constructor is called once for each constant. Because each enumeration constant has its own copy of price, you can obtain the price of a specified type of apple by calling getPrice(). For example, in main() the price of a Winesap is obtained by the following call:Apple.Winesap.getPrice()

Text from page-5

Advance Java and J2EE –Module 1 Enum Super class All enumerations automatically inherit one: java.lang.Enum. Enum class defines several methods that are available for use by all enumerations. ordinal( ) To obtain a value that indicates an enumeration constant’s position in the list of constants. This is called its ordinal value, and it is retrieved by calling the ordinal( ) method, shown here: final int ordinal( ) It returns the ordinal value of the invoking constant. Ordinal values begin at zero. Thus, in the Apple enumeration, Jonathan has an ordinal value of zero, GoldenDel has an ordinal value of 1, RedDel has an ordinal value of 2, and so on. compareTo( ) To compare the ordinal value of two constants of the same enumeration by using the compareTo( ) method. It has this general form: final int compareTo(enum-type e) equals() equals method is overridden method from Object class, it is used to compare the enumeration constant. Which returns true if both constants are same. Program to demonstrate the use of ordinal(), compareTo(), equals() enum Apple { Jonathan, GoldenDel, RedDel, Winesap, Cortland } class EnumDemo4 { public static void main(String args[]) { Apple ap, ap2, ap3; System.out.println("Here are all apple constants" + " and their ordinal values: "); for(Apple a : Apple.values()) System.out.println(a + " " + a.ordinal());

Lecture Notes