Lec 13 Writing an Instantiable Class II. Agenda Adding to our Balloon Class demo: – default...
-
Upload
jeremy-singleton -
Category
Documents
-
view
212 -
download
0
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.](https://reader035.fdocuments.in/reader035/viewer/2022072016/56649eef5503460f94bffbf7/html5/thumbnails/1.jpg)
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.](https://reader035.fdocuments.in/reader035/viewer/2022072016/56649eef5503460f94bffbf7/html5/thumbnails/2.jpg)
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.](https://reader035.fdocuments.in/reader035/viewer/2022072016/56649eef5503460f94bffbf7/html5/thumbnails/3.jpg)
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.](https://reader035.fdocuments.in/reader035/viewer/2022072016/56649eef5503460f94bffbf7/html5/thumbnails/4.jpg)
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.](https://reader035.fdocuments.in/reader035/viewer/2022072016/56649eef5503460f94bffbf7/html5/thumbnails/5.jpg)
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.](https://reader035.fdocuments.in/reader035/viewer/2022072016/56649eef5503460f94bffbf7/html5/thumbnails/6.jpg)
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.](https://reader035.fdocuments.in/reader035/viewer/2022072016/56649eef5503460f94bffbf7/html5/thumbnails/7.jpg)
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