Title: Inheritance Examples
Course: CPS 161: An Intro to Programming with Java - DL MM MASTER

Inheritance Examples

TitledEmployee extends SalariedEmployee

private String title;
public String getTitle()
public void setTitle(String)
public String getName() --- override

Square extends Rectangle

If someone calls the setWidth or setHeight I have a problem. What can I do to protect myself.

finish the intersectingRectangle routine.

What happens if I use these for the Square class. Do they work? Do I want to get back answers that are Rectangle or Square?



public class Rectangle {
    private int height, width;
    private int x, y;
    public Rectangle(int x, int y, int w, int h)
    {
        this.x =x;
        this.y = y;
        width=w;
        height =h;
    }
    public void setWidth(int w)
    {
        width = w;
    }
    public void setHeight(int h)
    {
        height = h;
    }
    public Rectangle()
    {
        this(1,1,1,1);
    }
    public String toString()
    {
        return "Rectangle: width="+width +"height="+height;
    }
    public boolean equals(Object obj)
    {
        if (getClass() != obj.getClass())
            return false;
        Rectangle r=(Rectangle)obj;
        if (x==r.x && y==r.y && width==r.width && height == r.height)
            return true;
        else
            return false;
    }
    public int area()
    {
        return height*width;
    }
    public int perimeter()
    {
        return 2*(width+height);
    }
    Rectangle boundingRectangle(Rectangle other)
    {
        int x_new = Math.min(x, other.x);
        int y_new = Math.min(y, other.y);
        int w_new = Math.max(x+width, other.x+other.width) - x_new;
        int h_new = Math.max(y+height, other.y+other.height) - y_new;
        return new Rectangle(x_new, y_new, w_new, h_new);
    }

    Rectangle intersectingRectangle(Rectangle other)
    {
        int x_new = 1;// Fill in real code
        int y_new = 1;// Fill in real code
        int w_new = 1;// Fill in real code
        int h_new = 1;// Fill in real code
        if (w_new > 0 && h_new > 0)
            return new Rectangle(x_new, y_new, w_new, h_new);
        else
            return null;
    }



    public static void main(String[] args) {
        //Create some code using the above:

    }

}



Finished Programs from the video

TitledEmployee.java



public class TitledEmployee extends SalariedEmployee{

    private String title;

    public String getTitle()
    {
        return title;
    }
    public void setTitle(String t)
    {
        title = t;
    }

    public TitledEmployee(String theName, Date theDate, double theSalary, String title)
    {
        super(theName, theDate, theSalary);
        this.title = title;
    }
    public String getName()
    {
        return title+ " "+ super.getName();
    }


    public static void main(String[] args) {
        TitledEmployee te = new TitledEmployee("Smith", new Date(1,2,2000),
                20000, "Mr.");

        System.out.println(te.toString());

        te.setTitle("Doctor");
        System.out.println(te.toString());


    }

}



Square.java



public class Square extends Rectangle{

    Square(int x, int y, int side)
    {
        super(x, y, side, side);
    }


    public void setWidth(int w)
    {
        super.setWidth(w);
        super.setHeight(w);
    }
    public void setHeight(int h)
    {
        super.setWidth(h);
        super.setHeight(h);
    }

    public String toString()
    {
        return "Square: side="+getWidth();
    }

    public static void main(String[] args) {
        Square s = new Square(1,2, 5);
        Square s2 = new Square(2,4, 8);

        Rectangle bounding = s.boundingRectangle(s2);
        Rectangle intersection = s.intersectingRectangle(s2);

        System.out.println("bounding="+ bounding.toString());
        System.out.println("intersecting="+ intersection.toString());

        System.out.println(s);

        s.setWidth(10);

        System.out.println(s);

        System.out.println("area=" + s.area());

    }

}



Rectangle.java


// The only thing that changed from the video was the following routine

    Rectangle intersectingRectangle(Rectangle other)
    {
         int x_new = Math.max(x, other.x);
         int y_new = Math.max(y, other.y);
         int w_new = Math.min(x+width, other.x+other.width) - x_new;
         int h_new = Math.min(y+height, other.y+other.height) - y_new;
        if (w_new > 0 && h_new > 0)
            return new Rectangle(x_new, y_new, w_new, h_new);
        else
            return null;
    }



Last Modified: October 26, 2013 10:34 AM