3

Here is the code:

public static void main(String args[])
{
    Circle objCircle = new Circle(5);
    Square objSquare = new Square(5);
    Triangle objTriangle = new Triangle(3, 4);
    Sphere objSphere = new Sphere(5);
    Cube objCube = new Cube(5);
    Tetrahedron objTetra = new Tetrahedron(5);
    Object[] Shape = new Object[5];
    Shape[0] = objCircle;
    Shape[1] = objSquare;
    Shape[2] = objTriangle;
    Shape[3] = objSphere;
    Shape[4] = objCube;
    Shape[5] = objTetra;
    for(int i = 0; i<Shape.length; i++)
    {
       //Problem occured in this section of code
        System.out.println(Shape[i].getArea());
    }
}

}

I have six different classes and all of them have getArray() method with definition of their own. I want to print the values that getArray() method returns from different classes using the Shape array.

6
  • You need to cast, or use an interface. Commented Apr 14, 2015 at 15:47
  • i have used abstract class. i have 6 classes. casting works if i cast for every class separately. Commented Apr 14, 2015 at 15:49
  • 3
    @Chandan: Well does your abstract class have a getArea() method? If so, just change your array to be an array of that type rather than Object[]. Commented Apr 14, 2015 at 15:49
  • The first problem I can see is an ArrayIndexOutOfBoundsException. Do not use Object[]. Use Shape[] or List<Shape> where Shape is an interface with a getArea method. Commented Apr 14, 2015 at 15:50
  • 2
    There are several problems with this code: 1. You're initializing the array to size 5 and trying to index 6 elements. This will invariably cause an ArrayIndexOutOfBoundsException. 2. Your array variable has a name starting with a capital letter. This should be reserved for class names, as it can be (and especially in this case is) confusing. 3. You cannot call getArea() like this, because you have declared your array to be of type Object[]. Object doesn't have the method getArea(). Commented Apr 14, 2015 at 15:53

4 Answers 4

1

Define interface like below. Implement this interface in class Circle , Square ..

public interface Shape {

    public Double getArea();
}

Then you can different classes getArea as below.

Shape [] shape = new Shape [5];
    shape[0] = objCircle;
    shape[1] = objSquare;
    shape[2] = objTriangle;
    shape[3] = objSphere;
    shape[4] = objCube;
    shape[5] = objTetra;
    for(int i = 0; i<Shape.length; i++)
    {
       //Problem occured in this section of code
        System.out.println(Shape[i].getArea());
    }
Sign up to request clarification or add additional context in comments.

2 Comments

Why use a Double instead of primitive double??
its your choice I used it just for example :)
1

Use an interface or an abstract class and extend all different shapes from it.

public abstract class Shape{
    public abstract SomeType getArray();
}

And use

Shape[] Shapes

instead of Object[].

Comments

0

First of all you should avoid to use an Object[] What will happened if you do so?

 ...
shape[6] = Integer.parseInt(1);
for(int i = 0; i<shape.length; i++)
{
   //Oups a problem will occured here
    System.out.println(shape[i].getArea());
}

By using a Object[] you are not using the Java type safety. Every instance inherits from Object in Java which means that you can put everything in your Array.

I see that you put a variable with capitalize. It's break the Java convention capitalize is for Class, Enum, Interface. Maybe I am too orthodox :)

You can do the following:

public abstract Shape {
 public abstract double getArea();
}

public Circle extends Shape {
  public abstract double getArea(){
   //implementation here
  }
}

etc...

Depends of your use case. An Interface could be much more appropriate due to inheritance trade-offs in some case.

And use a List of Shape. You will ensure that you have the same type when you will call List.add method. Furthermore you can use a foreach loop

1 Comment

I used for each loop initially but i got this error: Can only iterate over an array or an instance of java.lang.Iterable.
0

Thank you everyone i got it correct and i am posting my answer that i solved with your help

public static void main(String args[])
{

    Circle objCircle = new Circle(5);
    Square objSquare = new Square(5);
    Triangle objTriangle = new Triangle(3, 4);
    Sphere objSphere = new Sphere(5);
    Cube objCube = new Cube(5);
    Tetrahedron objTetra = new Tetrahedron(5);
    Shape[] a = new Shape[6];

    a[0] = objCircle;
    a[1] = objSquare;
    a[2] = objTriangle;
    a[3] = objSphere;
    a[4] = objCube;
    a[5] = objTetra;
    for(int i = 0; i<a.length; i++)
    {
        a[i].getArea();
    }


}

}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.