I Want to use attributes such as the volume and capacity of a cup, I have made base values for the capacity and volume size of a cup in a liquadContainer class. i want to set my cup to the volume and capacity I have set in my liquadcontainer class and display the volume and capacity. however, I am not sure how to use the volume and capacity value in the other class and use it and display it, This is what i have done so far:
public class Cup extends LiquadContainer
{
// ---------------------------------------------------------------------
// class variables
// ---------------------------------------------------------------------
private ContainerColour myColour;
private int iCapacity;
private int iVolume;
private int iTemp;
private int iFill;
private String SzMaterial;
private Boolean bEmpty;
// ---------------------------------------------------------------------
// Constructors
// ---------------------------------------------------------------------
public Cup()
{
super();
reset();
return;
}
public Cup (Cup original) {
this.iCapacity = original.getCapacity();
this.iVolume = original.getVolume();
this.SzMaterial = original.getMaterial();
}
public Cup(int size)
{
this();
super.setCapacity(size);
setColour(ContainerColour.NOT_SET);
return;
}
public Cup (int size , ContainerColour colour)
{
this (size);
setColour(colour);
return;
}
//setters
//main
public void display()
{
System.out.println("Cup: \nCapacity = " + this.iCapacity);
System.out.println("Volume = " + this.iVolume);
System.out.println("Colour = " + getColour() );
if(iVolume == 0) {
bEmpty = true;
}
else {
bEmpty = false;
}
System.out.println("Empty = " + bEmpty );
return;
}
public void Wash()
{
iVolume = 0;
bEmpty = true;
}
public void Fill()
{
iFill = getCapacity() - getVolume();
iVolume = getVolume() + iFill;
}
public static void main(String[] args) {
Cup myCup = new Cup();
myCup.display();
}
}
my liquid container code:
//Private class
private int iCapacity;
private int iVolume;
private int iTemp;
private int iFill;
private String SzMaterial;
private String SzColor;
private ContainerColour myColour;
//Constructor
public LiquadContainer()
{
reset();
return;
}
//default
//Specific
//method
//Setters
public void setCapacity(int iValue)
{
iCapacity = iValue;
}
public void setVolume(int iValue)
{
iVolume = iValue;
}
public void setColour(ContainerColour c)
{
myColour = c;
}
public void setMaterial(String SzValue)
{
SzMaterial = SzValue;
}
public void setFill(int iValue)
{
iFill = getVolume() - getCapacity() ;
}
//getters
public int getCapacity()
{
return iCapacity;
}
public ContainerColour getColour()
{
return(this.myColour) ;
}
public String getMaterial()
{
return SzMaterial ;
}
public int getVolume()
{
return iVolume ;
}
public int getFill()
{
iFill = getCapacity() - getVolume();
return iFill;
}
//utilities
public void fill()
{
iFill = getCapacity() - getVolume();
iVolume = getVolume() + iFill;
}
public void reset()
{
setCapacity(250);
setMaterial("ceramic");
setVolume(200);
setColour(ContainerColour.BLACK);
}
public static void main(String[] args)
{
LiquadContainer myLiquadContainer = new LiquadContainer();
myLiquadContainer.setCapacity(250);
myLiquadContainer.setMaterial("ceramic");
myLiquadContainer.setVolume(200);
myLiquadContainer.setColour(ContainerColour.BLACK);
System.out.println(" LiquadContainer Capacity = " + myLiquadContainer.getCapacity() + "\n Volume = " + myLiquadContainer.getVolume() + "\n Color = " + myLiquadContainer.getColour() + "\n Material = " + myLiquadContainer.getMaterial());
}
>Solution :
All fields of a class are available to all its subclasses (unless they’re private), e.g.
public class LiquidConainer {
protected int volume;
public LiquidContainer(int v) {
volume = v;
}
}
public class Cup extends LiquidContainer {
private ContainerColor myColor;
public Cup(int v, ContainerColor c) {
super(v);
myColor = c;
}
public void display() {
System.out.println("Cup: volume = " + volume + ", color = " + color");
}
}
Volume can be used here, since it is a field of LiquidContainer, and so all subclasses of LiquidContainer can use it as if it was an attribute of their own.
Quick info about visibility keywords:
private: visible only from the class it belongs toprotected: visible only from the class it belongs to and its subclassespublic: visible from anywhere