Title: Classes Part 2 : Rectangle
Course: CPS 161: An Intro to Programming with Java - DL MM MASTER

Defining Classes Part 2

Rectangle with:

instance variables:

- length
- width
- x
- y

methods:

- getArea()
- getPerimeter()
- equals method().
- Demo parameter conversion.
- boolean overlap(Rectangle )
- Point class ... Class within a class

The key word "this"

1. Demo usage in setting instance variables with setRectangle type routines.
2. Note that within a class "this." is implied.
3. "this" can only be used inside of a class. It always references the current object. Note what happens when you try to use it in a static method like main.

Code from Video


class Point
{
    int x, y;
}


public class Rectangle {
    private int x, y, width, height;
    int someVar;

    public void setRectangle(int x, int y, int width, int height)
    {
        this.x = x;
        this.y = y;
        this.width = width;
        this.height = height;
    }
    private int min(int v1, int v2)
    {
        if (v1 < v2)
            return v1;
        else
            return v2;
    }
    private int abs(int v1)
    {
        if (v1 >= 0)
            return v1;
        else
            return -v1;
    }
    public void setRectangle(Point p1, Point p2)
    {
        x = min(p1.x, p2.x);
        y = min(p1.y, p2.y);
        width = abs(p1.x - p2.x);
        height = abs(p1.y - p2.y);
    }

    public String toString()
    {
        return x + ":" + y + ":" + width + ":" + height;
    }

    public int getArea()
    {
        return width * height;
    }
    public int getPerimeter()
    {
        return 2* (width + height);
    }
    boolean equals(Rectangle otherRect)
    {
        if (x == otherRect.x && this.y == otherRect.y &&
                width == otherRect.width && height == otherRect.height)
            return true;
        else
            return false;
    }

    boolean inside (int xp, int yp)
    {
        if (xp >= x && xp <= (x+width) &&
                yp >= y && yp <= y+height)
            return true;
        else
            return false;
    }

    boolean overlaps( Rectangle r)
    {
        if (inside(r.x, r.y)  || inside( (r.x+r.width), r.y) ||
                inside (r.x, r.y + r.height) || inside (r.x+r.width, r.y+r.height))
                return true;
        else
            return false;
    }

    public static void main(String[] args) {
        Rectangle r1 = new Rectangle();

        //this.someVar = 8;

        byte b1=0, b2=0, b3=5;
        long l4 = 3;

        r1.setRectangle(b1, b2, b3, (int)l4);
        System.out.println( r1.toString());

        System.out.println("area =" + r1.getArea());
        System.out.println("perimeter =" + r1.getPerimeter());

        Rectangle r2 = new Rectangle();
        r2.setRectangle(3, 2, 5, 3);

        if (r1.equals(r2))
            System.out.println("Rectangles are EQUAL");
        else
            System.out.println("Rectangles are NOT EQUAL");

        if (r1.overlaps(r2))
            System.out.println("r1 and r2 overlap");


    }

}



Last Modified: September 30, 2013 3:22 PM