2.3 Exercises

1. Design of an Address class

  1. Describe state and behavior of the real-world object address, that contains all information necessary to send a letter to someone. Implement this in an Address class.
  2. Compare this to our implementation (click on the link) and discuss the possible differences.

2. Drawing Geometrical Shapes

Write an applet that
  1. draws a blue disk on the screen;
  2. draws a red colored square on the screen;
  3. draws a filled triangle on the screen.
Hint: useful methods from the awt package are fillRect, fillOval, and fillPolygon.

An array, say of three integers 0,1, and 2, can be created and initialized by the statement

int[] intValues = {0,1,2};

3. A Disk Class

In this exercise we are going to create a new class, called Disk, to build up and work with disks. The class contains three instance variables: the x and y coordinates of the center, and the radius. Instance methods are translate and draw for translating a disk and drawing a disk on the screen.
  1. Define the Disk class with only the constructors Disk() and Disk(int a, int b, int r) for creation of the unit disk around the origin and the disk with center (a,b) and radius r, respectively.
  2. Implement the methods translate and draw.
  3. Write a Java applet that draws a Disk object on the screen.
  4. Implement more interactivity to the previous applet by adding a button that makes that the disk resizes and repositions at random. The applet should look like:

  5. Implement two methods, say enlarge and shrink, to double or halve the radius of a disk, respectively. Write an applet that draws a Disk object on the screen together with two buttons to enlarge and shrink the circle. The applet should look like:

    Hint: in the action(Event evt, Object arg) { ... } method to handle the action of pressing one of the buttons, the variable evt.target refers to the name of the button actually pressed. You can compare the value of this variable with the names of your buttons to find out which button was pressed and what action should be undertaken.

4. Medians of a Triangle (optional exercise)

In this exercise we are going to create a new class, called Triangle, to build up and work with triangles. The class contains six instance variables: x1, y1, x2, y2, x3, y3, which are the x and y coordinates of the three vertices. Instance methods are drawTriangle and drawMedians for drawing a triangle on the screen and for drawing the medians of a triangle on the screen.
  1. Define the Triangle class with only the constructor Triange(int X1, int Y1, int X2, int Y2, int X3, int Y3) for creation of a triangle with vertices (X1, Y1), (X2, Y2), and (X3, Y3), respectively.
  2. Implement the method drawTriangle and write a Java applet that illustrates that everything works as supposed.
  3. Implement more interactivity to the previous applet by adding a button that generates a new randomly sized and positioned triangle when pressed. The applet should look like:

  4. Implement the method drawMedians that draws the medians of the current triangle. A median is a line from a vertex of the triangle to the midpoint of the opposite side. If the vertices have coordinates (X1, Y1), (X2, Y2), and (X3, Y3), then the medians are the lines connecting
    • (X1, Y1) and ((X2+X3)/2, (Y2+Y3)/2);
    • (X2, Y2) and ((X1+X3)/2, (Y1+Y3)/2);
    • (X3, Y3) and ((X1+X2)/2, (Y1+Y2)/2).
    Extend the previous applet by drawing the medians of the given triangle as well. Verify that your method drawMedians works fine (e.g., do your medians intersect in one point?). The applet should look like: