calling methods. review we can declare and create objects: dinosaur dino; dino = new dinosaur(); ...

26
Calling Methods Calling Methods

Upload: blanche-cummings

Post on 18-Jan-2016

215 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Calling Methods. Review  We can declare and create objects: Dinosaur dino; dino = new Dinosaur();  We can also shortcut the first two lines: Dinosaur

Calling MethodsCalling Methods

Page 2: Calling Methods. Review  We can declare and create objects: Dinosaur dino; dino = new Dinosaur();  We can also shortcut the first two lines: Dinosaur

ReviewReview We can declare and create objects:We can declare and create objects:

Dinosaur dino;Dinosaur dino;

dino = new Dinosaur();dino = new Dinosaur(); We can also shortcut the first two lines:We can also shortcut the first two lines:

Dinosaur dino = new Dinosaur();Dinosaur dino = new Dinosaur(); We can declare and create primitives:We can declare and create primitives:

double myArea = 54.2*24;double myArea = 54.2*24;

Page 3: Calling Methods. Review  We can declare and create objects: Dinosaur dino; dino = new Dinosaur();  We can also shortcut the first two lines: Dinosaur

MethodsMethods

Another name for Another name for ““BehaviorsBehaviors”” Methods are small pieces of code Methods are small pieces of code

that can be used in other pieces of that can be used in other pieces of code.code.

They have 0 or more inputs, and 0 They have 0 or more inputs, and 0 or 1 output.or 1 output.

Page 4: Calling Methods. Review  We can declare and create objects: Dinosaur dino; dino = new Dinosaur();  We can also shortcut the first two lines: Dinosaur

Why Methods?Why Methods?

Allows you to break up a hard problem Allows you to break up a hard problem into smaller, more manageable partsinto smaller, more manageable parts

Makes your code easier to understandMakes your code easier to understand Makes part of the code reusable so that Makes part of the code reusable so that

you:you: Only have to type it out onceOnly have to type it out once Can debug it all at onceCan debug it all at once Can make changes in one placeCan make changes in one place

Page 5: Calling Methods. Review  We can declare and create objects: Dinosaur dino; dino = new Dinosaur();  We can also shortcut the first two lines: Dinosaur

How to use methodsHow to use methods

First, you must know what behaviors a First, you must know what behaviors a class has.class has.

You need:You need: An object that has the right behaviorAn object that has the right behavior The name of the behaviorThe name of the behavior What inputs it needs, if anyWhat inputs it needs, if any What output it gives, if anyWhat output it gives, if any

Page 6: Calling Methods. Review  We can declare and create objects: Dinosaur dino; dino = new Dinosaur();  We can also shortcut the first two lines: Dinosaur

Example with a PictureExample with a Picture

Accountant:

getPayment

double (debt amount)

int (length of loan)

double (interest rate)

double (payment)

What it means:The Accountant class has a behavior, getPayment.It takes three inputs and gives one output.

Remember: Any number of inputs, but only zero or one output.

Page 7: Calling Methods. Review  We can declare and create objects: Dinosaur dino; dino = new Dinosaur();  We can also shortcut the first two lines: Dinosaur

Call the methodCall the method

Accountant:

getPayment

double (debt amount)

int (length of loan)

double (interest rate)

double (payment)

First, you need an Accountant:

Accountant harry = new Accountant();

Page 8: Calling Methods. Review  We can declare and create objects: Dinosaur dino; dino = new Dinosaur();  We can also shortcut the first two lines: Dinosaur

Four easy steps: Four easy steps: 1. Output1. Output

Accountant:

getPayment

double (debt amount)

int (length of loan)

double (interest rate)

double (payment)

Accountant harry = new Accountant();

Make a variable that will hold the output. If there is no output, skip this step.

double pay =

Page 9: Calling Methods. Review  We can declare and create objects: Dinosaur dino; dino = new Dinosaur();  We can also shortcut the first two lines: Dinosaur

Four easy steps: Four easy steps: 2. Object2. Object

Accountant:

getPayment

double (debt amount)

int (length of loan)

double (interest rate)

double (payment)

Accountant harry = new Accountant();

Write the name of the OBJECT that has the correct behavior and then a period (or dot).

double pay = harry.

Page 10: Calling Methods. Review  We can declare and create objects: Dinosaur dino; dino = new Dinosaur();  We can also shortcut the first two lines: Dinosaur

Four easy steps: Four easy steps: 3. Name of Method3. Name of Method

Accountant:

getPayment

double (debt amount)

int (length of loan)

double (interest rate)

double (payment)

Accountant harry = new Accountant();

Write the name of the method, followed by parentheses & semicolon.

double pay = harry.getPayment();

Page 11: Calling Methods. Review  We can declare and create objects: Dinosaur dino; dino = new Dinosaur();  We can also shortcut the first two lines: Dinosaur

Four easy steps: Four easy steps: 4. Inputs4. Inputs

Accountant:

getPayment

double (debt amount)

int (length of loan)

double (interest rate)

double (payment)

Accountant harry = new Accountant();

In the parentheses, provide the input values in the same order. Give actual values, not the types! If no inputs are needed, skip this step.

double pay = harry.getPayment(3200, 14, .03);

Page 12: Calling Methods. Review  We can declare and create objects: Dinosaur dino; dino = new Dinosaur();  We can also shortcut the first two lines: Dinosaur

Try this one:Try this one:

Calculator:

sqRoot

int (any number)

double (square root of original number)

Use the behavior to calculate the square root of 14. Don’t forget that you also have to create a Calculator object to do the job.

