# Circle program errors

Dianne Calhoun

Greenhorn

Posts: 29

posted 15 years ago

I'm also working on a circle program, and have gotten the error down to just three errors. Please look at my code and the error and see if you can help me with what I need to do to fix it. Thanks for you help.

Here are the instructions:

Write Circle class that has only 2 static methods, area

and perimeter. Create TestCircle class with main method to test

Circle class. Use the Math.PI constant to calculate the

area and perimeter.

The Circle class has the following methods:

/**

* Class to calculate area and perimeter of circle.

*/

public class Circle {

/** Calculate area of circle.

* @param radius The radius of circle.

*/

public static double area(double radius);

/** Calculate perimeter of circle.

* @param radius The radius of circle.

*/

public static double perimeter(double radius);

}

______________________End of Instructions

End Circle.java

End TestCircle.java

Here are the errors that I'm getting.

C:\myjava\assignment2\problem4>javac Circle.java

Circle.java:17: variable area might not have been initialized

double area = area;

^

Circle.java:18: variable perimeter might not have been initialized

double perimeter = perimeter;

^

Circle.java:19: variable radius might not have been initialized

double radius = radius;

^

3 errors

------------------

[This message has been edited by Dianne Calhoun (edited September 30, 2001).]

Here are the instructions:

Write Circle class that has only 2 static methods, area

and perimeter. Create TestCircle class with main method to test

Circle class. Use the Math.PI constant to calculate the

area and perimeter.

The Circle class has the following methods:

/**

* Class to calculate area and perimeter of circle.

*/

public class Circle {

/** Calculate area of circle.

* @param radius The radius of circle.

*/

public static double area(double radius);

/** Calculate perimeter of circle.

* @param radius The radius of circle.

*/

public static double perimeter(double radius);

}

______________________End of Instructions

End Circle.java

End TestCircle.java

Here are the errors that I'm getting.

C:\myjava\assignment2\problem4>javac Circle.java

Circle.java:17: variable area might not have been initialized

double area = area;

^

Circle.java:18: variable perimeter might not have been initialized

double perimeter = perimeter;

^

Circle.java:19: variable radius might not have been initialized

double radius = radius;

^

3 errors

------------------

[This message has been edited by Dianne Calhoun (edited September 30, 2001).]

Thanks, Dianne

Marilyn de Queiroz

Sheriff

Posts: 9067

12

posted 15 years ago

These need to be initialized to constants. It seems that you're initializing each variable to itself. What's the point of that?

These need to be initialized to constants. It seems that you're initializing each variable to itself. What's the point of that?

JavaBeginnersFaq

"Yesterday is history, tomorrow is a mystery, and today is a gift; that's why they call it the present." Eleanor Roosevelt

Dianne Calhoun

Greenhorn

Posts: 29

posted 14 years ago

Okay, so I think I got my constants initialized right now, but my program is not giving me the right answer, so maybe I'm not getting what what it is I need to put into the constant area right. Here is my changed Circle.java code

Here is the output

Microsoft Windows 2000 [Version 5.00.2195]

(C) Copyright 1985-2000 Microsoft Corp.

C:\myjava>cd assignment2

C:\myjava\assignment2>cd problem4

C:\myjava\assignment2\problem4>javac TestCircle.java

C:\myjava\assignment2\problem4>java TestCircle

radius = 0.0

perimeter = 14.0

area = 11.0

C:\myjava\assignment2\problem4>java TestCircle

radius = 0.0

perimeter = 14.0

area = 11.0

C:\myjava\assignment2\problem4>

End of out put

My TestCircle.java code has not changed from what I already posted. Can you please help me figure out what I'm doing wrong to have the answer come out incorrectly.

------------------

Thanks, Dianne

Here is the output

Microsoft Windows 2000 [Version 5.00.2195]

(C) Copyright 1985-2000 Microsoft Corp.

C:\myjava>cd assignment2

C:\myjava\assignment2>cd problem4

C:\myjava\assignment2\problem4>javac TestCircle.java

C:\myjava\assignment2\problem4>java TestCircle

radius = 0.0

perimeter = 14.0

area = 11.0

C:\myjava\assignment2\problem4>java TestCircle

radius = 0.0

perimeter = 14.0

area = 11.0

C:\myjava\assignment2\problem4>

End of out put

My TestCircle.java code has not changed from what I already posted. Can you please help me figure out what I'm doing wrong to have the answer come out incorrectly.

------------------

Thanks, Dianne

Thanks, Dianne

Martin Rennix

Ranch Hand

Posts: 34

posted 14 years ago

Dianne,

You are being overly complicated! The assignment says Circle should have only 2 static methods, area() and perimeter(). Why has your class got more? These are the only methods your class needs. You don't need any constructors as you will not be creating any Circle objects.

So your calls from TestCircle would be:

<pre>

double radius = 5.0;

System.out.println("area = " + Circle.area(radius));

System.out.println("perimiter = " + Circle.perimeter(radius));

</pre>

In Circle.java all you need is:

<pre>

public static double area(double r)

{

return Math.PI*r*r;

}

public static double perimeter(double r)

{

return 2*Math.PI*r;

}

</pre>

The thing to remember is that static methods can be called without an object being created. You just put the class name in front of the method.

Hope that helps,

Martin

You are being overly complicated! The assignment says Circle should have only 2 static methods, area() and perimeter(). Why has your class got more? These are the only methods your class needs. You don't need any constructors as you will not be creating any Circle objects.

So your calls from TestCircle would be:

<pre>

double radius = 5.0;

System.out.println("area = " + Circle.area(radius));

System.out.println("perimiter = " + Circle.perimeter(radius));

</pre>

In Circle.java all you need is:

<pre>

public static double area(double r)

{

return Math.PI*r*r;

}

public static double perimeter(double r)

{

return 2*Math.PI*r;

}

</pre>

The thing to remember is that static methods can be called without an object being created. You just put the class name in front of the method.

Hope that helps,

Martin