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:
- Instance variables describe the position of the center and the lengths of
the semi-major and semi-minor axes.
- There exists a constructor to create an arbitrary ellipse.
- There exists a method to translate an ellipse.
- There is a method to draw the ellipse on the screen.
Define a class Circle
as subclass of the Ellipse
class
with the following characteristics:
- Instance variables describe the position of the center and the radius.
- There exists a constructor to create an arbitrary circle.
- There exists a method to translate a circle.
- There is a method to draw the circle on the screen.
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:
- Instance variables describe the positions of the vertices.
- There exists a constructor to create an arbitrary triangle.
- There exists a method to translate a triangle.
- There is a method to draw the triangle on the screen.
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: