Advertisement
replicaJunction

OOP 2: Container

Jan 11th, 2013
36
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.24 KB | None | 0 0
  1. /*
  2.  * This is another simple example class for Object-Oriented Programming.
  3.  * It represents a container that can have another box inside it - but you can only put something in it
  4.  * or see what's in it if the container is open (think of it as an opaque lid).  You can also only
  5.  * store things in the Container that are smaller than the Container is. Modifying this class so that
  6.  * it checks not just the total volume, but also each individual side, is left as an exercise to the
  7.  * user.
  8.  *
  9.  * This class extends the Box class here: http://pastebin.com/mCu64xbP
  10.  *  
  11.  * Created by replicaJunction on 1/11/2013.
  12.  */
  13.  
  14. public class Container extends Box
  15. {
  16.     private Box myContents;
  17.     private boolean isOpen;
  18.    
  19.     public Container(int l, int w, int h)
  20.     {
  21.         super(l, w, h);
  22.         isOpen = false;
  23.     }
  24.    
  25.     public void open()
  26.     {
  27.         isOpen = true;
  28.     }
  29.    
  30.     public void close()
  31.     {
  32.         isOpen = false;
  33.     }
  34.    
  35.     public Box getContents()
  36.     {
  37.         if (isOpen)
  38.             return myContents;
  39.         else
  40.             return null;
  41.     }
  42.    
  43.     public boolean setContents(Box newContents)
  44.     {
  45.         if (isOpen == false)
  46.             return false;
  47.        
  48.         int contentVolume = newContents.getVolume();
  49.         if (contentVolume >= getVolume() )
  50.             return false;
  51.        
  52.         myContents = newContents;
  53.         return true;
  54.     }
  55. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement