Lecture 6 - September 11, 2008

 

Quiz  based on PlayWithText.java and PlayWithTextB.java   

Refer back to Talk about PlayWithText.java and PlayWithTextB.java 

 

Review Lab 5 - Lab5 Key

Different versions of Remember.java   Remember2.java  Remember3.java  Remember4.java

 

Review Lab 6  -  Lab6 Key

 

Look at examples of "for each" loops in:    TestClass.java and   Planet.java

 

Homework:  work on programs which are due on Tuesday….

Be sure you fully understand  toString  and compareTo methods and how to use them.

 

http://java.sun.com/docs/books/tutorial/java/javaOO/enum.html

Java programming language enum types are much more powerful than their counterparts in other languages. The enum declaration defines a class (called an enum type). The enum class body can include methods and other fields. The compiler automatically adds some special methods when it creates an enum. For example, they have a static values method that returns an array containing all of the values of the enum in the order they are declared. This method is commonly used in combination with the for-each construct to iterate over the values of an enum type. For example, this code from the Planet class example below iterates over all the planets in the solar system.

for (Planet p : Planet.values()) {
    System.out.printf("Your weight on %s is %f%n", 
                          p, p.surfaceWeight(mass));
}

 

 


Note: All enums implicitly extend java.lang.Enum. Since Java does not support multiple inheritance, an enum cannot extend anything else.


In the following example, Planet is an enum type that represents the planets in the solar system. They are defined with constant mass and radius properties.

Each enum constant is declared with values for the mass and radius parameters. These values are passed to the constructor when the constant is created. Java requires that the constants be defined first, prior to any fields or methods. Also, when there are fields and methods, the list of enum constants must end with a semicolon.


Note: The constructor for an enum type must be package-private or private access. It automatically creates the constants that are defined at the beginning of the enum body. You cannot invoke an enum constructor yourself.


In addition to its properties and constructor, Planet has methods that allow you to retrieve the surface gravity and weight of an object on each planet. Here is a sample program that takes your weight on earth (in any unit) and calculates and prints your weight on all of the planets (in the same unit):

 
 
I did a search on java.lang.Enum   found  http://kickjava.com/3725.htm  at the following:
 

Java > Java SE, EE, ME > java > lang > Enum _ Java API By Example ...

Java > Java SE, EE, ME > java > lang > Enum. ... java.lang.Enum<E>. All Implemented Interfaces: Serializable, Comparable<E>. See Also: Top Examples ...
kickjava.com/3725.htm - 22k - Cached - Similar pages