Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "Calling Methods. Review  We can declare and create objects: Dinosaur dino; dino = new Dinosaur();  We can also shortcut the first two lines: Dinosaur."— Presentation transcript:

1 Calling Methods

2 Review  We can declare and create objects: Dinosaur dino; dino = new Dinosaur();  We can also shortcut the first two lines: Dinosaur dino = new Dinosaur();  We can declare and create primitives: double myArea = 54.2*24;

3 Methods  Another name for “Behaviors”  Methods are small pieces of code that can be used in other pieces of code.  They have 0 or more inputs, and 0 or 1 output.

4 Why Methods?  Allows you to break up a hard problem into smaller, more manageable parts  Makes your code easier to understand  Makes part of the code reusable so that you:  Only have to type it out once  Can debug it all at once  Can make changes in one place

5 How to use methods  First, you must know what behaviors a class has.  You need:  An object that has the right behavior  The name of the behavior  What inputs it needs, if any  What output it gives, if any

6 Example 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.

7 Call 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();

8 Four easy steps: 1. 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 =

9 Four easy steps: 2. 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.

10 Four easy steps: 3. 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();

11 Four easy steps: 4. 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);

12 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.

13 Did you get it? Calculator: sqRoot int (any number) double (square root of original number) Calculator calc = new Calculator(); double root = calc.sqRoot(14);

14 Objects as input and output 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)

15 Objects as input and output 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)

16 Missing steps Printer: printOut Form (what to print) Form register = new Form(); Printer myHP = new Printer(); myHP.printOut(register);

17 Missing steps Student: getHours int (number of credit hours complete) Student fred = new Student(); int hours = fred.getHours();

18 Missing steps Student: printHours Student fred = new Student(); fred.printHours();

19 Missing 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();

20 But…  How do you know what the parts are? (There aren’t any pictures in the code)  Look at where the method is written for the method HEADER:

21 Java Methods public int average(int num1, int num2){ int result = 0; result = num1 + num2; result = result/2; return result; } ModifiersOutput Type Method Name Input Types Input Names Body How to give output Method header

22 Method Headers public class Calculator{ public int average(int num1, int num2){ Calculator average int (first number) int (second number) int (average)

23 Arguments or Parameters  The inputs to a method are called arguments or parameters.  Parameters must appear in the order and types specified at the top of the method

24 Outputs  The type of output for the method is given in the method header.  If the method has no output, its return type is void. Printer: printOut Form (what to print) public class Printer{ public void printOut(Form toPrint){

25 Create the Method Headers  Your code will:  Average three integers  Convert a Celsius temperature to Fahrenheit  Draw a smiley face  Print any String any number of times  Reverse a word  Tell whether or not a number is even  Give the first three letters of a String

26 Practice calling methods  Calculator:  Average three integers  Convert a Celsius temperature to Fahrenheit  Printer:  Draw a smiley face  Print any String any number of times  Utility:  Reverse a word  Tell whether or not a number is even  Give the first three letters of a String


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

Similar presentations


Ads by Google