LAB EXERCISE

 

RECTANGLE

and

Tic Tack Toe

 

 

Background:

 

1.    Professional programmers carefully design the classes they need before any coding is done. With well-designed classes, programming is much easier and the program has fewer bugs. Object-oriented design consists of deciding what classes are needed, what data they will hold, and how they will behave. All these decisions are documented (written up) and then examined. If something doesn't look right, it is fixed before any programming is done.

 

2.    The specifications of a class that models a rectangular shape would be:

 

Variables

private double myX;      // the x coordinate of the rectangle

private double myY;      // the y coordinate of the rectangle

private double myWidth;  // the width of the rectangle

private double myHeight; // the height of the rectangle

 

// Creates a 500 x 500 drawing area, by using the Java Graphics class.

 

Constructors

// Creates a default instance of a Rectangle object with all dimensions

//   set to zero.

Rectangle()

 

// Creates a new instance of a Rectangle object with the left and right

// edges of the rectangle at x and x + width. The top and bottom edges

// are at y and y + height.

Rectangle(double x, double y, double width, double height)                                          

 

Methods

// calculates and returns the perimeter of the rectangle

public double getPerimeter()

 

// Calculates and returns the are of the rectangle.

public double getArea()

 

// Draws a new instance of a Rectangle object with the left and right

// edges of the rectangle at x and x + width. The top and bottom edges

// are at y and y + height.

public void draw()


Assignment:

 

1.    Implement a Rectangle class with the following properties.

 

a.    A Rectangle object is specified in the constructor with the left and right edges of the rectangle at x and x + width. The top and bottom edges are at y and y + height.

b.    A method getPerimeter calculates and returns the perimeter of the Rectangle.

c.    A method getArea calculates and returns the area of the Rectangle.

d.    A method draw displays a new instance of a Rectangle object.

2.    Write a testing class with a main method that constructs a Rectangle and calls getPerimeter and getArea for each Rectangle created.  Sample usage would be:

 

// Construct a 400 x 160 rectangle at location -200, -80.

Rectangle rectA = new Rectangle(-200, -80, 400, 160);

   rectA.draw();  // draw the rectangle

 

   System.out.println("Perimeter = " + rectA.getPerimeter());

   System.out.println("Area = " + rectA.getArea());

 

The resulting images would be similar to the one shown below:

 

 

3.    Construct a 3x3 grid of Rectangle objects as show below. You should be able to produce the grid with only 3 rectangles. In addition, calculate and display the perimeter and area of the rectangles.

 

 

      First write the RectangleTester driver class and the Rectangle class

      You can start the Rectangle class with this code…

 

import java.applet.*;

import java.awt.*;

 

// Remember to run this program as an Applet

public class Rectangle extends Applet{

      private double myX;      // the x coordinate of the rectangle

      private double myY;      // the y coordinate of the rectangle

      private double myWidth;  // the width of the rectangle

      private double myHeight; // the height of the rectangle

     

      Graphics g;

     

// This creates a no-argument constructor

      public Rectangle(){

             myX = 1;

             myY = 1;

             myWidth = 150;

             myHeight = 150;

      }

     

      public Rectangle(double x, double y, double width, double height){

             myX = x;

             myY = y;

             myWidth = width;

             myHeight = height;

             this.paint(g);

      }

     

      // a stub that needs to be filled in by you

      public double getPerimeter(){

             double temp= 0.0;

             return temp;

      }

 

       

 

//    Calculates and returns the are of the rectangle.

      // a stub that needs to be filled in by you

      public double getArea(){

             double temp= 0.0;

             return temp;

      }

 

       

      // to call paint it needs to be overwritten

      // a small beginning.

      public void paint(Graphics g){

             // draw a big rectangle using the Graphics class

             g.setColor(Color.red);

             g.drawRect((int)myX, (int) myY, (int) myWidth, (int) myHeight);

              

      }

 

}

 

Steps to completion

  1. Create a 3x3 grid that returns the perimeter and area of each rectangle
  2. Draw a Tic Tack Toe board
  3. Complete the Game