Lab: Experimenting with Sophisticated 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 a Simple Enumerated Type: This part of the lab will help you see some of the shortcomings of simple enumerated types.
  1. Open Example1.java in the editor.
  2. Add a declaration of a double variable named avg.
  3. What did you add?

  4. Add a declaration of two LetterGrade variables named cs139 and cs239.
  5. What did you add?

  6. Assign the grade B+ to cs139 and the grade B to cs239 (using the enumerated type LetterGrade).
  7. What did you add?

  8. Add the following:
    
    
           System.out.printf("Grade in CS139: %s\n", formatGrade(cs139));
           System.out.printf("Grade in CS239: %s\n", formatGrade(cs239));
           System.out.flush();
        
  9. Compile and execute Example1.java
  10. What output was generated?

  11. Add code that compares the grades cs139 and cs239 and outputs either "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".
  12. What code did you add?

  13. Add code that calculates the average grade (in quality points) in the two courses (using the pointsGrade() method).
  14. What code did you add?

  15. Add code that outputs "Average for CS139 and CS239: ", followed by the average, followed by "\n".
  16. What code did you add?

2. 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 LetterGrade.java
  3. Open LetterGrade.java in the editor and make sure you understand it.
  4. Open Example2.java in the editor.
  5. Add a toPoints() method to LetterGrade that returns the points attribute.
  6. What code did you add?

  7. Add a toString() method to LetterGrade that returns the symbol attribute.
  8. What code did you add?

  9. Modify Example2.java so that it outputs the grades in the two courses in the same format as in Example1.java.
  10. What code did you add?

  11. Modify Example2.java so that it calculates the average and outputs it in the same format as in Example1.java.
  12. What code did you add?

  13. Modify Example2.java so that it uses an array of two LetterGrade objects rather than the variables cs139 and cs239.
  14. What changes did you make?

3. Looking Ahead: This part of the lab will get you to start thinking about a topic you will study in the future.
  1. Copy Example2.java to Example3.java.
  2. Open Example3.java in the editor.
  3. Change Example3.java so that it uses an array of 1000 LetterGrade objects, only assigning values to elements 139 and 239.
  4. What changes did you make?

  5. Do you think the approach you used in Example3.java is a good or bad? Why?

Copyright 2007