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...
Class Constraints
Declare 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?
Data Fields
Declare an x and a yall 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 Color.CYAN;
Constructors
public Shape;
A constructor with no parameters. You can choose what values the fields should have by default.
public Shapeint x int y Color color;
A constructor with parameters for the x and y coordinates and the color.
public ShapeShape other;
A constructor with the parameter for a Shape Object that will create a new exact copy.
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 should return "Shape 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
Subclasses will override this method to return a useful value.
public void drawGraphics 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 and color:null and area:
b: Shape and color:java.awt.Colorrgb and area:
c: Shape and color:java.awt.Colorrgb and area:
Step by Step Solution
There are 3 Steps involved in it
1 Expert Approved Answer
Step: 1 Unlock
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
