3.4 Exercises



1. Inheritance of Geometrical Shapes

Describe the class hierarchy of circles, ellipses, squares, and rectangles, which are all examples of two-dimensional geometrical shapes.

2. Ellipses and Circles

Define a class Ellipse with the following characteristics: Define a class Circle as subclass of the Ellipse class with the following characteristics: Write an applet that uses your Ellipse and Circle class to draw an ellipse or a circle on the screen. It should look like

3. Random Ellipses and Circles

Make your applet in the previous exercise more interactive by adding a button to it that when pressed draws on the screen randomly an ellipse or circle at random position and size. It should look like

4. Colorable Random Ellipses and Circles

Extend the applet in the previous exercise so that pressing the button also means a random selection of the color of the drawing from the set of three colors red, green, and blue. It should look like

5. Triangles

Define a class Triangle with the following characteristics: A triangle is like an ellipse and a circle a geometrical shape. How would you use inheritance of classes to implement the geometrical shapes? Write an applet that illustrates your style of writing Java code. It should draw randomly triangles, ellipses, and circles, and therefore look like

6. Mondriaan

The following applet draws on the screen rectangles and squares with randomly chosen position, size, and color.

The source files are The other source files Ellipse.java, Circle.java, and Triangle.java only contain dummy definitions:
class Ellipse extends Shape {
}

class Circle extends Ellipse {
}

class Triangle extends Shape {
}
Write the Java code of these classes so that the applet will draw closed shapes and so that it will also randomly draw ovals, disks, and triangles. Your applet should look as follows: