-2

I am trying to calculate the area of the circle using class and object in Java, but the output is not as I want. I want an answer as 78.5 but the area = 0.0, why? Here is the code below-

package com.company;
import java.util.Scanner;
class Circle{
    double r;
    double area= Math.PI*r*r;
}
public class practice {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        Circle c = new Circle();
        System.out.print("Enter the radius of circle: ");
        c.r = sc.nextDouble();
        System.out.println("The area of circle is: "+ c.area);
    }
}

The result I got is-

Enter the radius of circle: 5
The area of circle is: 0.0

Process finished with exit code 0
  • 3
    Hint: when do you expect the calculation for the `Circle.area` field to be performed? When you declare a field, any initialization is done once, at initialization time - not every time you access the field. I suspect you should make it a method rather than a field, or give the `Circle` class a constructor accepting the radius, and perform the calculation then. – Jon Skeet May 12 '21 at 06:37
  • You need to implement getter and setter for your Circle class. – Vlam May 12 '21 at 06:37

3 Answers3

3

Try this code, should work. Compare with what you have done so far: You are calculating area before the user has entered data. I suggest you to read about constructors

package com.company;
import java.util.Scanner;
class Circle{
    double r = 0.0;
    double area= 0.0;

    public Circle( double r ){
      this.r = r;
      this.area = Math.PI*this.r*this.r;
    }

}
public class practice {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        System.out.print("Enter the radius of circle: ");
        Circle c = new Circle( sc.nextDouble() );
        System.out.println("The area of circle is: "+ c.area);
    }
}
  • This would require a new object to be created for each calculation, not optimal IMO – Joakim Danielson May 12 '21 at 06:44
  • OP's question not related with optimal code, so code mimics OP's original code. – Victor Polo De Gyves Montero May 12 '21 at 06:46
  • The solution provided is incorrect. In that it does allow reassigning of the `r` variable of the Circle instance. Which would yield in an incorrect `area` for the set `r`. Your solution only works if you keep creating new Circle instances, but the code does not force you to do so. – Gerben Jongerius May 12 '21 at 07:04
  • @GerbenJongerius the solution is focused on the OP's question and issues, not on java correctness. You can easily see that his code creates Circle and calculates area only once, so no need to keep creating more Circle instances since no need to recalculate area. – Victor Polo De Gyves Montero May 12 '21 at 07:18
  • I meant optimal when it came to design and usage and nothing else just so there is no misunderstanding. I don’t think your solution is the best but it is a workable one, I agree. I think you should make the solution more complete by making `r` private so it can’t be changed. – Joakim Danielson May 12 '21 at 08:03
3

At new Circle(), r is initialized to be 0 (since no explicit assignment is given) and area is calculated with this and thus store the value 0.

At c.r = sc.nextDouble();, c.r is reassigned to hold the scanned value. However, area will not be automatically recomputed based on this assignment and hence remains at 0.

1

You have to understand that the code at the constructor will be run only once when an object is created.

If you have no constructor (like in your example code above) then the code will be run when the program is run. The values of not initialized double values will be 0.0. That's the problem in your case too. Your area calculation will be translated to area = 3.14 * 0.0 * 0.0. I would suggest following the conventions and best practices this way:

     class Circle
    {
        private double radius = 0.0; // Best practice is to declare the variable private and access it through getters & setters

        public Circle(double radius)
        {
            this.radius = radius;
        }

        public double calculateArea()
        {
            return Math.PI * this.radius * this.radius ;
        }

        public double getRadius()
        {
            return radius;
        }

        public void setRadius(double radius)
        {
            this.radius = radius;
        }
    }

    public class Practice
    {
        public static void main(String[] args)
        {
            Circle c = new Circle(5);
            System.out.println("Area of this circle is : " + c.calculateArea());
        }
    }
Renis1235
  • 4,116
  • 3
  • 15
  • 27