Review for Quiz 2

One-page version suitable for printing.

Generics

Question 2-1

Name and explain a reason why generics (such as generic packages in Ada or templates in C++) are a useful feature for a programming language to include.

Solution

Object-oriented features

Question 2-2

Write the withdraw: method of our Account class so that the balance cannot drop below 0.

Solution

Question 2-3

Say you want to add a unary instance method into Smalltalk's built-in Integer class called abs, which returns the absolute value of the Integer instance to which the method is applied. Write the definition of this method.

Solution

Question 2-4

Name two significant and distinct differences between the object-oriented features of Java and Smalltalk. Do not address other differences, like their radically different syntax.

Solution

Question 2-5

Consider the following Java program.

class A {
    int f() { return 1; }
}

class B extends A {
    int f() { return 0; }
}

class Main {
    public static void main(String[] args) {
        A a = new B();
        System.out.println(a.f());
    }
}