Computer Science Department |
Objectives:
|
The student will:
|
Background:
|
Enumerated types are included as part of java 5.0 and have been enhanced in java 6.0. This lab provides experience in thinking about the use of enum types and the practice of working with enumerated types in programs. |
New Terms: |
enum type "... a type whose fields consist of a fixed set of constants. Common examples include compass directions (values of NORTH, SOUTH, EAST, and WEST) and the days of the week." from java.sun.com |
Materials:
|
worksheet.doc |
Acknowledgment |
Adapted from a lab by Dr. David Bernstein, modified first by N. Harris and then by E. Adams |
As ususal, this lab is
to be submitted to Blackboard
by midnight tonight if you don’t finish it during the lab period.
Before going any further you should:
It is common practice to use a group of integer contants rather than an actual enumerated type. This part
of the lab will help you see some of the problems with this approach.
Constants
, SemesterUtiltities
, and Example1.java
in the editor. Constants
, SemesterUtiltities
, and Example1.java
. Example1
. FALL
to TALL
in Constants.java
. Constants.java
.
Example1
. Example1
executed
given that it uses Constants.FALL
which is no longer defined. Why did it execute?
(You may not know the answer to this question but you should be able to
make some conjectures.) Example1.java
. TALL
back to FALL
in Constants.java
. SUMMER
to Constants.java
and assign it
the value 2. Constants.java
and Example1.java
(remembering
that it is very important to compile Example1.java
even though it did not change). Example1
. SUMMER
and FALL
in the proper order (in Constants.java
)
and adjust their values accordingly. Constants.java
and Example1.java
. Example1
. main(
)
method in Example1.java
.
System.out.println(SemesterUtilities.startingMonth(7));
Example1.java
compile? 28. What output would be more informative for the previous statement?
It is also common practice to use a group of String
contants rather than an actual
enumerated type. This part of the lab will help you see some of the additional
problems with this approach.
Example2.java
in the editor. Example2.java
. String.compareTo
(
java.lang.String
)
can be used to compare two String
objects. Use this method to
assign second
to better
if second.compareTo
(
first)
is greater than 0 and to
assign first
to better
otherwise. main
(
)
method
inExample2.java
. System.out.println("Better grade: " + better);
Example2.java
. This part of the lab will help you see some of the
advantages of using enumerated types. You will explore some of the other
benefits later.
Example3.java
in the editor. Example3.java
. compareTo
(
)
method?
.class
files were created
when you compiledExample3.java
? (Hint: Look for all files that start with
"Example3" and end with ".class".) Example3.java
and into
a file named LetterGrade.java
.public enum LetterGrade
{
F, D, DPLUS, CMINUS, C, CPLUS, BMINUS, B, BPLUS, AMINUS, A;
}
7. Delete all of the.class
files in the directory you created for this lab.
8. CompileLetterGrade.java
andExample3.java
.
9. What.class
files were generated?
10. ExecuteExample3.java
.
11. What output was generated?
12. JMU has instituted a grade of "D-". What changes would you need to make toLetterGrade.java
?
This part of the lab will give you some experience creating
a simple enumerated type.
Sessions.java
that includes a Spring, Summer, and Fall semester
(in the appropriate order for a calendar year). SemesterUtilities.java
and Example1.java
so
that they work correctly with this enumerated type. (Hint: Think about
using a "for each" loop.) IF you don’t remember or haven’t seen
as “for each” loop, look it up. Updated 09/10/08 (esa)