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)
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.
-
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.
-
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.
- Create the
LetterGrade
enumeration.- Add an instance variable of type
double
calledminPercent
for the minimum percent score to achieve theLetterGrade
. - Implement the
LetterGrade
constructor that accepts adouble
forminPercent
. - Implement the
getMinimumPercent()
method that returns the minimum percent score to achieve theLetterGrade
. - 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.
- Add an instance variable of type
- Create the
Grade
class.- Add the
numQuestions
andnumCorrect
fields. (Neither getters nor setters will be necessary.) - Implement a constructor that sets the
numQuestions
andnumCorrect
fields. - 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 adouble
.
- Add the
- Create the
Student
class.- Implement a constructor that accepts the student ID.
- Implement a getter for the ID. (No setter will be necessary.)
- 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 itsAnswer[]
from a constructor parameter) or in the variable declaration (the way Quiz creates an emptyQuestion[]
). - Implement a getter for the
grades
. (No setter will be necessary.) - Implement
addGrade()
. This needs to copy the existinggrades
to a slightly larger array and add the newGrade
. There are examples in the providedQuiz
andQuestion
classes.
- Create the
Grader
class.- Add the
students
field and initialize it to an emptyStudent
array. - Implement
addStudent()
. LikeStudent.addGrade()
, this needs to expand and copy an array. - Implement
getLetterGrade()
. This will loop through the student’s grades, callinggetPercentScore()
on each, and calculate the average. Because theLetterGrade
values are in descending order, it can then simply loop throughLetterGrade.values()
and return the first one that is less than or equal to the student’s average. - Implement
administer()
. This will find the correct theStudent
, then callquiz.administer()
andquiz.getNumberOfQuestions()
. Finally, it will create a newGrade
using these values and add it to theStudent
. - Take a look at your code for
administer()
andgetLetterGrade()
. 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 astudentId
as its parameter, loops through the students until it finds a match, and returns thatStudent
.
- Add the
- Create the
-
Verify your results.
- 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.
- 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