Simple program
Hi, i'm trying to make a simple program to calculate the surface of a circle but my program doesn't work
Does anybody knows what i did wrong?
import java.util.*;
publicclass Opdracht3_1{
publicstaticvoid main(String[] args){
System.out.println("Geef de straal van de cirkel:");
Scanner kb =new Scanner(System.in);
double r = kb.nextDouble();
}publicstaticdouble oppervlakte(double r){
if (r >= 0){
double oppervlakte = r * r * Math.PI;
System.out.println("De oppervlakte van de cirkel is: " + oppervlakte);
return oppervlakte;
}
}}
[1402 byte] By [
Kukiwona] at [2007-11-26 19:39:08]

You never call your method 'oppervlakte' from your main method.kind regards,Jos
I can't see a call to oppervlakte. Shouldn't you call it?Kaj
What does "doesn't work" mean? What input are giving, what output do you expect to see, and what output are you actually seeing? Also, what exception, if any, are you getting?
But from what I see right now, at a quick glance, you are taking the input, but then are not doing anything with it. You don't call the method, and you don't output it's result, even if you had called it.
Edit: Man, I need to give up. I am just too slow today.
> You never call your method 'oppervlakte' from your> main method.> > kind regards,> > JosWhat? You beat me? Since when is you fast? ;)Was it because of the dutch? :p
> > You never call your method 'oppervlakte' from your
> > main method.
> >
> > kind regards,
> >
> > Jos
>
> What? You beat me? Since when is you fast? ;)
>
> Was it because of the dutch? :p
I feel so speedy today after two espressos ;-)
Here's another (but very ugly) solution to the OP's problem: leave out
the lines:}public static double oppervlakte(double r){
...
return oppervlakte;
kind regards,
Jos :-)
Ok, i'll specifie the problem. I'm new to methods and I don't really see why i should use them. I'm trying tocreate a method in where the surface of the circle (with radius r) is calculated. After this is done, I'm want to display the result. Normally I just work in the main method but I have to use a second method now. When I try to print the result in the main method, like this: import java.util.*;
public class Opdracht3_1{
public static void main(String[] args){
System.out.println("Geef de straal van de cirkel:");
Scanner kb = new Scanner(System.in);
double r = kb.nextDouble();
System.out.println("De oppervlakte van de cirkel is: " + oppervlakte);
public static double oppervlakte(double r){
if (r >= 0){
double oppervlakte = r * r * Math.PI;
return oppervlakte;
}
}
}
}
It says the beginning of the second method is an illegal start of expression...
Srry for the bad english
Greetings
Hi,
Replace the following line in your program
System.out.println("De oppervlakte van de cirkel is: " + oppervlakte);
--with--
System.out.println("De oppervlakte van de cirkel is: " + oppervlakte(r));
:-)
Regards,
Kumar.
When I do this, I get an error that says: missing return statement. Anyone have any idea?RegardsJordy
Take a look at your method, and think about what would happen (program flow-wise) if r < 0. Then take a stab at repairing that program flow in respect to a return statement.
Hello again,
I solved my problem this way:
import java.util.*;
public class test{
public static void main(String[] args){
Scanner kb = new Scanner(System.in);
System.out.println("Voer de straal van de cirkel in:");
double r = kb.nextDouble();
double oppervlakte = max(r);
}
public static double max(double r){
//post: retourneert grootste van x en y
double res = 0;
if (r >= 0){
res = r * r * Math.PI;
System.out.println("De oppervlakte van de cirkel is:"+ res);}
else
System.out.println("De cirkel kan geen negatieve straal hebben!");
return res;
}
}
Thanks to everybody!
Greetings
Jordy
> Hello again,> I solved my problem this way:You shouldn't print in max. Btw. Why did you call it max?Main should call the method, and print the returned value.Kaj
> I feel so speedy today after two espressos ;-)Jebus. I need to get some energy drinks in order to be able to keep up the pace.Kaj
Hi,
Throw an unchecked exception if r is negative.
if (r >= 0)
{
double oppervlakte = r * r * Math.PI;
return oppervlakte;
}
else
{
throw new IllegalArgumentException();
}
Regards,
Kumar.
I am late by 4 messages :)Regards,Kumar.
if you don't call any methods in main then there's no point in having a second method. you mine as well perform the logic in main.