Page 13: Calling Methods. Review  We can declare and create objects: Dinosaur dino; dino = new Dinosaur();  We can also shortcut the first two lines: Dinosaur

Did you get it?Did you get it?

Calculator:

sqRoot

int (any number)

double (square root of original number)

Calculator calc = new Calculator();

double root = calc.sqRoot(14);

Page 14: Calling Methods. Review  We can declare and create objects: Dinosaur dino; dino = new Dinosaur();  We can also shortcut the first two lines: Dinosaur

Objects as input and Objects as input and outputoutput

Registrar:

makeForm

Transcript (gives student history)

Need to create the object that has the behavior as well as any objects that are used as input.

Registrar harry = new Registrar();Transcript stud123 = new Transcript();

Form (to send with applications)

Page 15: Calling Methods. Review  We can declare and create objects: Dinosaur dino; dino = new Dinosaur();  We can also shortcut the first two lines: Dinosaur

Objects as input and Objects as input and outputoutput

Registrar:

makeForm

Transcript (gives student history)

Now follow the steps:Registrar harry = new Registrar();Transcript stud123 = new Transcript();

Form app = harry.makeForm(stud123);

Form (to send with applications)

Page 16: Calling Methods. Review  We can declare and create objects: Dinosaur dino; dino = new Dinosaur();  We can also shortcut the first two lines: Dinosaur

Missing stepsMissing steps

Printer:

printOut

Form (what to print)

Form register = new Form();Printer myHP = new Printer();

myHP.printOut(register);

Page 17: Calling Methods. Review  We can declare and create objects: Dinosaur dino; dino = new Dinosaur();  We can also shortcut the first two lines: Dinosaur

Missing stepsMissing steps

Student:

getHours

int (number of credit hours complete)

Student fred = new Student();

int hours = fred.getHours();

Page 18: Calling Methods. Review  We can declare and create objects: Dinosaur dino; dino = new Dinosaur();  We can also shortcut the first two lines: Dinosaur

Missing stepsMissing steps

Student:

printHours

Student fred = new Student();

fred.printHours();

Page 19: Calling Methods. Review  We can declare and create objects: Dinosaur dino; dino = new Dinosaur();  We can also shortcut the first two lines: Dinosaur

Missing stepsMissing steps

If you are calling a method in the same object as it was written, you can skip the dot and name part:

printHours();int hours = getHours();

Page 20: Calling Methods. Review  We can declare and create objects: Dinosaur dino; dino = new Dinosaur();  We can also shortcut the first two lines: Dinosaur

But…But…

How do you know what the parts are? How do you know what the parts are? (There aren’t any pictures in the code)(There aren’t any pictures in the code)

Look at where the method is written for Look at where the method is written for the method HEADER:the method HEADER:

Page 21: Calling Methods. Review  We can declare and create objects: Dinosaur dino; dino = new Dinosaur();  We can also shortcut the first two lines: Dinosaur

Java MethodsJava Methods

public int average(int num1, int num2){public int average(int num1, int num2){int result = 0;int result = 0;result = num1 + num2;result = num1 + num2;result = result/2;result = result/2;return result;return result;

}}

Modifiers OutputType Method

Name

Input TypesInput Names

Body

How to give output

Method header

Page 22: Calling Methods. Review  We can declare and create objects: Dinosaur dino; dino = new Dinosaur();  We can also shortcut the first two lines: Dinosaur

Method HeadersMethod Headers

public class Calculator{public class Calculator{

public int average(int num1, int num2){public int average(int num1, int num2){

Calculator

average

int (first number)

int (second number)int (average)

Page 23: Calling Methods. Review  We can declare and create objects: Dinosaur dino; dino = new Dinosaur();  We can also shortcut the first two lines: Dinosaur

Arguments or ParametersArguments or Parameters

The inputs to a method are called The inputs to a method are called argumentsarguments or or parametersparameters..

Parameters must appear in the order and Parameters must appear in the order and types specified at the top of the methodtypes specified at the top of the method

Page 24: Calling Methods. Review  We can declare and create objects: Dinosaur dino; dino = new Dinosaur();  We can also shortcut the first two lines: Dinosaur

OutputsOutputs The type of output for the method is given The type of output for the method is given

in the method header.in the method header. If the method has no output, its return type If the method has no output, its return type

is is voidvoid. . Printer:

printOut

Form (what to print)

public class Printer{

public void printOut(Form toPrint){

Page 25: Calling Methods. Review  We can declare and create objects: Dinosaur dino; dino = new Dinosaur();  We can also shortcut the first two lines: Dinosaur

Create the Method Create the Method HeadersHeaders

Your code will:Your code will: Average three integersAverage three integers Convert a Celsius temperature to FahrenheitConvert a Celsius temperature to Fahrenheit Draw a smiley faceDraw a smiley face Print any String any number of timesPrint any String any number of times Reverse a wordReverse a word Tell whether or not a number is evenTell whether or not a number is even Give the first three letters of a StringGive the first three letters of a String

Page 26: Calling Methods. Review  We can declare and create objects: Dinosaur dino; dino = new Dinosaur();  We can also shortcut the first two lines: Dinosaur

Practice calling methodsPractice calling methods Calculator:Calculator:

Average three integersAverage three integers Convert a Celsius temperature to FahrenheitConvert a Celsius temperature to Fahrenheit

Printer:Printer: Draw a smiley faceDraw a smiley face Print any String any number of timesPrint any String any number of times

Utility:Utility: Reverse a wordReverse a word Tell whether or not a number is evenTell whether or not a number is even Give the first three letters of a StringGive the first three letters of a String