Home > database >  How To Use Methods With Array In Java?
How To Use Methods With Array In Java?

Time:04-12

My first question here so please point out my mistakes. And to be honest I couldn't find any similar questions.

So I am trying to write something very basic and I really can't figure why I can't call a method from my Util Class in my Main class.

Util class


    private double accumulatedArea;
    private double accumulatedCircumference;


    public static double getAccumulatedArea(Shape[] shapes) {
        double accumulatedArea = 0;
        for (Shape s : shapes) {
            accumulatedArea = accumulatedArea   s.area();
            return accumulatedArea;
        }
        return accumulatedArea;
    }


    public static double getAccumulatedCircumference(Shape[] shapes) {
        double accumulatedCircumference = 0;
        for (Shape q : shapes) {
            accumulatedCircumference = accumulatedCircumference   q.circumference();
            return accumulatedCircumference;
        }
        return accumulatedCircumference;
    }

    public String toString() {
        return "Number: ";
     }

}

and Main class


    public static void main(String[] args) {

        Shape[] shapes = new Shape[4];

        shapes[0] = new Circle(100);
        shapes[1] = new Circle(23);

        shapes[2] = new Rectangle(10, 20);
        shapes[3] = new Rectangle(8, 12);

        System.out.println("Area of the circle 1: "   shapes[0].area()   " and its circumference is: "   shapes[0].circumference());
        System.out.println("Area of the circle 2: "   shapes[1].area()   " and its circumference is: "   shapes[1].circumference());

        System.out.println("Area of the rectangle 1: "   shapes[2].area()   " and its circumference is: "   shapes[2].circumference());
        System.out.println("Area of the rectangle 2: "   shapes[3].area()   " and its circumference is: "   shapes[3].circumference());
        //Small test
        System.out.println("Number of shapes are in the system: "   shapes.length);

        //????? What to do here??
        for (int p = 0; p < shapes.length; p  ) {
            Shape[p].getAccumulatedArea();
        }


    }
}

How can I call my getAccumulatedArea and getAccumulatedCircumference methods??

Been searching for couple of days now and I really don't have any idea how.

Just in case there is also 2 Rectangle and Circle classes that I use. They are very similar so I am only putting one.

    private double radius;
    private double circumference;
    private double area;
    private ArrayList<Circle> circles;


    //throws Exception gives error! learn how to do exceptions!
    public Circle(double radius) {
        super();
        if(radius > 0) {
            this.radius = radius;
        }
        else {
            System.out.println("Throw new Exception!!");
        }
    }

    public void setRadius(double radius) throws Exception {
        if (radius < 0) {
            throw new Exception();
        }
        else {
            this.radius = radius;
        }
    }
    public void setCircumference(double circumference) throws Exception {
        if (circumference < 0) {
            throw new Exception();
        }
        else {
            this.circumference = circumference;
        }
    }

    public void setArea(double area) {
        if (area <= 0) {
            System.out.println("Please check your input!");
        }
        else {
            this.area = area;
        }
    }

    public double getRadius() {
        return radius;
    }

    public double getCircumference() throws Exception{
        if (radius < 0) {
            throw new Exception();
        }
        else {
            return circumference;
        }
    }

    public double getArea() throws Exception{
        if (radius < 0) {
            throw new Exception();
        }
        else {
            return area;
        }
    }

    @Override
    public double circumference() {
        return 2 * Math.PI * radius;
    }

    @Override
    public double area() {
        return radius * radius * Math.PI;
    }
}

The code seems kinda messy but that's because I've been trying everything :) Any advice is welcome. Btw I was using Arraylist but according to some people I know, using Array is much better in this circumstance.

CodePudding user response:

The methods in your Util class are static. A static method should be invoked on the class it is declared as part of. Both of your methods you have declared to take a parameter of type Shape[]. So you should invoke them like so:

Util.getAccumulatedArea(shapes)

…where shapes is a value of type Shape[].

  • Related