views:

239

answers:

2

so i made a circle class that sets the radius, and should output the radius, circumference and area however, if i input the radius, the radius, circumference, area wont output back, it just shows as 0.0 heres my Circle.java

public class Circle
{
    private double radius;
    private double PI = 3.14159;

    public void setRadius(double rad)
    {
            radius = rad;
    }

    public double getRadius()
    {
            return radius;
    }

    public double getArea()
    {
            return PI * radius * radius;
    }

    public double getDiameter()
    {
            return 2 * radius;
    }

    public double getCircumference()
    {
            return 2 * PI * radius;
    }

}

and heres circledemo.java http://pastie.org/466414 -formatting didnt come out well here

First I input the radius, but when i call getRadius, circumference, area, it just outputs 0.0

+5  A: 
htw
A good rule of thumb is that each time you see a {, you're entering a new scope, and each time you see a }, you're leaving it. Meaning that anything declared inside a pair of {}'s, goes out of scope when you get to the }. I might be oversimplifying a little bit, but that gives you the right idea I think.Also, it's a good idea to increase your indentation at every {, and then decrease it at every }.
MatrixFrog
That's a good way of phrasing it.
htw
alright thank you, that solved the problem
Raptrex
@Raptrex It is a courtesy that if @htw's answer was the answer you Accept as correct for you to mark the answer as "Accepted" (by clicking the checkmark next to the answer).
Alex B
+1  A: 

Create the Circle object instance (i.e the code Circle one = new Circle()) OUTSIDE of the while loop. Every iteration of the loop is causing the object to be re-created with new state (i.e. new radius).

royrules22