Java OOP

 

SHORT ANSWER QUESTIONS

CONSTRUCTORS

 

SOLUTIONS – PART 1

 

a)    constructor

b)    class

c)     Constructors do not have a return-type

d)    To initialize each instance variable

e)    statements:

           Point p = new Point();

           p.x = 2;

     p.y = 7;

f)      statements:

        Point p = new Point(6, 4);

g)    16.0

h)    3.0

i)       0.0

j)       5.0

k)     2.0

l)       missing code:

           fullName = firstName + " " + lastName;

          Reminder: The goal of the constructor is to give an initial value to the instance           variable(s).

 

SOLUTIONS – PART 2

 

m)   true

n)    overloading

o)    We use the this keyword.

p)    missing code:

           this.x = x;

           this.y = y;

 

q)    two instance variables and three constructors

r)      25

s)     Karen

t)      Jimmy

u)    For classes that have no constructor, Java automatically provides a default constructor.

v)     No. 

w)   Instance variables get their default values.  Integers get 0, doubles get 0.0, objects get null and so on.

x)     No.  You should always create your own constructor for a class.

y)     default

z)     constructor chaining

aa) first

bb) this

cc) this(0, 0);