CS401 Lab 6: Extending Classes


Introduction

As we discussed in lecture, it's often easier or necessary to utilize the properties and behavior from a previously defined class in a new class, which then only needs to add the extra properties required for the task. We learned that there are two ways of accomplishing this: composition and inheritance. Composition involves including an instance of the existing class in our own class and using its public interface in our instance methods when needed. Inheritance lets us directly including the existing class's public interface as part of our own while overriding existing methods to perform a new task in addition to adding new instance methods and variables. We saw that this involves using the extends keyword and saw how the public, private , and protected keywords affect a subclass's ability to access the inherited properties. Lastly, we examined the set of methods all classes inherit from the Object class: toString(), equals(), and hashCode(). In this lab, you'll write some simple extensions to a class using good inheritance design.


MyRectangle

In Lab 5, you wrote a simple MyRectangle Class to represent a rectangle drawn on a computer screen at a certain position with a certain width and height. For this class, we need to define what it means for two rectangles to be logically "equal." We could write some new instance method to perform this check for us, but since the equals() method is inherited from Object, we'll just use that. If you choose to, make a new copy of your MyRectangle.java file from Lab 5. Edit this class to include an equals() method that returns true if and only if the argument MyRectangle object has the same width and height as the current one (we'll ignore the position of the rectangle when determining equality). Be maintain the equivalent relation that we discussed in lecture.

MyColoredRectangle

We now want to create a class that represents a rectangle that also has a color property, which will be the color in which the rectangle should be drawn on the "screen." The Java API already defines a Color class for our use, which will import into our classes. We'll learn more about this when we start discussing graphical programs. For now add an import statement to your class including the java.awt.Color class. For convenience, the class defines several public static constants that define common colors, accessed as any other static property: Color.black, Color.white, Color.red, Color.blue, etc. The class also overrides the equals() method to determine whether two colors are equal (the same color) or not.

Clearly, a MyColoredRectangle IS NOT A Color, but it does HAVE A Color; so the relationship between these two classes strictly falls into composition. The relationship between MyColoredRectangle and MyRectangle, though, seems like a nice application for inheritance. In fact, you're going to implement both in this lab.

MyColoredRectangle1

This first implementation should use the extends keyword to inherit from MyRectangle. Implement the following methods:

Constructors
MyColoredRectangle1()	//Default Constructor
MyColoredRectangle1(int startx, int starty, int width, int height, Color color) 
Accessors
public Color getColor()			//return the color of the MyColoredRectangle1
public String toString()		//returns the string representation of a MyColoredRectangle1
					//see NOTE below
public boolean equals(Object o)		//returns true if and only if the argument Rectangle has the
					//same width, height, and Color as 'this' MyColoredRectangle1 
Mutators
public void setColor(Color color) 	//change the color of this MyColoredRectangle1

Notice here that you need not override all the methods of the MyRectangle class, which saves you a little work.

MyColoredRectangle2

The second implementation should include an instance of MyRectangle as a instance variable WITHOUT using the extends keyword. Implement the following methods:

Constructors
MyColoredRectangle2()			//Default Constructor
MyColoredRectangle2(int startx, int starty, int width, int height, Color color)
Accessors
public int area() 			//return the area of the given MyColoredRectangle2
public String toString() 		//return the string representation of the MyColoredRectangle2
					//See NOTE Below 
public boolean isInside(int X, int Y)	//Return true if point X, Y is inside the
					//MyColoredRectangle2, and false otherwise
public boolean equals(Object o)		//returns true if and only if the argument MyColoredRectangle2
					//has the same width, height, and Color as 'this' 
					//MyColoredRectangle2
public Color getColor()			//return the color of the MyColoredRectangle2
Mutators
public void setSize(int newWidth, int newHeight)//change width and height of
						//this MyColoredRectangle2 to the values passed in

public void setPosition(int newX, int newY)	//change X and Y position of this
						//MyColoredRectangle2 to the values passed in
public void setColor(Color color)		//change the color of the MyRectangle2

NOTE: The Color class does not provide a decent string representation with the class. For convenience, the Lab6.java file below provides a simple method, forColor(Color c), that returns a string for each of the static colors. For this to work, though, none of your classes can create a copy of its color arguments for the constructor or setColor() methods. Since the Color class is immutable, this isn't too much of a problem.


Main Program

Once you've implemented the above interface, download and compile the main program below with it in the same folder as your other classes. Running it should then produce output similar to the text file below.

Lab6.java
lab6out.txt