Link Search Menu Expand Document

Class Diagrams

Learning Objectives Covered

By the end of this lesson, you will be able to translate a class diagram into code that describes public vs. private methods, constructors, primitive and references fields, and composition but does not describe inheritance, package-default or protected visibility.

Learning Materials


Time Commitment

Approximately 1-2 hours


Read (Approximately 30-40 minutes)

UML Class Diagrams, Part 1

NOTE: Reading the information in the various links throughout this reading is not required to understand and use UML Class Diagrams in this program. They are provided for further exploration if you so desire.


Try (Approximately 40-50 minutes)

In this try you will create a class diagram for a quiz program then create a grading program matching a different, provided class diagram.

  1. Download and open the BlueJ project QuizCD. This code is the beginning of a program that administers a multiple-choice quiz. Draw a class diagram for this program.

    You can verify your results by visually comparing with the solution class diagram. Account for any differences.

    You can also look at the PlantUML source, if you’re interested.

  2. Examine the class diagram for the quiz grading service. Add implementations for the classes described by the class diagram to the QuizCD project. Remember that it’s usually easiest to start with the class that has the fewest dependencies.

    Also note that the PlantUML source is available.

    1. Create the LetterGrade enumeration.
      1. Add an instance variable of type double called minPercent for the minimum percent score to achieve the LetterGrade.
      2. Implement the LetterGrade constructor that accepts a double for minPercent.
      3. Implement the getMinimumPercent() method that returns the minimum percent score to achieve the LetterGrade.
      4. Add all the LetterGrade enumeration values in descending order, as shown in the class diagram. Each value must use the constructor with the appropriate minimum percentage.
    2. Create the Grade class.
      1. Add the numQuestions and numCorrect fields. (Neither getters nor setters will be necessary.)
      2. Implement a constructor that sets the numQuestions and numCorrect fields.
      3. Implement getPercentScore(). Keep in mind that this should return the percent score: the number of correct answers / the number of questions * 100. Integer math will not be sufficient to calculate accurate scores; you’ll want to cast one of the operands to a double.
    3. Create the Student class.
      1. Implement a constructor that accepts the student ID.
      2. Implement a getter for the ID. (No setter will be necessary.)
      3. Add the grades field. Initialize it to an empty array. You can instantiate an empty array like: new Grade[0]. You can initialize grades in the constructor (for instance, Question initializes its Answer[] from a constructor parameter) or in the variable declaration (the way Quiz creates an empty Question[]).
      4. Implement a getter for the grades. (No setter will be necessary.)
      5. Implement addGrade(). This needs to copy the existing grades to a slightly larger array and add the new Grade. There are examples in the provided Quiz and Question classes.
    4. Create the Grader class.
      1. Add the students field and initialize it to an empty Student array.
      2. Implement addStudent(). Like Student.addGrade(), this needs to expand and copy an array.
      3. Implement getLetterGrade(). This will loop through the student’s grades, calling getPercentScore() on each, and calculate the average. Because the LetterGrade values are in descending order, it can then simply loop through LetterGrade.values() and return the first one that is less than or equal to the student’s average.
      4. Implement administer(). This will find the correct the Student, then call quiz.administer() and quiz.getNumberOfQuestions(). Finally, it will create a new Grade using these values and add it to the Student.
      5. Take a look at your code for administer() and getLetterGrade(). Is there code in common? Can we refactor so they both call a single method that looks a student up by their id? If you haven’t already, write a helper method that takes a studentId as its parameter, loops through the students until it finds a match, and returns that Student.
  3. Verify your results.

    1. Download GraderTester.java and add it to the project. If you’ve implemented all the classes and methods as described in the class diagram, nothing should need to be renamed. Execute it by right-clicking and selecting void main(String[] args). It will print messages indicating whether your code calculated the grade as expected for two different students.