Lec 13 Writing an Instantiable Class II. Agenda Adding to our Balloon Class demo: – default...

7
Lec 13 Writing an Instantiable Class II

Transcript of Lec 13 Writing an Instantiable Class II. Agenda Adding to our Balloon Class demo: – default...

Page 1: Lec 13 Writing an Instantiable Class II. Agenda Adding to our Balloon Class demo: – default Constructor method – inflate method that takes a parameter.

Lec 13 Writing an Instantiable Class II

Page 2: Lec 13 Writing an Instantiable Class II. Agenda Adding to our Balloon Class demo: – default Constructor method – inflate method that takes a parameter.

Agenda

• Adding to our Balloon Class demo:– default Constructor method– inflate method that takes a parameter (amount)– pop method– volume method

Page 3: Lec 13 Writing an Instantiable Class II. Agenda Adding to our Balloon Class demo: – default Constructor method – inflate method that takes a parameter.

Recall Structure of an Instantiable Class

class Balloon

sizecolor

BallooninflategetSizegetColorsetColorpop

Class NameInstance Varbls

Methods

Constructor methodsame name as class

Page 4: Lec 13 Writing an Instantiable Class II. Agenda Adding to our Balloon Class demo: – default Constructor method – inflate method that takes a parameter.

Method Overloading• We can add methods to Balloon with same names

– as long as have different parameters– the Constructor we made in our Balloon class

– A second "Default" constructor with no parameters

Page 5: Lec 13 Writing an Instantiable Class II. Agenda Adding to our Balloon Class demo: – default Constructor method – inflate method that takes a parameter.

Which one is used?

• Balloon myBal = new Balloon(10,Color.RED);

• Balloon yrBal = new Balloon();

• default constructor automatically – sets size to 0– sets color to Color.WHITE

Page 6: Lec 13 Writing an Instantiable Class II. Agenda Adding to our Balloon Class demo: – default Constructor method – inflate method that takes a parameter.

Now we'll begin BalloonDemo2

• start with BalloonDemo from last time• add default constructor• add inflate method that takes an amount• add pop method

– modify both inflate methods• add volume method

– should return 0 if balloon has been popped

Page 7: Lec 13 Writing an Instantiable Class II. Agenda Adding to our Balloon Class demo: – default Constructor method – inflate method that takes a parameter.

More Dairy.java

• make Lab13DairyFarm folder• Copy, Dairy.java file from Lab12 folder, then add

– default Constructor– addCow takes a number– calcMllkProfit takes a price/gallon– calcButterProfit takes a price/pound

• Create a new class Farm.java that– tests your new Dairy class by constructing a dairy farm,

adding cows and printing the number of cows– And printing profit from selling milk or butter