0
public abstract class ShapeClass {
    private double area;

    CONSTRUCTORS
    MUTATORS, ACCESSORS

    public abstract double calcArea();
}

public class CircleClass extends ShapeClass {
    private int diameter;
    private double area;

    public CircleClass() {
        super();
        diameter = 10;
    }

    public CircleClass(CircleClass inCircle) {
        super(inCircle);
        diameter = inCircle.getDiameter();
    }

    public CircleClass(int inDiameter) {
        setDiameter(inDiameter);
        area = calcArea();
        super.setArea(area);
    }

    public void setDiameter(int inDiameter) {
        if(validateInt(inDiameter)) {
            diameter = inDiameter;
        } else {
            throw new IllegalArgumentException("invalid diameter");
        }
    }

    public int getDiameter() {
        return diameter;
    }

    public boolean equals(int inDiameter) {
        return(diameter == inDiameter);
    }

    public boolean equals(Object inObj) {
        boolean same = false;
        if(inObj instanceof CircleClass) {
            CircleClass inCircle = (CircleClass)inObj;
            if(super.equals(inCircle)) {
                if(diameter == inCircle.getDiameter()) {
                    same = true;
                }
            }
        }

        return same;
    }

    public String toString() {
        return (" area of circle is: " + super.toString());
    }

    private boolean validateInt(int inDiameter) {
        boolean valid = false;
        if (inDiameter>0) {
            valid = true;
        }
        return valid;
    }

    private boolean validateReal(double inArea) {
        boolean valid = false;
        if(inArea>0.0) {
            valid = true;
        }
        return valid;
    }

    @Override
    public double calcArea() {
        double radius;
        radius = ((double) diameter) / 2.0;
        area = Math.PI * radius * radius;
        return area;
    }
}

This is my code for a ShapeClass. I have two other classes Rectangle and Triangle, they're pretty much the same as the CircleClass.

In another class i'm assigning the ShapeClass objects in an array.

if I do that it'll be something like shape[3] = {Shape Object,Shape Object,Shape Object}. I don't know if that's right, I'm new to java. Sorry if there's any confusion.

My question is if I do that how do I distinguish what object is Circle, Rectangle or Triangle? When I want to print out a circle object only?

Thanks for the help.

f-CJ
  • 4,235
  • 2
  • 30
  • 28
ch1234
  • 17
  • 2
  • 3
    1) Don't name your classes with the suffix `Class`. `Circle` will do just fine. 2) Read https://stackoverflow.com/questions/7313559/what-is-the-instanceof-operator-used-for 3) If you need to do this, you might should reconsider your model. – bradimus May 26 '17 at 17:06

4 Answers4

0

You can check by using instanceof :

if(shape[0] instanceof Circle){
 // do something
}
FilipRistic
  • 2,661
  • 4
  • 22
  • 31
0

So there is an operator in java - instance of:

if(shapeObject instanceof Circle){
  //print
}

so you can use it to distinguish objects by type. Also as for your question whether it's correct: You can use this approach with creating array of parent object type and putting children in it. After that, if you call toString method on each object from that array specific implementation of that method will be invoked. For example if there is Circle object in this array and there is overridden toString method in it then after calling toString on object from array of ShapeObject specific implementations will be invoked.

Alex
  • 1,940
  • 2
  • 18
  • 36
0

You have 2 options:

// Solution 1: prits out all instances of Circle, basically also all subclasses of Circle
for (ShapeClass shape : shapes) {
    if (shape instanceof CircleClass)
        System.out.println(shape.toString());
}

//  Solution 2: Matches exact class
for (ShapeClass shape : shapes) {
    if (shape.getClass().equals(CircleClass.class))
        System.out.println(shape.toString());
}

The above solutions will solve the task you asked about. But maybe the information below will be userful for you:

What if you want to print out the names of each shape, how to distingush them in this case?

Let's say we have 3 shapes:

public class Shape {
   public void print() {
       System.out.println("Shape is printed");
   }
}

public class Triangle extends Shape {
   @Override
   public void print() {
       System.out.println("Triangle is printed");
   }
}

public class Circle extends Shape {
   @Override
   public void print() {
       System.out.println("Circle is printed");
   }
}

This code will print exactly what you need, because you defined the same function for all of the shapes, overriding it in child classes, and the appropriate function will be called based on object type determined at the runtime:

for (Shape shape : shapes) {
    shape.print();
}
J-Alex
  • 6,881
  • 10
  • 46
  • 64
  • i cant quite understand the comment. so if i use the first solution does that print out the string in the super class only or string in the circle class as well? – ch1234 May 26 '17 at 17:19
  • It means that if you have ChildCircleClass which extends CircleClass and the objects of this type are also stored in this array - they are also will be recongized as instanceof CircleClass – J-Alex May 26 '17 at 17:21
  • Maybe you would like solve other task, like appropriate printing for each shape? because it has another solution. You should read what polymorphism is. – J-Alex May 26 '17 at 17:30
  • thanks so much for the solution this was exactly what i was looking for. just another quick question. so if we assume toString() in the parent class was "area is 10". if i write ("Circle is printed" + super.toString()) then itll will print "Circle is printed area is 10"? – ch1234 May 26 '17 at 18:07
  • It should be "Circle is printedarea is 10" - don't forget whitespaces :) And don't forget to press 'accept' button on the answer that exactly answered your question – J-Alex May 26 '17 at 18:26
0

Try like this,

for(int i = 0; i < shapeArray.length; i++){
    if(shapeArray[i] instanceof CircleClass){
          // print circle here
     }
 }
Kushan
  • 10,657
  • 4
  • 37
  • 41