Java Reference
In-Depth Information
LISTING 7.12
continued
for (Season time : Season.values())
System.out.println (time + "\t" + time.getSpan());
}
}
OUTPUT
winter December through February
spring March through May
summer June through August
fall September through November
KEY CONCEPT
We can add attributes and methods to
the definition of an enumerated type.
In addition to the list of possible values defined in every enumerated
type, we can include any number of attributes or methods of our own
choosing. This provides various opportunities for creative class design.
SELF-REVIEW QUESTION (see answer in Appendix N)
SR 7.17 Using the enumerated type Season as defined in this section, what is
the output from the following code sequence?
Season time1, time2;
time1 = Season.winter;
time2 = Season.summer;
System.out.println (time1);
System.out.println (time2.name());
System.out.println (time1.ordinal());
System.out.println (time2.getSpan());
7.7 Method Design
Once you have identified classes and assigned basic responsibilities, the design of
each method will determine how exactly the class will define its behaviors. Some
methods are straightforward and require little thought. Others are more interest-
ing and require careful planning.
An algorithm is a step-by-step process for solving a problem. A recipe is an
example of an algorithm. Travel directions are another example of an algorithm.
Every method implements an algorithm that determines how that method accom-
plishes its goals.
 
Search WWH ::




Custom Search