Problem 1 was a Code Warrior exercise.
Solutions for problems 2 to 4:
2. [5 pts] Do exercise 3, p. 111.
Ans:
3. [5 pts] Describe the relationship we want among subclasses.
For example, how do we know something should be a subclass of another class?
Ans:
4. [5 pts] Why is encapsulation and information hiding
important for programs? How can we achieve these in Java?
Ans:
An object can be accessed or its
state altered only through the public methods - everything else is "hidden"
from the "outside world". This is the concept of Information Hiding. The
idea behind it is that you do not really need to know how a class implements
a method when you pass a message to an object of that class. You can "trust"
it to behave correctly.
a)BorderLayout/Flowlayout -
No subclass<->superclass relationship.
b)TextArea/TextComponent -
TextArea is a subclass of TextComponent
c)TextField/Component -
TextField is a subclass of Component
d)Button/Component -
Button is a subclass of Component
e)Panel/Object - Panel
is a subclass of Object
To determine if a class A should be a
subclass of another class B, we look for an "is-a" relationship between
the two. ie, we see if the statement A is-a B makes sense. If it does,
then we make A a subclass of B.
Encapsulation is important because
when it comes to large projects, it is easier to develop the components
of the project and then "plug" them together. So we should be able to "trust"
the components to exhibit the behavior we expect of them(or in other words,
we expect them to perform their "roles" perfectly). This is achieved by
splitting up the responsibilities between the different easily manageable
classes and encapsulating it.