Reputation: 115
So I need to implement a getCurrentVolumeInCCs method for the vessel class, however I am a little confused as to how to do it. Also I am getting the correct values for the volume in both the Glass and Cup classes but when I call any of the other methods in the Vessel class these values do not get carried over. I'm guessing its because they are stored in the subclass and cannot be used in the base class, am I right?
Vessel Class (abstract)
package containers;
public abstract class Vessel {
private double volume;
private double currentVolume;
public Vessel() {
volume = 0;
currentVolume = 0;
}
abstract double getVolumeInCC();
public double getCurrentContents() {
return currentVolume;
}
public double drink(double amountToDrink) {
double volumeLeft;
System.out.println("you drank " + amountToDrink + " CCs");
volumeLeft = volume - amountToDrink;
return volumeLeft;
}
public double fill(double amountPoured) {
double volumeAdded = amountPoured;
double volumeSpilled = 0;
System.out.println("you poured " + amountPoured + " CCs");
if(volumeAdded > volume) {
volumeSpilled = amountPoured - volume;
System.out.println("you spilled " + volumeSpilled);
}
return volumeAdded;
}
}
Glass Class
package containers;
public class Glass extends Vessel{
private double volume;
private double radiusBase;
private double height;
public Glass() {}
public Glass(double r, double h) {
this.radiusBase = r;
this.height = h;
}
@Override
double getVolumeInCC() {
volume = (Math.PI)*(Math.pow(radiusBase, 2))*height;
return volume;
}
}
Cup Class
package containers;
public class Cup extends Vessel {
private double volume;
private double radiusLip;
public Cup(double r) {
volume = 0;
this.radiusLip = r;
}
@Override
double getVolumeInCC() {
volume = (0.5)*(Math.PI)*(Math.pow(radiusLip, 3))*(4/3);
return volume;
}
}
Upvotes: 1
Views: 122
Reputation: 201537
You are shadowing volume
from Vessel
in your subclasses because it's a private
field. I believe that you wanted to make volume
and currentVolume
protected in Vessel
,
protected double volume;
protected double currentVolume;
Also, you should remove private double volume
from the sub-classes (so you don't shadow the field they now inherit from Vessel
).
Upvotes: 2