Reputation: 65
I have an interface named shape with just a single method that calculates the area of the shape. I then have two classes Shape and Rectangle that implement the shape interface. All of the classes have the appropriate setters, getters, and calcArea method. In main, I have an ArrayList that will store 2 rectangles and two circles. How will I add the rectangles and circles to the array? I have tried to do shapeArr.
and my methods from Rectangle and Circle don't appear. My code is below and any help is appreciated. Thanks!
public interface Shape {
public double calcArea();
}
public class Circle implements Shape {
private int radius;
private double area;
public Circle() {
radius = 1;
}
public void setRadius(int r){
radius = r;
}
public int getRadius(){
return radius;
}
@Override
public double calcArea() {
area = (Math.PI * (Math.pow(radius, 2)));
return area;
}
}
public class Rectangle implements Shape{
private int height;
private int width;
public int area;
public Rectangle(){
height = 1;
width = 1;
}
public void setHeight(int h){
height = h;
}
public void setWidth(int w){
width = w;
}
public int getHeight(){
return height;
}
public int getWidth(){
return width;
}
@Override
public double calcArea() {
area = (height * width);
return area;
}
}
import java.util.ArrayList;
public class ShapeDemo {
public static void main(String[] args) {
ArrayList<Shape> shapeArr = new ArrayList<Shape>(4);
}
public static void displayArea(Shape s) {
System.out.println("The area of the shape is " + s.calcArea());
}
}
Upvotes: 1
Views: 4796
Reputation: 5136
Since both classes implement the same interface you should have no problem to add these objects to the ArrayList just by calling the add method. After you can call your displayArea method for all the elements in the ArrayList to check their area.
public static void main(String[] args) {
ArrayList<Shape> shapeArr = new ArrayList<Shape>(4);
shapeArr.add(new Circle());
shapeArr.add(new Rectangle());
for (Shape s : shapeArr) {
displayArea(s);
}
}
Upvotes: 0
Reputation: 521053
You seem to be struggling with trying to find a method from a Shape
type belonging to a List
. However, you should be looking for a method in List
which would allow to add a Shape
to the collection. The method you are looking for is List.add()
:
public static void main(String[] args) {
ArrayList<Shape> shapeArr = new ArrayList<Shape>(4);
Circle c = new Circle();
c.setRadius(10);
Rectangle r = new Rectangle();
r.setHeight(10);
r.setWidth(5);
shapeArr.add(c);
shapeArr.add(r);
}
Now if you want to compute the area of each Shape
in your List
, you need only iterate over the collection and call the calcArea()
method:
for (Shape s : shapeArr) {
System.out.printf("Found a shape with area %.2f", s.calcArea());
}
Upvotes: 2
Reputation: 1541
You will need to cast the item from the ArrayList back to the correct type to get the extra fields.
(Circle)shareArr.get(1)
for example.
You can use the instanceof
operator to find out what type it is.
for(Shape shape: shapeArr)
{
if(shape instanceof Circle)
{
(Circle)shape.circleMethod();
}
//same for Rectangle
}
}
Upvotes: 1