Writing classes II

Download Report

Transcript Writing classes II

Lec 13
Writing an Instantiable Class II
Agenda
• Adding to our Balloon Class demo:
– default Constructor method
– inflate method that takes a parameter (amount)
– pop method
– volume method
Recall Structure of an
Instantiable Class
class Balloon
Class Name
size
color
Instance Varbls
Balloon
inflate
getSize
getColor
setColor
pop
Methods
Constructor method
same name as class
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
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
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
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