Search code examples
javavariablessubclasssuperclass

Cannot find variable from subclass in main class


I am trying to use the Volume method from my sub class in my testing class super class by using v.Volume(volume); but it is saying it can't find the symbol volume. I am setting the radius in the testing class, and it is supposed to output the volume from it. I feel like I am over looking something very obvious. I know that it is trying to use a variable named volume from the super class but I am not sure what to put.

testing class code

//balloon testing
    Balloon r = new Balloon();
    Balloon v = new Balloon();

    System.out.println("Radius " + r.getRadius());
    System.out.println("Volume " + v.getVolume());

    r.inflate(5);
    v.Volume(volume);

    System.out.println("Radius " + r.getRadius());
    System.out.println("Volume " + r.getVolume());

sub class code

public class Balloon {
//declare
public double radius;
public double volume;

//constructor
public Balloon(){
    radius = 0;
    volume = 0;
}

//setters
public void inflate(double r){
    radius = r;
}

//accessors
public double getRadius(){
    return radius;
}

public double getVolume(){
    return volume;
}

public void Volume(double volume){
    volume = ((4/3)* Math.PI * (radius*radius*radius));
}

}

Solution

  • change Volume method body to (and rename it):

    public void calculateVolume(){
        volume = ((4/3)* Math.PI * (radius*radius*radius));
    }
    

    and before invoke this method - set radius value by setter.