Home > front end >  Java - interacting with other classes teacup example
Java - interacting with other classes teacup example

Time:02-01

I want to create 2 classes one where I have an object which is a mug that has a volume of tea in it and a second class which is essentially a teapot that can pour and fill the mug with tea to the top but I am not sure how to do this. I have written code for the Mug class but I am not sure how to use a class(teapot) to interact and pour and fill the mug. This is a school assignment. This is what I have done:

Mug class:

public class Mug {
    
    //Private class

    private int iCapacity;
    private int iVolume;
    private int iTemp;
    private int iFill;
    private String SzMaterial;
    private String SzColor;
    
    //Constructor
    
    public Mug() 
    {
     reset();
     
     return;
    }
    
     //default
    
     //Specific
    
     //method
       
      //Setters

      public void setCapacity(int iValue) 
      {
        
        if(iValue <= 250 ) {
            iCapacity = iValue; 
        }
        else
        {
            iCapacity = 0;
        }
      }
      
    public void setVolume(int iValue) 
   {
        iVolume = iValue;  
      }
    
    public void setColor(String SzValue) 
    {
        SzColor = SzValue;
    }
    
    public void setMaterial(String SzValue) 
    {
        SzMaterial = SzValue;
    }
    
    public void setTemp(int iValue) 
    {
        iTemp = iValue;
    }
    
    public void setFill(int iValue) 
    {
        iFill = getVolume() - getCapacity() ;
    }
    
    //getters
      
      public int getCapacity()
      {
          return iCapacity;
      }
      
      public String getColor()
      {
          return SzColor ;
      }
      
      public int getTemp()
      {
          return iTemp ;
      }
      
      public String getMaterial()
      {
          return SzMaterial ;
      }
      
      public int getVolume()
      {
          return iVolume ;
      }
      
      public int getFill() 
      {
          

        iFill = getCapacity() - getVolume();
          
        return iFill;
      }
      
    
    //utilities
      
      public void reset() 
      {
        setCapacity(250);
        setMaterial("ceramic");
        setVolume(45);
        setTemp(20);
        setColor("blue");
      }
      
      
      
      public static void main(String[] args) 
    {
          Mug myMug = new Mug();
          myMug.setCapacity(250);
          myMug.setTemp(2);
          myMug.setMaterial("ceramic");
          myMug.setVolume(240);
          myMug.setColor("white");
          
          System.out.println(" Mugs Capacity = "   myMug.getCapacity()   "\n Volume = "   myMug.getVolume()   "\n Temp = "   myMug.getTemp()   "\n Color = "   myMug.getColor()   "\n Material = "   myMug.getMaterial()  "\n Fill = "   myMug.getFill() );
          
    }
  }

And I was not able to do TeaPot class as I am unsure how to do this.

CodePudding user response:

You of course first need a class TeaPot:

class TeaPot {
}

This class should be able to fill a Mug:

class TeaPot {
    public void fill(Mug mug) {
    }
}

To actually fill the Mug to the top you first need to figure out how much room your Mug has. After that you can fill the mug:

class TeaPot {
    public void fill(Mug mug) {
        int capacity = mug.getCapacity();
        mug.setVolume(capacity);
    }
}

At last you need to call this method from your main method:

public static void main(String[] args) {
    Mug myMug = new Mug();
    // Removed for clarity
    TeaPot myTeaPot = new TeaPot();
    teaPot.fill(myMug);
}

Depending on how exactly you should model the TeaPot you might still need to add more attributes or implement the filling differently (for example by checking for free space instead of total space in the mug).

  •  Tags:  
  • Related