JamesMadisonUniversity

Computer Science Department


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:

  1. If you have not already done so, read chapter 9.9 in the Gaddis book and the Java Tutorial for Enum.
  2. Answer as many of the following questions as you can during the lab period. 

2 Getting Ready:

Before going any further you should:

  1. Make a directory for this lab.
  2. Setup your development environment.
  3. 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.

  1. Open Example1.java in the editor.
  2. Compile and execute Example1.java.
  3. What output was generated?
  4. What determines the order used by the compareTo() method?
  5. What.class files were created when you compiled Example1.java? (Hint: Look for all files that start with "Example1" and end with ".class".)
  6. 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.
  1. Delete all of the .class files in the directory you created for this lab.
  2. Compile LetterGrade.java and Example1.java.
  3. What.class files were generated?
  4. Execute Example1.
  5. What output was generated?
  6. 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.

  1. Open Example2.java in the editor.
  2. Add a declaration of a double variable named avg in the main method.
  3. Add a declaration of two LetterGrade variables named cs139 and cs239.
  4. Assign the grade B+ to cs139 and the grade B to cs239 (using the enumerated type LetterGrade).
  5. Add the following:
    System.out.printf("Grade in CS139: %s\n", formatGrade(cs139));
    System.out.printf("Grade in CS239: %s\n", formatGrade(cs239));
  6. Compile and execute Example2.java.
  7. What output was generated?
  8. 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".
  9. Compile and execute Example2.java.
  10. What output was generated?
  11. Add code that calculates the average grade (in quality points) in the two courses (using the pointsGrade() method).
  12. Add code that outputs "Average for CS139 and CS239: ", followed by the average, followed by "\n".
  13. Compile and execute Example2.java.
  14. 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.

  1. Delete all .class files
  2. Download LetterGradeV2.java if you haven't done so already.
  3. Open LetterGradeV2.java in the editor and make sure you understand it.
  4. Open Example3.java in the editor.
  5. Add a toPoints() method to LetterGradeV2 that returns the points attribute value.
  6. Compile LetterGradeV2 and correct any compile errors.
  7. Add a toString() method to LetterGradeV2 that returns the symbol attribute value.
  8. Compile LetterGradeV2.java and fix any compile errors.
  9. Modify Example3.java so that it outputs the grades in the two courses in the same format as in Example2.java.
  10. Compile and execute Example3.java.
  11. What output was generated?
  12. Modify Example3.java so that it calculates the average and outputs it in the same format as in Example1.java.
  13. Compile and execute Example3.java.
  14. What output was generated?
  15. Modify Example3.java so that it uses an array of two LetterGrade objects rather than the variables cs139 and cs239.
  16. Compile and execute Example3.java.
  17. What output was generated?

Updated 02/02/10 (nlh)