| Homework 2DetailsHomework 2
- (12 points) Answer the following questions based on the UML diagrams from the Inheritance Exercise.
- How many fields does Rectangle have?
- How many constructors does Oval have?
- List all of the methods on DrawingPanel that are overloaded.
- Are any of the fields in any of the classes accessible from outside the class? If not, how do you know? If so, list them.
- If Circle was a subclass of Oval, would I be able to call drawFilledShape from a method in the Circle class? Why or why not?
- What is the return type of the method getFilled() in Rectangle?
- (20 points)
This exercise is adapted from exercise 8.12 from page 294 of OFWJ.
Assume the following classes are fully implemented (details have been
omitted for simplicity).
public class Person
{
String getName() {/* details omitted */};
}
public class Student extends Person
{
double getGPA() { /* details omitted */ };
}
public class Teacher extends Person
{
void giveGrade(Student s,int grade) { /* details omitted */ };
}
public class PhDStudent extends Student
{
boolean isABD() { /* details omitted */ };
}
Also assume we have the following code (which is error free):
Person p1 = new Student();
Person p2 = new PhDStudent();
Person p3 = new Person();
Person p4 = new Teacher();
PhDStudent phd1 = new PhDStudent();
Teacher t1 = new Teacher();
Student s1 = new Student();
For each of the following, tell whether the statement is legal,
will cause a compiler error,
or will cause a runtime error,
and briefly state why (if it is not legal).
Each statement should be thought of as independent from the others.
- t1 = new Person();
- Object obj = p1;
- s1 = (Student) new Object();
- s1 = p2;
- double gpa = p2.getGPA();
- double gpa = (Student) p2.getGPA();
- boolean abd = ((PhDStudent) p2).isABD();
- t1.giveGrade(phd1,1);
- p3.giveGrade(s1,1);
- ((Teacher) p4).giveGrade( (Student) (new PhDStudent()), 1);
(Hint: If you are having problems with any of these, see exercise 8.13).
|
|
|