Program not compiling + calling methods help
Sup guys, newcomer to the forums with a relatively easy question to ask. Being a relatively newcommer to Java, my first assignment was to create a simple program which asked the user to input a desired radius, and then I was to create several helper methods which used the radius as a passing argument.
I'm using Netbeans 7.2
Code goes as follows:
Code java:
package circleapp;
/**
*
* @author Alex
*/
import java.util.Scanner;
import java.text.DecimalFormat;
public class CircleApp
{
public static void main(String[] args)
{
double radius;
double circ;
double area;
radius = getRadius();
circ = calcCirc();
area = calcArea();
DecimalFormat fmt = new DecimalFormat ("0.##");
System.out.println("The circumference of the circle is" + circ + "\n"
+ "The area of the circle is" + area + "\n" + "Thank you"
+ "for using my program");
}
public static double getRadius()
{
double radius;
Scanner scan = new Scanner(System.in);
System.out.println("Enter the radius of the circle");
radius = scan.nextDouble();
return radius;
}
public static double calcCirc(double rad)
{
radius = rad;
double circ;
double pi;
pi = 3.14159;
circ = 2.0 * rad * pi;
return circ;
}
public static double calcArea(double rad)
{
radius = rad;
double area;
double pi;
pi = 3.14159;
area = pi * rad * rad;
return area;
}
}
When I debugg it, the program compiles up to the beginning step of asking me for the radius, then stops due to the error found when calling the circumference method.
Can't seem to find what is wrong with my helper methods, any ideas?:confused:
Re: Program not compiling + calling methods help
Quote:
stops due to the error
Please post the full text of the error message.
Also please Edit your post and wrap your code with
[code=java]
<YOUR CODE HERE>
[/code]
to get highlighting and preserve formatting.
Re: Program not compiling + calling methods help
1) please put [code=java] before your code and [/code] after your code, it makes the code pretty for my old eyes.
2) when you have a question associated with an error message, always copy paste the full text of the error message, and all relevant code in your post. This makes it easier for people to see what is necessary to help you all in the first go.
You have defined two methods as follows:
public static double calcCirc(double rad)
public static double calcArea(double rad)
...and subsequently called those methods as follows
circ = calcCirc();
area = calcArea();
Notice anything different in the footprint? The methods each have a parameter of type double, and the calls to the methods do not have this necessary data. Modify the calls to supply the double maybe?
Re: Program not compiling + calling methods help
Sorry about not putting it around the code format!
So in order to fix it I would have to call the methods as, circ = calcCirc(double); ?
and area = calcArea(double);
Or instead of (double) I would put (radius)? is that how I would succesfully pass an arguement into a method?
Re: Program not compiling + calling methods help
Put the name of the variable with the value you want to pass to the method within the ()s
Re: Program not compiling + calling methods help
when ur calling calcCirc & calcArea u aren't passing any parameters.... check the methods u defined outside of the main and see if you're following the rules u specified for the two
Re: Program not compiling + calling methods help
Quote:
Originally Posted by
C++kingKnowledge
when ur calling calcCirc & calcArea u aren't passing any parameters.... check the methods u defined outside of the main and see if you're following the rules u specified for the two
so when I identify calcCirc, instead of calcCirc(); I would have to identify it as calcCirc(double radius); in order to pass the radius value onto the calcCirc method?
Re: Program not compiling + calling methods help
ur on the right track... since u defined radius as a double already u dont have to write (double radius) in the parameter just (radius)... o yeah and u didnt assign any type to radius in ur calcCirc and calcArea methods so u should do something about that so ur program will compile correctly
Re: Program not compiling + calling methods help
Quote:
Originally Posted by
C++kingKnowledge
ur on the right track... since u defined radius as a double already u dont have to write (double radius) in the parameter just (radius)... o yeah and u didnt assign any type to radius in ur calcCirc and calcArea methods so u should do something about that so ur program will compile correctly
What do you mean I didn't assign any type to radius in my calcCirc and calcArea? In the methods I put radius = rad; is that wrong? am I supposed to put double rad = radius???
Re: Program not compiling + calling methods help
Quote:
Originally Posted by
alex067
What do you mean I didn't assign any type to radius in my calcCirc and calcArea? In the methods I put radius = rad; is that wrong? am I supposed to put double rad = radius???
The number type for the radius is already declared in the calcCirc and calcArea methods, so you don't need to do it again. They expect doubles.
As for rad/radius: The methods already have a double rad variable declared in the parameters, so you can happily just use rad. (Remember that the radius variable declared in main won't be seen by the other methods.)