Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Java – OOP using original value of a variable in another class

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());

    }


MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>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 to
  • protected: visible only from the class it belongs to and its subclasses
  • public: visible from anywhere
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading