Fall 1999 - 492/1 (JAVA) - Midterm # 2 -... Page 1 of 6 Take a look at the following code:

Transcription

Fall 1999 - 492/1 (JAVA) - Midterm # 2 -... Page 1 of 6 Take a look at the following code:
Fall 1999 - 492/1 (JAVA) - Midterm # 2 - Sample Solution
Page 1 of 6
• Question 1. (6 points)
Take a look at the following code:
1
2
3
4
5
6
7
8
9
10
11
class B {
void f() { ... }
void g() { ... }
}
class E extends B {
void g() { ... }
}
class test {
static void p(B x) {
x.g() ;
}
static void main(String a[]) {
E e = new E() ;
p(e) ;
e.f() ;
}
12
13
14
15
16
17
}
Explain where in the code above, each of the following basic concepts of
object-oriented programming is illustrated. Refer to the line numbering
in your answer.
1. An object of a derived type is also an object of the base class.
On line 14, object e which is of type E is passed as an argument to p() which
is a method that expects an argument of type B. But that is legal since class
E is derived from B.
2. A derived class inherits all the methods of the base class.
On line 15, we called method f() on object e which is of type E. Although
no f() method was defined on class E, we were able to do so since E inherits
all methods from its base class B which defined method f().
3. JAVA supports dynamic binding of methods.
On line 10, resolving which g() to call depends on the type of object x. For
example, when p(e) is executed on line 14, the overriding g() of class E is
executed rather than the base g().
Fall 1999 - 492/1 (JAVA) - Midterm # 2 - Sample Solution
Page 2 of 6
• Question 2. (6 points)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
class B {
int i ;
B(int _i) {
int g() {
int h() {
}
i = _i ;
}
return h() ; }
return -1 ; }
class E extends B {
int i ;
E(int _i) { super(10) ; i = _i ;
int h() { return 2 * i ;
}
}
}
B b = new B(1) ;
E e = new E(3) ;
B be = e ;
Fill in the result of evaluating each of the following expressions:
b.i
1
e.i
3
be.i
10
b.g()
-1
e.g()
6
be.g()
6
• Question 3. (8 points)
4
class
class
class
class
5
X x = new X() ;
1
2
3
B
C
X
Y
{ ... }
extends B { ... }
extends C { ... }
extends C { ... }
Indicate if each of the following expressions would generate a compiletime error, a run-time error, or if it would evaluate to TRUE or FALSE.
Compile
Error
Runtime
Error
True/
False
true
return (x instanceof B) ;
true
B b = x ;
return (b instanceof C) ;
X
B b = x ;
Y y = (Y) b ;
return (y instanceof B) ;
false
return (x instanceof Y) ;
Y y = x ;
return (y instanceof B) ;
X
Fall 1999 - 492/1 (JAVA) - Midterm # 2 - Sample Solution
Page 3 of 6
• Question 4. (8 points)
Consider the following class definition.
class Person {
private String name ; int age ;
static int N ; static Person[] Ps = ... ;
public String getName() { return name ; }
public Person(String n, int a) {
name = n ; age = a;
Ps[N++] = this ;
}
}
• Write a complete definition of class Student. Every Student is a Person.
In addition, each Student has a GPA field which is of type double and is initially
set to 0.0
class Student extends Person {
double GPA = 0.0 ;
Student(String name, int age) {
super(name,age) ;
}
}
• Override the getName method in Student. The new method should return
the name of the student concatenated to the string "(student)".
public String getName() {
super.getName() + " (student)" ;
}
• Complete the following method which is part of the Student class and
that utilizes the fields Ps and N to print the names of all the Students only.
public static void printAll() {
for (int i = 0 ; i < N ; i++)
if (Ps[i] instanceof Student)
System.out.println(Ps[i].getName()) ;
}
Fall 1999 - 492/1 (JAVA) - Midterm # 2 - Sample Solution
Page 4 of 6
• Question 5. (10 points)
DO YOUR WORK ON THE NEXT PAGE
You’re given the following code:
class Figure extends Canvas {
private Color myColor ;
public void setColor(Color c) { myColor = c ;
public void paint(Graphics g) {
g.setColor(myColor) ;
...
}
...
}
}
interface Colorable {
void changeColor(Color c)) ;
}
Objects that are Colorable change their color to c whenever the changeColor(c)
method is called.
• Define CFigure which is an extension of Figure such that it implements the Colorable interface.
• Define class CButton which is an extension of Button and has the
following constructor:
CButton(String s, Color c, Colorable o) ;
CButton should display the String s as its caption, and whenever the user
clicks on it, the Colorable o should automatically change its color to c.
• Use CButton and CFigure to implement
the applet you see on the right. The
applet displays CFigure on the top portion.
Whenever the user clicks any of the three
buttons, the figure should change to the respective color.
Fall 1999 - 492/1 (JAVA) - Midterm # 2 - Sample Solution
Solution page for question 5.
class CFigure extends Figure implements Colorable {
void changeColor(Color c) {
setColor(c) ;
repaint() ;
}
}
class CButton extends Button implements ActionListener {
Color c ;
Colorable o ;
CBurron(String s, Color _c, Colorable _o) {
super(s) ;
c = _c ;
o = _o ;
addActionListener(this) ;
}
public void actionPerformed(ActionEvent e) {
o.changeColor(c) ;
}
}
class Q extends Applet {
void init() {
CFigure cf = new CFigure() ;
add(cf) ;
add(new CButton("red",Color.red,cf)) ;
add(new CButton("green",Color.green,cf)) ;
add(new CButton("blue",Color.blue,cf)) ;
}
}
Page 5 of 6
Fall 1999 - 492/1 (JAVA) - Midterm # 2 - Sample Solution
Page 6 of 6
• Question 6. (8 points)
Contrast each pair of items listed below. Do not define the two terms.
Emphasize on the difference between them.
• this() v.s. super().
this() is used within a construcor to call other constructors of the same
class. super() is used within constructors to call constructors of the parent
class.
• overloading v.s. overriding.
Overloading is having two different methods with the same name but with
different signatures. Overriding is having two different methods with the
same signature. Overriding can only happen across derived classes.
• Event-listeners v.s. event-adapters.
Event-listeners are interfaces. An event-adapter is a class implementing an
event-listener with all methods being implemented as empty procedures.
• Abstract v.s. concrete classes.
You can’t instantiate an abstract class
• Question 7. (4 points)
List three differences between abstract classes and interfaces.
1. An abstract class may contain field declarations and method definitions.
Interfaces contains only constants and method declarations.
2. interfaces support multiple inheritance while classes support single inheritance only. Similarly, a class may implement multiple interfaces but can
only extend a single abstract class.
3. All methods of an interface are public.
MATH 492/1 (Java)
Second Midterm
December 11, 1999
Fall 1999
• This is a closed book, closed notes exam.
• Please do all your work in this booklet. The space provided should be enough for
your work. You may use the backside for scratch work.
• Before you start, write your name, student ID, and class serial number on this
cover.
• Check that you have 6 different pages.
• You have 50 minutes to finish your work.
• Relax!
Scores:
1. (6)
2. (6)
Name:
Sample Solution
3. (8)
4. (8)
Student ID:
5. (10)
6. (8)
7. (4)
total (50)
Serial Number: