Presentation is loading. Please wait.

Presentation is loading. Please wait.

Lec 13 Writing an Instantiable Class II

Similar presentations


Presentation on theme: "Lec 13 Writing an Instantiable Class II"— Presentation transcript:

1 Lec 13 Writing an Instantiable Class II

2 Agenda More class examples Adding to our Balloon Class demo:
default Constructor method inflate method that takes a parameter (amount) pop method volume method

3 We have been working with objects to represent complex things (Strings, Islands, Scanners)

4 Now we are taking a look at what's inside an object

5 data code (methods)

6 Structure of an Instantiable Class – blueprint for objects
Class Name class Dog size breed Dog bark rollOver beg Instance Varbls Methods Constructor method same name as class

7 Suppose we wanted to represent a piggy bank?
Class Name class PiggyBank Instance Varbls Methods Constructor method same name as class

8 Suppose we wanted to represent a piggy bank? (solution)
Class Name class PiggyBank numQuarters numDimes PiggyBank addQuarters addDimes removeQuarters removeDimes currentValue Instance Varbls Methods Constructor method same name as class

9 Suppose we wanted to represent a hamburger? (from lab 4)
Class Name class Hamburger Instance Varbls Methods Constructor method same name as class

10 Suppose we wanted to represent a hamburger? (solution)
Class Name class Hamburger numPatties numLettuce Hamburger addPatty addLettuce print Instance Varbls Methods Constructor method same name as class

11 Back to Balloon Class Name Instance Varbls Methods
class Balloon size color Balloon inflate getSize getColor setColor pop Instance Varbls Methods size Color.YELLOW Constructor method same name as class

12 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

13 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

14 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

15 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


Download ppt "Lec 13 Writing an Instantiable Class II"

Similar presentations


Ads by Google