0

I have some simple java code to call a method with a value to calculate the volume. I just get error like missing ; in JCreator? What is wrong? This is new beginners non object programming level course. Therefore i guess there should be no public static in the method?

public class Matematik {

public static void main(String[] args) {

System.out.println(volym(10));

    double volym(int tal){

    return round((4 * math.pi * math.pow(tal,3) / 3),2);

    }

}

}

2 Answers 2

3

The declaration for volym should not be in main:

public class Matematik {

    public static double volym(int tal){

        return round((4 * math.pi * math.pow(tal,3) / 3),2);

    }

    public static void main(String[] args) {

        System.out.println(volym(10));

    }
}

Edit: it's worth noting that you have other issues too. Namely, java.lang.Math.PI (note the casing) and Math.pow. And Math.round....

Sign up to request clarification or add additional context in comments.

4 Comments

And it should not be static. Rather create a new object of type Matematik in public static void main(... and call its volym method there.
I see no reason to not make it static. He said they haven't discussed objects, and it does not depend on any internal properties.
Thanks! That helped, but I still have some problems with the calculation with this line return Round((4 * Math.Pi * Math.pow(tal,3) / 3),2); I get an error: cannot find symbol ?
You likely need Math.Round. Not sure what imports you have. Also, see the note in Peter's post about round.
1

You can't define a method inside a method. This would be more obvious if you use the IDEs code formatting. Move one of the } up to before the second method (BTW it must be static as well)

Also its Math not math and Math.round on take one value which it rounds to an integer.

If you want to round to two decimal places you can do

Math.round(x * 100) / 100.0;

2 Comments

Can't I use Math.Round(10.2321234,2) to get 10.23?
There is no Math.Round but you could make one if you wanted. There is a Math.round but it takes only one argument. There is no point inventing methods unless you are writing them. ;)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.