5.3:
What are unchecked exceptions?
Unchecked exceptions occur when bad data is added by the user. They do not get checked at
the compile time and cannot be recovered. They do not usually need to be handled and it is
better for the user when they are not because there will be a stack trace printed that will help
the user locate the bug.
Example:
Class Main {
Public static void main(String args[]) {
Int x = 9/0;
}
}
5.5:
Why don’t you need to declare that your method might throw an
IndexOutOfBoundsException?
You don’t need to declare it because it’s a descendant of Runtime Exception. It is also an
unchecked exception and exceptions don’t need to be declared by the throws reserved word
since they are caused by errors in the code that the programmer should have noticed already
while they were testing the code.
5.6:
Taylour Ballard
ID: tballar5
Homework 1
Is the type of the exception object always the same as the type declared in the catch clause
that catches it? If not, why not?
Yes, the type of exception object is always the same as the type declared in the catch clause
that catches it. If there was not matching exception object, the catch clause would terminate.
6.3
a: In a static method, it is easy to differentiate between calls to instance methods and calls to
static methods. How do you tell the method calls apart?
The instance method will be called using object and the class method will be called using Class
name.
b: Why is it not easy to distinguish between calls to the instance and static methods which
are called from an instance method?
It is not easy because instance methods are called using reference and class methods are
independent of any objects so they could still have a reference. Instance methods can access
static variables and static methods directly but not the other way around.
6.8
(b) int a2 = H01_65.mY;
This is an illegal statement and will not compile because mY is private, so it can’t access outside
of the class.
(c) int a3 = H01_65.A;
This will compile because it is public. A3 = 100
(f) int a5 = cObj1.getX();
It will compile and a5 = -1
(h) cObj2.setX(cObj1.getX());
It will compile and set the value of mX to -1.
(k) int a7 = cObj1.getY();
It will compile and a7 = 0.
(m) int a8 = H01_65.getY();
It will not compile because the setY method is not a static method and cannot be accessed
using its class name in another class.
7.2
(a) What type of class relationship, if any, exists between Course and Roster? If there is a
relationship, modify the diagram to indicate the relationship and label each end of the
relationship with a multiplicity.
They have a relationship because Course has the responsibility for creating and destructing the
Roster. Course is not dependent on Roster; however, Roster is dependent of the Course class
object.
(b) What type of class relationship, if any exists between Roster and Student. If there is a
relationship, modify the diagram to indicate the relationship, if any, exists between Roster
and Student. If there is a relationship, modify the diagram to indicate the relationship and
label each end of the relationship with a multiplicity.
Roster has an aggregation relationship with the class object Student because the Student class
object may outlive the Roster.
(c) What type of class relationship, if any, exists between Student and UndergradStudent?
If there is a relationship, modify the diagram to indicate the relationship and label each end
of the relationship with a multiplicity.
They have a generalization relationship because UndegradStudent is a subclass of Student.
(d) What type of class relationship, if any exists between Student and GradStudent? If
there is a relationship, modify the diagram to indicate the relationship and label each end
of the relationship with a multiplicity.
They have a generalization relationship because GradStudent is a subclass of Student.
(e) What type of class relationship, if any exits between UndergradStudent and
GradStudent? If there is a relationship, modify the diagram to indicate the relationship
and label each end of the relationship with a multiplicity.
There isn’t a relationship between GradStudent and UndergradStudent.