Question: When designing your Square and Circle classes from last week, it would have been useful to have a template to start from, rather than starting

When designing your Square and Circle classes from last week, it would have been useful to have a template to start from, rather than starting from scratch. Lets define such a template here, a class that will contain data items common to all Shapes (such as an X, Y, or a Color all shapes have these elements). Well include a few methods that wont be used by this labDriver, but dont worry...these may prove useful in the future. Well be satisfied here with designing yet another class, even if some of the methods are just stubs for now. For our Paint program as it stands currently, all shapes will have the following data items and methods...
1.1 Class Constraints
Declare 4 class constraints for the Shape class as comments and submit them.
What might be a good constraints? Think about how we might use this Shape...
Perhaps x and y must be nonnegative integers?
1.2 Data Fields
Declare an x and a y.(all data here is private)
Declare a Color object.
Color Class Information
When you want to draw a shape on the screen, how do you specify its color? You can specify the color of a shape using the class java.awt.Color. To use it, first import the class at the top of your file.
import java.awt.Color;
This class defines several color constants, such as Color.RED and Color.BLUE. You can assign those constants to Color variables.
Color red = Color.RED;
Color blue = Color.BLUE;
Although we aren't going to draw our shapes on the screen yet, we'll give them a color field. When you construct a shape, you can pass a Color constant to the constructor.
Shape shape = new Shape(0,0, Color.CYAN);
1.3 Constructors
public Shape();
A constructor with no parameters. You can choose what values the fields should have by default.
public Shape(int x, int y, Color color);
A constructor with parameters for the x and y coordinates and the color.
public Shape(Shape other);
A constructor with the parameter for a Shape Object that will create a new exact copy.
1.4 Instance Methods
Getters and Setters for X and Y.
public String toString();
Returns a string that describes the x and y coordinates and the color of this shape.
For example, a blue shape at (10,200) should return "Shape (10,200) blue"
public double getArea();
Since we can't compute the area of the shape without knowing what kind of shape it is, this method should just return -1.
Subclasses will override this method to return a useful value.
public void draw(Graphics g);
This method takes a Graphics object as an argument.
Later, we will learn how to use the Graphics object to draw shapes, but for now, the body of the method will be empty
Sample Output
Uncomment and run the shapeDriver() method in ClassDesignIIDriver.java
a: Shape (120,0) and color:null and area: -1
b: Shape (10,140) and color:java.awt.Color[r=64,g=64,b=64] and area: -1
c: Shape (10,10) and color:java.awt.Color[r=64,g=64,b=64] and area: -1

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!