Lec 12 Writing an Instantiable Class
description
Transcript of Lec 12 Writing an Instantiable Class
![Page 1: Lec 12 Writing an Instantiable Class](https://reader035.fdocuments.us/reader035/viewer/2022062501/56816232550346895dd268aa/html5/thumbnails/1.jpg)
Lec 12 Writing an Instantiable Class
![Page 2: Lec 12 Writing an Instantiable Class](https://reader035.fdocuments.us/reader035/viewer/2022062501/56816232550346895dd268aa/html5/thumbnails/2.jpg)
Agenda
• Review objects and classes• Making our own class to create objects
– Our first "Instantiable" class– with it's own Javadoc!
• Constructor method• Parameter passing into methods• Returning values from methods
![Page 3: Lec 12 Writing an Instantiable Class](https://reader035.fdocuments.us/reader035/viewer/2022062501/56816232550346895dd268aa/html5/thumbnails/3.jpg)
We have been learning how to represent data in Java
• Primitives– int, char, double, boolean– only hold simple pieces of data
• a temperature, a choice, how many layers of lettuce
• Objects (of existing Instantiable Classes)– Island, Particle, GolfAnimator, String, Scanner
• more complicated "things"• data and methods (actions) combined• Class as a blueprint that lets us make many "instances"
![Page 4: Lec 12 Writing an Instantiable Class](https://reader035.fdocuments.us/reader035/viewer/2022062501/56816232550346895dd268aa/html5/thumbnails/4.jpg)
Remember: 3 Different Kinds of Classes1. Application Class – what we've been doing
– Has public static void main ( String [] args)– all of our programs to this point
2. Classes with only static utility methods – like Math class, NumericalStuff, StringUtil, Coins– to invoke a method:
• nameOfClass.method(parameters);• double flip = Math.random(); // picks a random number
// and stores in flip• Coins.dispenseQuarter(); // prints a quarter on console
![Page 5: Lec 12 Writing an Instantiable Class](https://reader035.fdocuments.us/reader035/viewer/2022062501/56816232550346895dd268aa/html5/thumbnails/5.jpg)
Instantiable Class3. Instantiable Class – used to make objects from
– String, Scanner, Point, Island, GolfAnimator, Particle– to invoke a method
• nameOfObject.method(parameters);– Island desert = new Island(5); // desert is name of new Island
– desert.moveNorth(); // move N on Island desert
– Island hawaii = new Island(1000); // hawaii is new Island– hawaii.moveEast(); // move E on Island hawaii
![Page 6: Lec 12 Writing an Instantiable Class](https://reader035.fdocuments.us/reader035/viewer/2022062501/56816232550346895dd268aa/html5/thumbnails/6.jpg)
Review: Classes and Objects
![Page 7: Lec 12 Writing an Instantiable Class](https://reader035.fdocuments.us/reader035/viewer/2022062501/56816232550346895dd268aa/html5/thumbnails/7.jpg)
![Page 8: Lec 12 Writing an Instantiable Class](https://reader035.fdocuments.us/reader035/viewer/2022062501/56816232550346895dd268aa/html5/thumbnails/8.jpg)
Object (reference) variables
![Page 9: Lec 12 Writing an Instantiable Class](https://reader035.fdocuments.us/reader035/viewer/2022062501/56816232550346895dd268aa/html5/thumbnails/9.jpg)
How to represent a Balloon in Java?
• Data to define it:sizecolor
• Methods to modify it:can make a new Ballooncan inflate itcan get the size, colorcan change the colorcan pop it!
size
Color.YELLOW
![Page 10: Lec 12 Writing an Instantiable Class](https://reader035.fdocuments.us/reader035/viewer/2022062501/56816232550346895dd268aa/html5/thumbnails/10.jpg)
Structure of an Instantiable Class
class Balloonsizecolor
BallooninflategetSizegetColorsetColorpop
Class NameInstance Varbls
Methods
Constructor methodsame name as class
![Page 11: Lec 12 Writing an Instantiable Class](https://reader035.fdocuments.us/reader035/viewer/2022062501/56816232550346895dd268aa/html5/thumbnails/11.jpg)
Balloon Demonstration• We will now create our own Balloon class• If you want you can just download these into new
class files and run– Balloon Class– BalloonApp Class
• We will also learn the following– parameter passing into a method– returning a value– creating our own JavaDoc for our balloon class
• Also, here is the Particle class from our previous labs
![Page 12: Lec 12 Writing an Instantiable Class](https://reader035.fdocuments.us/reader035/viewer/2022062501/56816232550346895dd268aa/html5/thumbnails/12.jpg)
Using a class -- objects• A class is a blueprint for making objects• When you declare a variable of your new class it’s
called an object reference variable:Balloon hotAir;hotAir = new Balloon(5,Color.RED);
Balloon bal=new Balloon(2,Color.BLUE); Balloon weather= new Balloon(200,Color.WHITE);
hotAir
balweather
![Page 13: Lec 12 Writing an Instantiable Class](https://reader035.fdocuments.us/reader035/viewer/2022062501/56816232550346895dd268aa/html5/thumbnails/13.jpg)
You can use the public methods defined for the class
Balloon bal= new Balloon(5,Color.BLUE);
bal.inflate();
bal.pop();
![Page 14: Lec 12 Writing an Instantiable Class](https://reader035.fdocuments.us/reader035/viewer/2022062501/56816232550346895dd268aa/html5/thumbnails/14.jpg)
Parameter Passing• When we "call" a method: (in BalloonApp class)
• We jump into the method with the arguments(Balloon class)
![Page 15: Lec 12 Writing an Instantiable Class](https://reader035.fdocuments.us/reader035/viewer/2022062501/56816232550346895dd268aa/html5/thumbnails/15.jpg)
• When a method is invoked or "called":
• We jump into the method (with any params)
• When a value is "returned" from a method– we exit method body– return value replaces the "caller"
Return statements
![Page 16: Lec 12 Writing an Instantiable Class](https://reader035.fdocuments.us/reader035/viewer/2022062501/56816232550346895dd268aa/html5/thumbnails/16.jpg)
Why Classes and Objects ?
• It may seem overwhelming or unnecessary at first
• As the complexity/size of your code increases, classes will help you modularize your code
• Helps you visualize and solve problems better
• Brings more order into your code
![Page 17: Lec 12 Writing an Instantiable Class](https://reader035.fdocuments.us/reader035/viewer/2022062501/56816232550346895dd268aa/html5/thumbnails/17.jpg)
Dairy.java
• make Lab12DairyApp folder• Create new class, Dairy.java fill in definition of
– Constructor– getNumCows– addCow
• Create a new class DairyApp.java– test your new Dairy class by constructing a dairy
farm, adding cows and printing the number of cows