CSCI 235 Spring 2013
Data Structures and Software Design
Archived Class
Charles Cusack
Computer Science
Hope College
Main
Schedule
Grading
Gradebook

Policies
Advice
College
    Policies

Notes
Programs
Tutorials

CSCI 385
MATH 160
Others

Admin

Homework 2

Details

Homework 2

  1. (12 points) Answer the following questions based on the UML diagrams from the Inheritance Exercise.
    1. How many fields does Rectangle have?
    2. How many constructors does Oval have?
    3. List all of the methods on DrawingPanel that are overloaded.
    4. 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.
    5. 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?
    6. What is the return type of the method getFilled() in Rectangle?
  2. (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.

    1. t1 = new Person();
    2. Object obj = p1;
    3. s1 = (Student) new Object();
    4. s1 = p2;
    5. double gpa = p2.getGPA();
    6. double gpa = (Student) p2.getGPA();
    7. boolean abd = ((PhDStudent) p2).isABD();
    8. t1.giveGrade(phd1,1);
    9. p3.giveGrade(s1,1);
    10. ((Teacher) p4).giveGrade( (Student) (new PhDStudent()), 1);

    (Hint: If you are having problems with any of these, see exercise 8.13).