first off I tried researching my problem but I have no idea how to word my question ... so I am not sure if there is a question out there that solves my problem and also not sure if this is the best wording for my question either.
So, I have a Superclass Shape
public abstract class Shape {
protected String name;
protected String type;
public Shape(){
name = "";
type = "";
}
public void print (){
System.out.printf("Name = %s, Type = %s", name, type);
}
}
and a Subclass 2D
public abstract class TwoDimensionalShape extends Shape{
protected double length;
protected double area;
public TwoDimensionalShape(double length){
if (length<0.0)
throw new IllegalArgumentException("ERROR: POSITIVE NUMBER REQUIRED");
this.length = length;
type = "Two Dimensional Shape";
}
public abstract void getArea();
@Override
public void print(){
System.out.printf("Name = %s, Type = %s, Length of side = %d, Area = %d",
name, type, length, area);
}
}
along with several smaller subclasses that extend off 2D (and another almost identical class 3D). My problem is with the test code, it doesn't calculate area. Class Test code
Circle S1 = new Circle(2.5);
etc.
shapesArray[0] = S1;
etc.
for(Shape CS : shapesArray){
CS.getArea();
if(CS.Type == "Three Dimensional Shape"){
CS.getVolume();
}
CS.print();
System.out.println(" ");
}
}
I removed the getArea and getVolume methods and the print statement worked fine. Which lead me to think there is a problem with the way each subclass interacts with the superclass, however, the subclass print methods override and return the correct values (except for area :( )
With the area and volume commands, the code doesn't compile and I get this error
ShapeTest.java:25: error: cannot find symbol CS.getArea();
three times.
Here is one of the subclasses, in case it holds important info needed for a solution.
public class Circle extends TwoDimensionalShape {
public Circle(double length){
super(length);
name = "Circle";
}
@Override
public void getArea(){
area = Math.PI * length * length;
}
@Override
public void print(){
System.out.printf("Name = %s, Type = %s, Radius = %f, Area = %f",
name, type, length, area);
}
}
I am not experienced enough to understand the problem entirely and I have been changing loops, location of variables and methods in the classes but I have not made progress. I thank you for reading this long question and id appreciate any help you can offer.