3.1 Java OOPs - Balloons

Balloon 

Build a balloon

You should be able to construct balloons of any capacity, containing any valid volume of air. You will also need to be able to inflate and deflate air from a balloon. If you try to add too much air, the balloon will pop. You can not remove more air than there is in the balloon.

 

For the balloon class,

HAS-A: attributes

capacity : how much air the balloon can hold before it will pop

currentVolume: the amount of air currently in the balloon

isPopped: true or false wether the balloon is popped.

 

RESPONDS-TO: methods

constructors: special initialization code used when creating a new Balloon() parameters will include the capacity and a volume of air.  Notice that there are more than one constructor methods available to use. The names are the same (remember the constructors names must exactly match the class name).  This can be done provided that the signature of the parameter list is different.  Also notice that the constructors do not have return types.

 

Balloon(double c, double a)

where c is the capacity and a is the amount of air in the balloon.

This method will call balloon(c,a)

 

Balloon(double c )

where c is the capacity.

This method will call balloon(c,0)


Methods: 

private void balloon(double c, double a)

where c is the capacity and a is the amount of air in the balloon.

This method will create a balloon with a capacity of size c and a currentVolume of a

Although this method has the same name as the constructors, it is not a constructor; it starts with a lower case letter, and has a void for the return type.


public void inflate(double amount)

The parameter amount is the amount of air to be added to the balloon.

 

public void deflate(double amount)

The parameter amount is the amount of air to be removed from the balloon.

 

public String toString()

The method that allows a user to fetch a String representation of an instance. The toString() method is a special method that can be defined in any class you create. 


Party Time

Create a Party class that will build a number of balloons, inflate them and display their current state. Use each of your constructors. Below is a sample of what your program should look like.  Make sure you include all of your methods (inflate(), deflate()) in your Balloon class in your party class.


public class Party {

    Balloon b;

    Balloon b2;


    public Party(){

        b = new Balloon(10,10);

        b2 = new Balloon (15);


        b2.inflate(10);


        System.out.println(b);

        System.out.println(b2);

    }

}

Comments