Class with array of objects, methods on objects without inheritance?

I created three different classes of shapes, and the a separate class called "Shapes." I want Shapes to hold and array of the different shape objects I create. I'm able add each shape to this array through a method I created in shapes, but once I do that, I can no longer perform methods that are within these individual shapes. Is there a way to do this without the use of inheritance? I want to be able to use the stats method I have for each shape after they have been put into this array in the Shapes class. Since they are being held in an array of type Object, would I have to typecast them back to Triangle, Rectangle, etc?
[code]
class shapesOO {
public static void main(String args[]) {
Shapes shapes = new Shapes();

Triangle triangle0 = new Triangle(5,6,6);
shapes.add(0, triangle0);
Triangle triangle1 = new Triangle(3,3,2);
shapes.add(1, triangle1);
Triangle triangle2 = new Triangle(5,3,1);
shapes.add(2, triangle1);

Rectangle rectangle0 = new Rectangle(1,4);
shapes.add(3, rectangle0);
Rectangle rectangle1 = new Rectangle(2,3.3);
shapes.add(4, rectangle1);
Rectangle rectangle2 = new Rectangle(4,7);
shapes.add(5, rectangle2);

Circle circle0 = new Circle(3);
shapes.add(6, circle0);
Circle circle1 = new Circle(3.14);
shapes.add(7, circle1);
Circle circle2 = new Circle(1.56);
shapes.add(8, circle2);

//shapes.newShapes[0].stats(); method is no longer found
shapes.printShapes();
rectangle0.stats();
//System.out.println(triangle0.sideA);


System.out.println("Number of shapes in Shapes class: " + shapes.numberOfShapes());


}
}

class Shapes {
int i;
Object shape;

Object[] newShapes = new Object[100];


public int numberOfShapes() {
int count;
count = 0;

for (i = 0; i < 100 ; i++) {
if (newShapes[i] != null) {
count = count + 1;
}
}
return count;
}

public void add(int j, Object shape) {
newShapes[j] = shape;
}

public Object get(int j) {
return newShapes[j];
}
public void printShapes() {
//System.out.println(newShapes[0]);
//newShapes[0].stats();
for (i = 0; i < 9; i++) {
System.out.println(newShapes[i]);
//newShapes[i].stats();
}
}

/*public void sort() {
Object temp;

for (int i=1; i < 9; i++) {
if (newShapes[0].area > newShapes[i].area) {
temp = newShapes[0];
newShapes[0] = newShapes[i];
newShapes[i] = temp;
}
}
}*/
}

class Rectangle {
double sideA;
double sideB;
double area;

public Rectangle() {
this.sideA = 1;
this.sideB = 1;
}

public Rectangle(double sideA, double sideB) {
this.sideA = sideA;
this.sideB = sideB;
area = sideA * sideB;
}

public void stats() {
System.out.println("Rectangle " + "sides: " + sideA + ", " + sideB + " " + "area: " + area + "
");
}
}

class Triangle {
double sideA;
double sideB;
double sideC;
double s;
double area;
String shape = "Triangle";

public Triangle() {
this.sideA = 1;
this.sideB = 1;
this.sideC = 1;
}

public Triangle(double sideA, double sideB, double sideC) {
this.sideA = sideA;
this.sideB = sideB;
this.sideC = sideC;
s = (sideA + sideB + sideC)/2;
area = Math.sqrt(s*((s - sideA) * (s - sideB) * (s - sideC)));
}

public void stats() {
System.out.println("Triangle " + "sides: " + sideA + ", " + sideB + ", " + sideC + " " + "area: " + area + "
");
}
}

class Circle {
double sideA;
double area;
String shape = "Circle ";

public Circle() {
this.sideA = 1;
}

public Circle(double sideA) {
this.sideA = sideA;
area = Math.PI * Math.pow(sideA, 2);
}

public void stats() {
System.out.println("Circle " + "side: " + sideA + " " + "area: " + area + "
");
}
}
[/code]
Current Output:
Triangle@9304b1
Triangle@190d11
Triangle@190d11
Rectangle@a90653
Rectangle@dec6ced
Rectangle@c17164
Circle@1fb8ee3
Circle@1de33
Circle@14318bb
Rectangle sides: 1.0, 4.0 area: 4.0
Number of shapes in Shapes class: 9

Comments

  • Hey,
    Your thoughts are correct. Following code in Shapes printShapes() shows how it is done
    [code] for (i = 0; i < 9; i++) {
    System.out.println(newShapes[i].getClass());
    }
    for (Object obj : newShapes) {
    if (obj instanceof Triangle) {
    ((Triangle) obj).stats();
    }
    }[/code]
    regards, se52
  • Thank you for your response! This works, too, right?

    [code]
    public void print() {
    for (int i = 0; i < newShapes.length; i++) {
    if (newShapes[i] instanceof Circle) {
    ((Circle)newShapes[i]).print();
    } else if (newShapes[i] instanceof Rectangle) {
    ((Rectangle)newShapes[i]).print();
    } else if (newShapes[i] instanceof Triangle) {
    ((Triangle)newShapes[i]).print();
    }
    }
    [/code]
  • Hey,
    Often there are several ways to accomplish a task. If it works is determined by it doing what you want it to do.

    regards, se52
Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories