Reputation: 41
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));
}
}
Upvotes: 0
Views: 186
Reputation: 677
Its not clear what you're asking here, but I'll try to answer anyways.
You don't have a subclass or a superclass here, those are very specific terms which you are welcome to read more about.
To your question, though, I think your problem is that in your Volume method you pass in a parameter (volume
) which you do not use at all. Instead, you set a member variable (Balloon.volume
) based on the radius. Balloon.volume
is very different from the parameter double volume
that is passed. I have annotated your classes to try to clarify what's going on here.
public class Balloon {
//declare
public double radius; //this is the member variable which is a property of each member of the Balloon class. I refer to it above as Balloon.volume
public double volume;
public Balloon(){
radius = 0;
volume = 0;
}
public void inflate(double r){
radius = r;
}
public double getRadius(){
return radius;
}
public double getVolume(){
return volume;
}
public void Volume(double volume){ //this is an external, unused parameter
volume = ((4/3)* Math.PI * (radius*radius*radius)); //this sets Balloon.volume and ignores the parameter volume
}
}
Perhaps it would be clearer what was going on if you renamed your variables:
public void Volume(double someOtherVolume){ //this is an external, unused parameter
volume = ((4/3)* Math.PI * (radius*radius*radius)); //this sets Balloon.volume and ignores the parameter volume
}
This should make it clear that the parameter and the member variable are different. Your symbol exception comes from this line:
v.Volume(volume);
The variable there in parens is never defined. You must first declare and define a variable before using it.
double volume = 8;
Since in your testing class you are outside the balloon class, you cannot simply refer to Balloon.volume
. You must create a new variable.
Upvotes: 2
Reputation: 306
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.
Upvotes: 1