Practice w23am1q3
Liskov Substitution Principle
This Java code is poor! It violates the Liskov Substitution principle!
Restructure this code as UML and draw a UML Class Diagram of restructured code that doesn't violate the Liskov Substitution principle.
Draw a well-designed UML class diagram to represent this information. Provide the basic abstractions, attributes, methods, relationships, multiplicities, and navigabilities as appropriate.
"...” means much code is omitted.
This Java code is about quizzes (similar to eClass quizzes).
Be sure to leave a note on the diagram highlighting how you address the violation
class Quiz {
protected Question[] questions;
// ...
public void printAnswerKey() {
for (int i = 0; i < questions.length; i++) {
System.out.println(questions[i].getCorrectAnswer());
}
}
}
class Exam extends Quiz {
protected Room room; // unlike quizzes, exams are administered in a room
// ...
}
class Question {
protected String question;
protected String[] answers; // choices
protected int correctAnswer; // index of a correct answer
// ...
public int getCorrectAnswer() {
return correctAnswer;
}
}
class EssayQuestion extends Question {
// ...
public int getCorrectAnswer() {
System.err.println("Essay questions do not have a correct answer.");
return -1;
}
}