Paul Kappock
Paul Kappock

Reputation: 23

Calculating the area of a circle in Java

I need to write a program to calculate the area of a circle and I seem to have everything right except when I run the program and input the values the area calculation comes up as zero.

public class Circle {

    private double radius;
    private double area;

    public Circle() {
        radius = 0;
        area = 0;
    }

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

    public double getRadius() {
        return radius;
    }

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

    public double getArea() {
        return area;
    }

    public void setArea(double area) {
        area = radius * radius * Math.PI;
    }

    public String toString() {
        return "The radius of the circle is: " + radius + ", and the area is: " + area;
    }
}

What do I need to change so that when my test code calls the toString it will output a calculated area?

Upvotes: 1

Views: 57924

Answers (6)

Neeraj Gahlawat
Neeraj Gahlawat

Reputation: 1679

public static double getAreaOfCircle(int radius){
        return Math.PI*radius*radius;
    }

Upvotes: 0

Fadi
Fadi

Reputation: 307

import java.util.Scanner;
public class Circle {
    // variable PI is readable only;
    // constant value
    public static final double PI = 3.14;
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        Scanner input = new Scanner(System.in);

        System.out.print("Enter raduis: ");
        double raduis = input.nextDouble();

        double area = PI * raduis * raduis;
        System.out.print("Circle area = " + area);

    }

}

Upvotes: 0

Hak
Hak

Reputation: 99

This is my version of the Circle_Math Class, with entry validation and with the minimum number of lines / operations and memory space to get the job done. Please leave a comment if you want.

public class circle {
    static double rad;

    public circle() {
        rad = 0;
    }

    public static void setRad() {
        Scanner sc = new Scanner(System.in);

        do {
            while (!sc.hasNextDouble()) {
                sc.next();// this code is to skip the exception created 
            }
            rad = sc.nextDouble();
        } while (rad < 0);

        System.out.println("radius value is:" + rad);

    }

    public static double getCirclearea() {
        return rad * rad * Math.PI;
    }

    public static double getCircumference() {
        return 2 * Math.PI * rad;
    }

}

Upvotes: 0

sdanzig
sdanzig

Reputation: 4500

You should have a method calculate area based on the current radius. Area should not be set.

public class Circle {
    private double radius;

    public Circle() {
        radius = 0;
    }

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

    public double getRadius() {
        return radius;
    }

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

    public double getArea() {
        return calculateArea();
    }

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

    public String toString() {
        return "The radius of the circle is: " + radius + ", and the area is: "
                + calculateArea();
    }
}

If you did wish to store area in a variable, you should update it when you set the radius. It shouldn't be independently set from "setArea". Otherwise, you're vulnerable to inconsistency. Also, a note from Josh Bloch's "Effective Java". While your toString should take advantage of this "calculated area" rather than replicating the calculation, you shouldn't have toString call anything in your public API. That would be an issue if, for instance, you overrode getArea, which would mean it would behave differently than your Circle.toString expects. That's why I put the private "calculateArea" in there.

Upvotes: 3

satya
satya

Reputation: 201

Pass radius instead of area in the setArea method:

public void setArea( double radius )  
{
    area = (radius)*(radius)*Math.PI;
}   

for complete code- http://pastebin.com/jggRrUFd

Upvotes: 0

Michael Yaworski
Michael Yaworski

Reputation: 13483

You originally set the area to be 0. You created a method to change that, but never called it. So call it. Change this:

public String toString() {
    return "The radius of the circle is: " + radius + ", and the area is: " + area;
}

to this:

public String toString() {
    setArea(area); // change the value of the area
    return "The radius of the circle is: " + radius + ", and the area is: " + area;
}

Upvotes: -2

Related Questions