CS 1713 Week 3:
Object Oriented Design



Reading: Chapter 7.1-7.4, Chapter 2.7-2.9

What is an object?

An object is the fundamental entity in a Java program.

Case Study 3: The Rectangle class

Create a project called cs03. All of the classes in this project will be in the rectangle package.

Suppose we want to calculate the area and perimeter of several rectangles. We could write out the formulas for these and place them in the code where necessary. It would be nice to be able to keep a rectangle's width and height together. It would be even nicer if a rectangle "knew" what its area and perimeter were. Herein lies the usefulness of the OO (object-oriented) approach. We organize the program into objects that have data (e.g., height and width) and behavior (e.g., can tell us height, width, area, and perimeter).

A description of a rectangle is:

A rectangle is-a four-sided figure in which the opposite sides are equal and all the internal angles measure 90o. The two sizes of sides are referred to as the length and width. Users of this class will want to find the area and find the perimeter of the rectangle. The values of the length and width will need to be accessible.

Develop a Rectangle class with the appropriate data (attributes or fields) and behavior (methods). The Rectangle will have a constructor of the following form:

public Rectangle(double len, double wid);
Write a test program RectangleTest that has a main program to create some Rectangle objects and call their methods. Develop the two classes incrementally together. (Write a little of the Rectangle class and then a little of the other.)

You can run the Java Simulator on this example here.

Variables for primitives and objects

Until now, variables held data of primitive type, usually: int, double, char, or boolean.
Example 1: Trace the following program segment, showing the values of the variables:
     int x1;
     int x2;
     int x3;

     x1 = 4;
     x2 = 2;
     x3 = x1;
     x1 = 7;
You can run the Java Simulator on this example here.

Variables that hold objects behave differently, they contain references to objects.

Example 2:

   Rectangle r1;
   Rectangle r2;
   Rectangle r3;

   r1 = new Rectangle(4,5);
   r2 = new Rectangle(2,3);
   r3 = r1;
You can run the Java Simulator on this example here.

Extending the Rectangle class

Suppose we want to make our rectangle drawable.
What additional state and behavior would be required?

Additional Attirbutes:

Additional Methods: How do we make a new rectangle class that corresponds to a drawable rectangle?
We could start from scratch or we could start with what we had.

We will make a DrawableRectangle that extends Rectangle.
This means that DrawableRectangle has everything that Rectangle has plus some new things.
We do not have to rewrite any of the old code, such as getArea().

Case Study G1, Part 1: The DrawableRectangle class

Create a project called gcs01.
All of the classes in this project will be in the applet package.
We will assume we already have the Rectangle object already written as well as a base tester.
You can use Case Study 3 as a starting point.

The DrawableRectangle will need to have a draw method that draws the rectangle.
Java does not let you draw on the main screen of your computer.
We need to create a Java object that allows drawing.
The are several ways of doing this, two of which are: We will start with applets and do frames later.

An introduction to Applets

So far, all of the programs that you have run have been applications.

A Java application has a main method when starts executing when the program starts.

A Java applet is a program that is designed to run from a web page.
Input usually comes from widgets such as buttons, scrollbars or text fields.
Instead of running main, as with applications, an applet starts by running init.
This is usually used to initialize the size of the applet and any variables.

Applets are very complicated objects, because they need to interact with the browser in very specific ways.
All of this work is done by the Java class JApplet.
We will use the concept of extending and object to make an applet that does what we want.

The drawing done by an applet is done in the paint method.
The method looks like this:

public void paint(Graphics g);
Here are some of the methods of the Graphics class that can be used for drawing:
   void drawLine(int x1, int y1, int x2, int y2);
   void drawRect(int x1, int y1, int width, int height);
   void drawString(String str, int x, int y);

Case Study G1, Part 2: Making an applet.

Write the draw method of the DrawableRectangle and write an applet to test this.

Applets are meant to be run from a browser.
You can find instructions for putting your applet on the web here.

Exercise 1:
Make your applet produce a diagram like the one below.
It contains 3 rectangles with the first at (10,10) and having width 300 and height 200. Each additional rectangle is centered having a height and width 20 less than the previous one.

Here are two ways of doing this:
Method 1: Have the paint method use drawRect to draw the rectangles directly.
Method 2: Create 3 rectangles in the init method can have the paint method call the draw method of each rectangle.

What are the advantages of these two approaches?

What would we do if we had 10, 20, or 100 rectangles? Use an array.

So far you have used arrays of primitives such as int or double.

Example 3:

   int[] x = {2,4,9,16,25};
   int[] y;

   y = new int[3];
   y[0] = 4;
   y[1] = x[2];
   y[2] = 15;
Notice the use if new to create an array.
That is because arrays are objects.

You can also have arrays of objects.

Example 4:

   Rectangle[] myRectangles;
   myRectangles = new Rectangle[3];
   myRectangles[0] = new Rectangle(5,7);
   myRectangles[1] = new Rectangle(10,50);
   myRectangles[2] = myRectangle[0];
Exercise 2:
Redo our applet using an array of DrawableRectangle objects.