I know there are similar to posts to this but none of them exactly fit I was trying to do. I am a newer programmer and just started reading up on the super class for Java. I have created a super class named shape. I then created a subclass named circle. I am stumped on two parts here, calling the toString method in my super class from my subclass toString method as well as one of my constructors from my subclass to my superclass. I have provided my super class, subclass and a main test method below.
The Shape class:
public class Shape
{
private boolean isFilled;
private String color;
public Shape()
{
this.isFilled = true;
this.color = "Green";
}
public Shape(boolean x, String y)
{
this.isFilled = x;
this.color = y;
}
public void setFilled(boolean fill)
{
this.isFilled = fill;
}
public boolean getFilled()
{
return this.isFilled;
}
public void setColor(String x)
{
this.color = x;
}
public String getColor()
{
return this.color;
}
@Override
public String toString()
{
String s = "Filled:" + this.isFilled + "\n";
String t = "Color: " + this.color;
String u = s + t;
return u;
}
}
The Circle:
public class Circle extends Shape
{
private double radius;
public Circle()
{
this.radius = 1;
}
public Circle(double x)
{
this.radius = x;
}
public Circle(double radius, boolean isFilled, String Color)
{
super(isFilled, Color);
this.radius = radius;
}
public void setRadius(double radius)
{
this.radius = radius;
}
public double setRadius()
{
return this.radius;
}
public double getArea()
{
double area = this.radius * this.radius * Math.PI;
return area;
}
@Override
public String toString()
{
String x = "Radius: " + this.radius +"\n";
String y = "Area: " + getArea() + "\n";
String z = x + y;
return z;
}
}
A TestShape:
public class TestShape
{
public static void main(String[] args)
{
Circle c1 = new Circle(2.67);
System.out.println("c1: ");
System.out.println(c1.toString());
System.out.println();
Circle c2 = new Circle(3, false, "Red");
System.out.println("c2: ");
System.out.println(c2.toString());
System.out.println();
}
}
Expected output:
c1:
Radius: 2.67
Area: 22.396099868176275
Filled: true
Color: Green
c2:
Radius: 3.0
Area: 28.274333882308138
Filled: false
Color: Red
Actual output:
c1:
Radius: 2.67
Area: 22.396099868176275
c2:
Radius: 3.0
Area: 28.274333882308138