Couldn't find a relevant explanation online I have homework to do I should create a class called Shapes and 2 more classes one for rectangle and the second is for a circle. I wanted to know what is the best way to arrange constructors, data members and methods because for example for a rectangle I should have height and width and the circle has a radius.
public class Shapes {
//Should I use only common Data members, constructors and functions in the base class?
private int x;
private int y;
private int width;
private int height;
private String color;
private double radius;
also how do I create the relevant constructors using super()? I think I got it all mixed up:
//Constructors:
public Shapes() {
}
//Common constructor
public Shapes(int x, int y, String color) {
setX(x);
setY(y);
setColor(color);
}
//Circle constructor:
public Shapes(int x, int y, String color, double radius) {
this(x, y, color);
setRadius(radius);
}
//Rectangle constructor:
public Shapes(int x, int y, int width, int height, String color) {
this(x, y, color);
setWidth(width);
setHeight(height);
}
in the rectangle class it looks like this:
public Rectangle() {
super();
}
public Rectangle(int x, int y, int width, int height, String color) {
super(x, y, width, height, color);
}
and in the circle class I did it like that:
public Circle() {
super();
}
public Circle(int x, int y, String color, double radius) {
super(x, y, color, radius);
}
I need a print method to print all info from each class that is relevant to the class is there any way using this print method in the base (shapes) class to avoid multiple print methods?
There are different parameters to show but we've been told to avoid multiplication of the code.