Professional Documents
Culture Documents
Homework 01 Solution
Homework 01 Solution
a. c = (C)i;
b. j = c;
c. i = (I)j;
a. I i = c;
b. J j = (J) i;
c. C d = (C) i;
Quiz.java
/**
* Description: this class represents a quiz:
* a quiz has a score and a letter grade.
*
* @author Mihajlo Jovanovic
* @version 1.0 (May 21, 2008)
*/
public class Quiz implements Measurable {
private double score;
private String letterGrade;
/**
* Constructor method.
*
* @param score The quiz score
* @param letterGrade The quiz grade (example: "B+")
*/
public Quiz(double score, String letterGrade) {
this.score = score;
this.letterGrade = letterGrade;
}
/**
* See Measurable.getMeasure()
*/
public double getMeasure() {
return score;
}
/**
* Getter method for the score.
* @return The quiz score
*/
public double getScore() {
return score;
}
/**
* Getter method for the grade.
* @return The quiz grade
*/
public String getLetterGrade() {
return letterGrade;
}
}
ExP9_2.java
/**
* Description: this class processes a collection of quizzes,
* displaying the average score and the quiz with the highest score.
*
* @author Mihajlo Jovanovic
* @version 1.0 (May 21, 2008)
*/
public class ExP9_2 {
public static void main(String[] args) {
DataSet quizData = new DataSet();
quizData.add(new Quiz(89, "B+"));
quizData.add(new Quiz(90, "A-"));
quizData.add(new Quiz(75, "C"));
System.out.println("Average score is " +
quizData.getAverage());
Measurable max = quizData.getMaximum();
// Explicit cast needed here!
Quiz bestQuiz = (Quiz) max;
System.out.println("The highest score is " +
bestQuiz.getScore() + "("
+ bestQuiz.getLetterGrade() + ")");
}
}