Homework 5

Java Language Programming

 

bullet

Due in the class on Sunday 30th of April 2000

bullet

Late submissions are not accepted

bullet

You can discuss this homework with others, but the submitted code must be yours

bullet

Write and test the following Java programs and submit a hardcopy of your source code. Each program must be printed on a separate page. Don’t forget to write your name and registration number.

Problem 1

Create two classes, A and B, with default constructors (empty argument lists) that announce themselves. Inherit a new class called C from A, and create a member B inside C. Do not create a constructor for C. Create an object of class C. What messages are printed when the program is run?


Problem 2

Type the following Java program in a file called Cartoon.java.

class Art {
    Art() {
        System.out.println("Art constructor");
    }
}
class Drawing extends Art {
    Drawing() {
        System.out.println("Drawing constructor");
    }
}
public class Cartoon extends Drawing {
    Cartoon() {
        System.out.println("Cartoon constructor");
    }
    public static void main(String[] args) {
        Cartoon x = new Cartoon();
    }
}

A) What messages does this program display?

B) Comment out the constructor for the Cartoon class. Explain what happens when you attempt to compile and run the resulting file.


Problem 3

Type the following Java program in a file called Chess.java.

class Game {
    Game(int i) {
        System.out.println("Game constructor");
    }
}
class BoardGame extends Game {
    BoardGame(int i) {
        super(i);
        System.out.println("BoardGame constructor");
    }
}
public class Chess extends BoardGame {
    Chess() {
        super(11);
        System.out.println("Chess constructor");
    }
    public static void main(String[] args) {
        Chess x = new Chess();
    }
}

A) What messages does this program display?

B) Comment out the constructor for the Chess class. Explain what happens when you attempt to compile and run the resulting file.


Problem 4

Write an applet that draws a solid rectangle and allows the user to select the dimensions and color of the drawn rectangle.

 

Problem 5

Write a calculator applet that allows the user to perform the 4 arithmetic operations (+, -, *, and /) on integer numbers.

Problem 6

Extend Problem 4 such that the user additionally specifies the location of the rectangle in a special drawing area inside the applet, and allow the user to delete or add up to 10 rectangles on the same drawing area.