Sunteți pe pagina 1din 4

Homework 1

Note: The numbers in parentheses refer to the Review Exercises


and Programming Exercises in Big Java 2nd Edition.
Problem 1, R9.1 (2nd ed., R11.1)
Suppose C is a class that implements the interfaces I and J. Which of
the following assignments require a cast?
C c = . . . ;
I i = . . . ;
J j = . . . ;

a. c = (C)i;

b. j = c;

c. i = (I)j;

Problem 2, R9.2 modified (2nd ed., R11.2)


Suppose C is a class that implements the interfaces I and J. Which of
the following assignments will throw an exception?
C c = new C();

a. I i = c;

b. J j = (J) i;

c. C d = (C) i;

COMP 121 Homework 1

None of these will throw an exception. Remember that we only have


one concrete object of class C, which is compatible with all three
types of references.

COMP 121 Homework 1

Problem 3, R9.4 (2nd ed., R11.4)


How does a cast such as (BankAccount) x differ from a cast of number
values such as (int) x?
When casting primitive types, you acknowledge a potential loss of
information. When casting object types, you take a risk of causing an
exception, and you are telling the compiler that you agree to that risk.

Problem 4, R9.2 (2nd ed., P11.2)


Define a class Quiz that implements the Measurable interface. A quiz
has a score and a letter grade (such as B+). Use the first
implementation of the DataSet class to process a collection of quizzes.
Display the average score and the quiz with the highest score (both
the letter grade and score).

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;
}

COMP 121 Homework 1

/**
* 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() + ")");
}
}

COMP 121 Homework 1

S-ar putea să vă placă și