1

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.

Liam.C
  • 15
  • 8
  • And hint: you have a bug in there ... http://stackoverflow.com/questions/513832/how-do-i-compare-strings-in-java ... do nut use == ... it simply looks like you are overburdening yourself right now. You should probably step back and focus more on the real basics at this point. – GhostCat Apr 15 '17 at 06:35

2 Answers2

0

Your type Shape doesn't declare that method.

The compiler doesn't know that you intend to put TwoDimensionalShape objects into that array. It only see that you said: this array contains Shapes; and shapes do no have those other two methods!

So you could do:

  • declare that array to contain only TwoDimensionalShape objects. Of course, then you can't add 3D
  • use if (thing is instanceof TwoDimensionalShape) { and then cast to that type
  • And then: you dont need a string type. All objects have a class; and that class already defines its exact type. That is why you use instanceof to determine types; not by adding a string field and comparing that string (the wrong way with ==) to other strings!
GhostCat
  • 137,827
  • 25
  • 176
  • 248
0

Your super most class Shape should have all the methods you want to access through polymorphic feature of java. One would assign a sub-type instance of to a supertype variable to handle all possible subtype classes in a uniform fashion, e.g. using methods declared (but possibly overriden) by the supertype class.

I have made minor changes to your classes.

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);
    }

    public abstract void getArea();

    public abstract void getVolume();

}
// ----------------
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";
    }

    @Override
    public void print() {
        System.out.printf(
                "Name = %s, Type = %s, Length of side = %d, Area = %d", name,
                type, length, area);
    }
}
//------------------
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);
    }

    @Override
    public void getVolume() {
        System.out.println("Vaolume method invoked");
    }
}
//------------------
public class Dim {

    public static void main(String[] args) {
        Shape[] shapesArray = new Shape[10];
        Circle S1 = new Circle(2.5);
        shapesArray[0] = S1;
        for (Shape CS : shapesArray) {
            if (CS != null) {
                CS.getArea();
                if (CS.type.equals("Three Dimensional Shape")) {
                    CS.getVolume();
                }
                CS.print();
                System.out.println(" ");
            }
        }
    }
}
Ganesh S
  • 510
  • 11
  • 19
  • Hey Narayana, thank you for posting this answer, I was unsure from the previous comment how to word this if (CS.type.equals("Three Dimensional Shape")) { CS.getVolume(); } thanks for helping – Liam.C Apr 15 '17 at 07:10