Homework 1
Homework 1
If a problem consists of multiple classes, you can assume the classes are contained in the same .java file.
1. (5 pts) Explain why this code does not compile. Explain two solutions for fixing the error. What
value is assigned to instance variable v for each proposed solution?
2. (5 pts) Explain why this code does not compile. How would you fix it? What value is assigned to
instance variable v based on your solution?
3. (5 pts) Explain why this code has a logic error. How would you fix it? What value is assigned to
instance variable v based on your solution?
4. (5 pts) Explain why this code does not compile. How would you fix it? What value is assigned to
instance variable v based on your solution?
5. (5 pts) Given all the issues identified in problems 1 through 4, explain in detail why the following
code compiles without errors and initializes the instance variable based on the existing constructor
call in the main method. What value is assigned to instance variable v?
• DefenseDepartment
o General
▪ Private
7. (5 pts) Give at least one example of a reasonable field for each class in the following class hierarchy.
Be sure the field is at the right level of the hierarchy.
• Vehicle
o Car
o Airplane
▪ Passenger
▪ Fighter
▪ Bomber
o Spaceship
8. (5 pts) Give at least one example of a reasonable method for each class in the following class
hierarchy. Be sure the method is at the right level of the hierarchy. Do not include constructors,
getters, or setters.
• Vehicle
o Car
o Airplane
▪ Passenger
▪ Fighter
▪ Bomber
o Spaceship
9. (5 pts) Describe a class model consisting of three classes where two of the classes are in an
association relationship with each other, and two of the classes are in an inheritance relationship.
10. (5 pts) Describe a class hierarchy containing at least 5 classes to model trees (like the kind you find in
a forest). Give a short explanation for why the classes you are proposing are in good parent-child
relationships.
Grading Rubric:
Explains two solutions for fixing the Does not explain two solutions for fixing the
error. error.
Describes correct value assigned to Does not describe correct value assigned to
instance variable v for each solution. instance variable v for each solution.
Problem 2 5 points 0 points
Explains the compiler error. Does not explain compiler error.
Explains how to fix it. Does not explain how to fix it.
Describes correct value assigned to Does not describe correct value assigned to
instance variable v. instance variable v.
Problem 3 5 points 0 points
Explains the logic error. Does not explain logic error.
Explains how to fix it. Does not explain how to fix it.
Describes correct value assigned to Does not describe correct value assigned to
instance variable v. instance variable v.
Problem 4 5 points 0 points
Explains the compiler error. Does not explain compiler error.
Explains how to fix it. Does not explain how to fix it.
Describes correct value assigned to Does not describe correct value assigned to
instance variable v. instance variable v.
Describes the correct value assigned to Does not describe the correct value
instance variable v. assigned to instance variable v.