0

I created two circle objects that (from what I thought) each hold a different value for radius. However, the output for each Circle object is the same. From my minute understanding of Programming, I was under the impression that if you don't pass a parameter for your object, the object uses the default constructor. Can someone please explain where I am wrong, and how I can go about receiving two different circle objects with different radius values?

//create circle object use method to find area

package circle;

public class Circle {

    static final double PI = 3.14;
    static private double radius;
    static private double area;
    static private String name;

    //default constructor 
    public Circle()
    {
      this.radius = 2;
    }

    //constructor with argument
    public Circle(int radius)
    {
        this.name = "JOESHMO-THE-CIRCLE";
        this.radius = radius;

    }

    public static double getRadius()
    {

        return radius;
    }

    public static double getArea()
    {

        //calculate area
        area = PI * Math.pow(radius,2);

        return area;
    }

    public static String getName()
    {

        return name;
    }


    public static void main(String[] args) {


        Circle circle1 = new Circle(4);

        Circle circle2 = new Circle();

        double answer = circle1.getArea();

        System.out.println(answer);
        System.out.println("The radius of " + circle1.getName() + " is " + circle1.getRadius());

        System.out.println(circle2.getArea());

    }

}

the output is currently:

12.56

The radius of JOESHMO-THE-CIRCLE is 2.0

12.56
Hovercraft Full Of Eels
  • 283,665
  • 25
  • 256
  • 373
DevEarl
  • 18
  • 1
  • 3

0 Answers0