I am havin an issue with the findVolume for the sphere part. It seems to ignore the "4/3" part but calculates everything else. Is there a way I can make it include this function?

Here is the code:

public class Sphere extends threeDShape{
    double r;
    public Sphere(double x, double y, double r){
        super(x, y, r, r, r);
        this.r = r;
    public double findArea(){
    public double findVolume(){
        return (double)(4/3*3.14*r*r*r);

    public double getR() {
        return r;

    public void setR(double r) {
        this.r = r;

    public String toString() {
        return String.format("The area of the sphere is: " + findArea() +
                "\nThe volume of the sphere is: " + findVolume());
4 Years
Discussion Span
Last Post by JamesCherrill

Expression is evaluated with / and * same priority, left to right, is as
The first part is 4/3, which is two ints, so evaluated in int arithmetic, ie = 1
It doesn't switch to floating point until you get to the *3.14

You can rearrange the formula so the first part to be evaluated goes straight into floating point...

Edited by JamesCherrill


How would I rearrange it? I tried doing double n = 4/3; int n = 4/3; (3.14 * rrr) * 4/3; And still no luck.


Something as simple as swapping round the first couple of terms will do it: 3.14*4/3*r*r*r - the first thing to be evaluated is 3.14*4, which will be done in floating point, so then dividing that by 3 will also be done in floating point


Oh, now I see, thank you very much :) I will keep in mind that a/b will be = 1 (Java is confusing :P)
By the way, the way you showed me worked and after I saw how you did it, I tried another way which worked:

    public double findVolume(){
        return (double)(4*3.14*r*r*r)/3;

Yes, that's just as good. The rule is simple - operations with two ints are evaluated as integer arithmetic, operations with one or more floating point are evaluated in floating point.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.