Lab: Experimenting with Enumerated Types


Instructions: Answer as many of the following questions as you can during the lab period. If you are unable to complete the assignment during the lab period it is strongly recommended that you complete it on your own.

Getting Ready: Before going any further, you should:

  1. Make a directory for this lab.
  2. Setup your development environment.
  3. Download the following files:
    to your working directory. (In most browsers, the easiest way to do this is by right-clicking on each of the links above.)

1. Using Integer Constants Instead of Enumerated Types: 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.
  1. Open Constants, SemesterUtiltities, and Example1.java in the editor.
  2. Compile Constants, SemesterUtiltities, and Example1.java.
  3. Execute Example1.
  4. What output was generated?

  5. Change FALL to TALL in Constants.java.
  6. Compile Constants.java.
  7. Execute Example1.
  8. What output was generated?

  9. You might be surprised that 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.)

  10. Compile Example1.java.
  11. What error was generated?

  12. Change TALL back to FALL in Constants.java.
  13. Add the constant SUMMER to Constants.java and assign it the value 2.
  14. Compile Constants.java and Example1.java (remembering that it is very important to compile Example1.java even though it did not change).
  15. Execute Example1.
  16. What output was generated?

  17. What is wrong with this output and what caused the problem?

  18. Put SUMMER and FALL in the proper order (in Constants.java) and adjust their values accordingly.
  19. Compile Constants.java and Example1.java.
  20. Execute Example1.
  21. What output was generated?

  22. What is wrong with this output and what caused the problem?

  23. Add the line:
        System.out.println(SemesterUtilities.startingMonth(7));
        

    to the main() method in Example1.java.

  24. Will this version of Example1.java compile?

  25. What output would be generated by this statement?

  26. Why is this somewhat troubling?

  27. What output would be generated by the statement:
        System.out.println(Constants.SPRING);
        

  28. What output would be more informative for the previous statement?

2. Using String Constants Instead of Enumerated Types: 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.
  1. Open Example2.java in the editor.
  2. Compile and execute Example2.java.
  3. What output was generated?

  4. The method String.compareTo(java.lang.String) java.lang.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.
  5. What code did you add to the main() method in Example2.java.

  6. Add the line:
           System.out.println("Better grade: "+better);
        

    to the end of the main() method in Example2.java.

  7. Compile and execute Example2.java.
  8. What output was generated?

  9. What is wrong with this output and why?

  10. How does a "B" compare to a "B-" at JMU and in Java?

3. Using a Simple Enumerated Types: 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 Example3.java in the editor.
  2. Compile and execute Example3.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 Example3.java? (Hint: Look for all files that start with "Example3" and end with ".class".)

  6. Move the lines:
    public enum LetterGrade
    {
           F, D, DPLUS, CMINUS, C, CPLUS, BMINUS, B, BPLUS, AMINUS, A;       
    }
        

    from Example3.java and into a file named LetterGrade.java.

  7. Delete all of the .class files in the directory you created for this lab.
  8. Compile LetterGrade.java and Example3.java.
  9. What .class files were generated?

  10. Execute Example3.java.
  11. What output was generated?

  12. Recently, JMU instituted a grade of "D-". What changes would you need to make to LetterGrade.java to support this new policy?

4. Creating a Simple Enumerated Types: This part of the lab will give you some experience creating a simple enumerated type.
  1. Create an enumerated type named Sessions.java that includes a Spring, Summer, and Fall semester (in the appropriate order for a calendar year).

  2. Modify SemesterUtilities.java and Example1.java so that they work correctly with this enumerated type. (Hint: Think about using a "for each" loop.)

Copyright 2010