CS
239 Lab: Enumerated Types
Objectives:
|
The student will:
- practice using
enumerated types
- create an enumerated
type with behavior (methods) as well as values
|
Background:
|
Enumerated types allow
the user to create a class of discrete constant objects. This lab will
explore the use of behaviors to accompany those values. |
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:
|
Example1.java
Example2.java
Example3.java
LetterGradeV2.java
worksheet.txt
|
Acknowledgment |
From a lab by David
Bernstein |
1 Instructions:
- If you have not already done
so, read chapter 9.9 in the Gaddis book and the Java Tutorial for Enum.
- Answer as many of the
following
questions as you can during the lab
period.
2 Getting Ready:
Before going any further you should:
- Make a directory for this
lab.
- Setup your development
environment.
- Download the files found in
Materials to your working directory. (In most browsers, the easiest way
to do this is by right-clicking on each of the links.)
3 Using a Simple Enumerated
Type:
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.
- Open
Example1.java
in the editor.
- Compile and execute
Example1.java
.
- What output was
generated?
- What determines the
order used by the
compareTo()
method?
- What
.class
files were created when you compiled Example1.java
? (Hint:
Look for all files that
start with "Example1" and end with ".class".)
- Copy the lines:
public enum LetterGrade
{
F, D, DPLUS, CMINUS, C, CPLUS, BMINUS, B, BPLUS, AMINUS, A;
}
from Example1.java
and into a new file named LetterGrade.java.
REMOVE the lines from Example1.java.
- Delete all of the
.class
files in the directory you created for this lab.
- Compile LetterGrade.java
and Example1.java.
- What.class files
were generated?
- Execute Example1.
- What output was
generated?
- JMU recently instituted a
grade of "D-". What
changes would you need to make to LetterGrade.java?
4 Using a Simple Enumerated
Type:
This part of the lab will help you see some of
the shortcomings of simple enumerated types.
- Open Example2.java
in the editor.
- Add a declaration of a double
variable named avg
in the main
method.
- Add a declaration of two LetterGrade
variables named cs139
and cs239.
- Assign the grade B+
to cs139
and the grade B
to cs239
(using the enumerated type LetterGrade).
- Add the following:
System.out.printf("Grade in CS139: %s\n", formatGrade(cs139));
System.out.printf("Grade in CS239: %s\n", formatGrade(cs239));
- Compile and execute Example2.java.
- What output was generated?
- Add code that compares the grades cs139
and cs239
and outputs:
"I did better in CS239 than in CS139\n"
"I did worse in CS239 than in CS139\n", or
"I got the same grade in CS239 and CS139\n".
- Compile and execute Example2.java.
- What
output was generated?
- Add code that calculates the
average grade (in quality points) in the two courses (using the pointsGrade() method).
- Add code that outputs "Average for CS139 and
CS239: ",
followed by the average, followed by "\n".
- Compile and execute Example2.java.
- What
output was generated?
5 Using a More
Sophisticated
Enumerated Type:
This part of the lab will help you understand how
you can overcome the shortcomings of simple enumerated types by adding
behaviors.
- Delete all .class files
- Download LetterGradeV2.java
if you haven't done so already.
- Open LetterGradeV2.java
in the editor and make sure you understand it.
- Open Example3.java
in the editor.
- Add a toPoints()
method to LetterGradeV2
that returns the points attribute
value.
- Compile LetterGradeV2
and correct any compile errors.
- Add a toString()
method to LetterGradeV2
that returns the symbol
attribute value.
- Compile LetterGradeV2.java
and fix any compile errors.
- Modify Example3.java
so that it outputs the grades in the two courses in the same format as
in Example2.java.
- Compile and execute Example3.java.
- What
output was generated?
- Modify Example3.java
so that it calculates the average and outputs it in the same format as
in Example1.java.
- Compile and execute Example3.java.
- What
output was generated?
- Modify Example3.java
so that it uses an array of two LetterGrade
objects rather than the variables cs139
and cs239.
- Compile and execute Example3.java.
- What
output was generated?
Updated 02/02/10 (nlh)