Reputation: 11
I am writing this code:
We focus on three geometric shapes: Circle, Rectangle and Square. Each one of these shapes is a geometric shape, hence, these geometric shapes can have a parent class (geometricShape) that includes common features of these shapes.
At the same time, every geometric shape is a printable and comparable object, among other objects that can also be printable and comparable, hence, these shapes will inherit the printability and comparability from two interfaces: Printable and Comparable.
Finally, as the Square is a special case of the Rectangle, the Square will be extended from the Rectangle in our design.
My problem is getting the square to accept only one input value (The side) while it is supposed to be inheriting the properties of the rectangle (that accepts 2 input values, width and height)
I've tried using a no argument constructor and a super constructor in the Rectangle class but neither worked to solve this problem.
Is it possible that the problem has something to do with something other than the constructors?
code of rectangle class:
class Rectangle extends geometricShape {
private double width, height;
private double side;
Rectangle(){
super();
}
Rectangle (double side){
this.side=side;
}
Rectangle(double width, double height) {
super.setShapeName("Rectangle");
this.width = width;
this.height = height;
}
public void setSide(double side) {
this.side = side;
}
public double getSide() {
return side;
}
@Override
public double gerPerimeter() {
return 2 * (width + height);
}
@Override
public double getArea() {
return width * height;
}
public double getWidth() {
return width;
}
public void setWidth(double width) {
this.width = width;
}
public double getHeight() {
return height;
}
public void setHeight(double height) {
this.height = height;
}
}
code of square:
class Square extends Rectangle {
double side;
Square(double side) {
super.setShapeName("Square");
this.side = side;
}
public double getside() {
return side;
}
public void setside(double s) {
side = s;
}
}
This is the rest of the code (without the square and the rectangle)
public class Inheritance {
public static void main(String[] args) {
int Select;
double Value1, Value2;
String s;
geometricShape Shapes[] = new geometricShape[2];
Scanner input = new Scanner(System.in);
for (int i = 0; i < 2; i++) {
s = (i < 1) ? "first " : "second ";
System.out.println("Choose the " + s + "shape type:\n" + "(1) for a circle\n" + "(2) for a rectangle\n" + "(*) for a square"); // "*" means any other number
Select = input.nextInt();
switch (Select) {
case 1:
System.out.print("Enter the radius: ");
Value1 = input.nextDouble();
Shapes[i] = new Circle(Value1);
break;
case 2:
System.out.print("Enter the width: ");
Value1 = input.nextDouble();
System.out.print("Enter the height: ");
Value2 = input.nextDouble();
Shapes[i] = new Rectangle(Value1, Value2);
break;
default:
System.out.print("Enter the side length: ");
Value1 = input.nextDouble();
Shapes[i] = new Square(Value1);
break;
}
}
System.out.println("The larger shape is:");
if (Shapes[0].isLargerThan(Shapes[1])) {
Shapes[0].print();
} else {
Shapes[1].print();
}
}
public Inheritance() {
}
}
interface Comparable {
public boolean isLargerThan(E obj); // The method returns true if the self object is larger than obj }
interface Printable {
public void print();
/* For simplicity, let this method prints "A " + object name + "with area: " + shape area */ }
abstract class geometricShape implements Comparable<geometricShape>, Printable {
private String shapeName;
abstract public double gerPerimeter();
abstract public double getArea();
public String getShapeName() {
return shapeName;
}
public void setShapeName(String Name) {
shapeName = Name;
}
@Override
public boolean isLargerThan(geometricShape obj) {
return this.getArea() > obj.getArea();
}
@Override
public void print() {
System.out.println("A " + this.getShapeName()
+ " with area: " + this.getArea());
}
}
/*part 1*/
class Circle extends geometricShape {
private double radius;
Circle(double newRadius) {
super.setShapeName("Circle");
radius = newRadius;
}
@Override
public double gerPerimeter() {
return 2 * 3.14 * radius;
}
@Override
public double getArea() {
return 3.14 * radius * radius;
}
public void setradius(double r) {
radius = r;
}
public double getradius() {
return radius;
}}
Upvotes: 1
Views: 108
Reputation: 393846
The described issue with Square and Rectangle can be easily handled by passing a single (side) argument to the Square
constructor and passing that argument twice (as both width and height) to the Rectangle
constructor:
public class Square extends Rectangle {
...
public Square (double side) {
super (side, side);
}
...
public double getSide () {
return getWidth (); // a Rectangle method
}
public void setside(double s) {
setWidth(s); // a Rectangle method
setHeight(s); // a Rectangle method
}
...
}
Your Square
class doesn't require the side
instance variable. And your Rectangle
class also doesn't require it (it has width
and height
).
Note, though, that this doesn't prevent the user of your class from setting the width and height of a Square
to different values. In order to prevent that, you can override the setWidth()
and setHeight()
methods of the Rectangle
class in the Square
class. The behavior can be as follows: whenever you change the width of a Square
(by calling setWidth()
), its height is changed to the same value, and vice versa.
public class Square extends Rectangle {
...
public Square (double side) {
super (side, side);
}
...
public double getSide () {
return getWidth (); // a Rectangle method
}
public void setside(double s) {
super.setWidth(s);
super.setHeight(s);
}
@Override
public void setWidth(double w) {
setSide(w);
}
@Override
public void setHeight(double h) {
setSide(h);
}
...
}
P.S. this answer doesn't explain the exception you mentioned, simply because you didn't include the code that produces that exception.
Upvotes: 1
Reputation: 1274
You can design your classes like
geometricShape
class having abstract method like area()
,perimeter()
Sub classs for example Rectangle
will have width, length fields and will override area()
and perimeter()
Upvotes: 0