Presentation is loading. Please wait.

Presentation is loading. Please wait.

Lec 12 Writing an Instantiable Class. Agenda Review objects and classes Making our own class to create objects – Our first "Instantiable" class – with.

Similar presentations


Presentation on theme: "Lec 12 Writing an Instantiable Class. Agenda Review objects and classes Making our own class to create objects – Our first "Instantiable" class – with."— Presentation transcript:

1 Lec 12 Writing an Instantiable Class

2 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

3 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"

4 Remember: 3 Different Kinds of Classes 1.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

5 Instantiable Class 3. 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

6 Review: Classes and Objects

7

8 Object (reference) variables

9 How to represent a Balloon in Java? Data to define it: size color Methods to modify it: can make a new Balloon can inflate it can get the size, color can change the color can pop it! size Color.YELLOW

10 Structure of an Instantiable Class class Balloon size color Balloon inflate getSize getColor setColor pop Class Name Instance Varbls Methods Constructor method same name as class

11 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 Balloon – BalloonApp Class BalloonApp 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 labsParticle

12 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 bal weather

13 You can use the public methods defined for the class Balloon bal= new Balloon(5,Color.BLUE); bal.inflate(); bal.pop();

14 Parameter Passing When we "call" a method: (in BalloonApp class) We jump into the method with the arguments (Balloon class)

15 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

16 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

17 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


Download ppt "Lec 12 Writing an Instantiable Class. Agenda Review objects and classes Making our own class to create objects – Our first "Instantiable" class – with."

Similar presentations


Ads by Google