Teaser Image

qnoid

Markos Charatzas - London, UK




/**
 * Describing all glasses of water in the known world
 */
public class GlassOfWater 
{
    private final Material material;
    private final Weight weight;
    private int capacity;
    
    /**
    * When creating a new glass of water you have to say what material, how
    * much it weights and how much water it can hold.
    * 
    * Pretty much like "Simon says"
    * 
    * @param material the material of the glass
    * @param weight its weight
    * @param capacity how much water can it hold
    */
    public GlassOfWater(Material material, Weight weight, int capacity) 
    {
        this.material = material;
        this.weight = weight;
        this.capacity = capacity;
    }

    /**
    * @return true if you can smash it, is it made of glass?
    */
    public boolean smash() {
    return this.material.equals(Material.GLASS);
    }
    
    /**
    * @return true if you can lift it, is it light?
    */
    public boolean lift() {
    return this.weight.equals(Weight.LIGHT);
    }
    
    /**
    * Drink responsibly
    *  
    * @param amount how much do you want to drink
    * @return what's left.
    */
    public int drink(int amount){
    return this.capacity -= amount;
    }
}

/**
 * Let's prove our theory that you can indeed lift/smash and drink from a glass 
 * of water
 */
public class GlassOfWaterTest 
{
    @Test
    public void test() throws Exception 
    {
        /**
        * Here is your glass of water object.
        */
        GlassOfWater aGlassOfWater = new GlassOfWater(Material.PLASTIC, Weight.HEAVY, 330);
        
        /**
        * Let's drink some, water level drops
        */
        int remainingWater = aGlassOfWater.drink(330);
        
        Assert.assertFalse( aGlassOfWater.lift() );
        Assert.assertFalse( aGlassOfWater.smash() );
        Assert.assertEquals( 0, remainingWater );
    }
}

This is the first part of the series (Introduction to object oriented programing) about taking a paragraph of text and identifying objects that are the corner stone of an object oriented program.

Before I go into the actual text let me first establish some basic rules about what is an object. Go get a glass of water. It's the first step to understanding OO programming. Go on, I'll be waiting.

Would you say that the glass of water is an object?

If so, how would you describe it? It's made of a material (glass/plastic), has some weight (heavy/light) and holds some amount of water as in; its capacity. It has properties.

What can you do with it? Smash it, lift it, and last but not least drink from it. It has methods.

Now, take a good look at yours. Is it broken? Is it floating in mid air? Is it half full? Drink some. Is it half empty now? In one word. It has a state.

Oh, and it has a name. Let's call it aGlassOfWater.

Now there are quite a lot of glasses of water out there. Therefore in order to define them we use a class.

A class merely describes what a glass of water really is. Thus, its properties and methods. (indirectly describes its possible states as well but leave that for now)

Oh, and it has a name. Let's call it GlassOfWater.

What about time? Or color? Are these objects? What classes would they describe them? Leave your thoughts below in the comments.

After that, take a look at the survival kit. That will be the basis of our OO analysis.

Disclaimer: "example code"