Introduction to Methods. How do we use Methods in Java? Let us start by creating one which displays...

31
Introductio n to Methods

Transcript of Introduction to Methods. How do we use Methods in Java? Let us start by creating one which displays...

Page 1: Introduction to Methods. How do we use Methods in Java? Let us start by creating one which displays “hello” on Dos Prompt.

Introduction to Methods

Page 2: Introduction to Methods. How do we use Methods in Java? Let us start by creating one which displays “hello” on Dos Prompt.

How do we use Methods in Java?

Let us start by creating one which displays “hello” on Dos Prompt

Page 3: Introduction to Methods. How do we use Methods in Java? Let us start by creating one which displays “hello” on Dos Prompt.

Why it is not working?

• We are missing the start point of our program– The main method– Add the main method

Page 4: Introduction to Methods. How do we use Methods in Java? Let us start by creating one which displays “hello” on Dos Prompt.

Still Not working!!

• Our main method does not have any code• We need to call the new method,

sayHello(), from the main method!

Page 5: Introduction to Methods. How do we use Methods in Java? Let us start by creating one which displays “hello” on Dos Prompt.

Voila! Is this magic????

• How is this possible?• The program first goes to the main method

by default and executes the code that there is in the main method().

• In this case the main method is pointing to another method sayHello() and executes the code in that method.

• A method which is not referenced from the main method does not execute on its own

Page 6: Introduction to Methods. How do we use Methods in Java? Let us start by creating one which displays “hello” on Dos Prompt.

So is println a method?

• Yes• The System class has a method called

println amongst others– These methods are used to display text in the

dos screen amongst other functionalities

The parseInt and parseDouble from Integer and Double classes respectively are methods too.

Page 7: Introduction to Methods. How do we use Methods in Java? Let us start by creating one which displays “hello” on Dos Prompt.

Can we add more methods?

• Let us add a method which says bye

Page 8: Introduction to Methods. How do we use Methods in Java? Let us start by creating one which displays “hello” on Dos Prompt.
Page 9: Introduction to Methods. How do we use Methods in Java? Let us start by creating one which displays “hello” on Dos Prompt.

Is there a limit on how many methods I can have in a class?

• Theoritically speaking no• But ideally we should group methods into

classes in which they naturally fit– Like the Integer class has the methods

belonging to integers– Like the Double class has the methods

belonging to double (real numbers)

Page 10: Introduction to Methods. How do we use Methods in Java? Let us start by creating one which displays “hello” on Dos Prompt.

Method Parameters

• Can we pass information to a method, like the name we want to display?– Yes– These are called method parameters

Page 11: Introduction to Methods. How do we use Methods in Java? Let us start by creating one which displays “hello” on Dos Prompt.

What about numbers?

• Yes you can pass numbers like everything else as long as the method being called is expecting a number

Page 12: Introduction to Methods. How do we use Methods in Java? Let us start by creating one which displays “hello” on Dos Prompt.

What about displaying a real number?

Page 13: Introduction to Methods. How do we use Methods in Java? Let us start by creating one which displays “hello” on Dos Prompt.

How does Java know?

• How does Java know which method to execute?– This is called METHOD OVERLOADING –

two methods with the same name but different signatures are found in the same class

– The first method is expecting an int while the second is expecting a double

– Like in jigsaw puzzles only the right piece fits in its place, the method matching the parameters will be executed

Page 14: Introduction to Methods. How do we use Methods in Java? Let us start by creating one which displays “hello” on Dos Prompt.

Can we have more than one method parameter?

• Yes – methods can receive as many parameters as they require.

• The following has 2 parameters

Page 15: Introduction to Methods. How do we use Methods in Java? Let us start by creating one which displays “hello” on Dos Prompt.

Can we have 2 parameters with the same name?

• No – all parameters must have a unique name

• Furthermore, you cannot have a variable within a method which shares the same name with one of the parameters

Page 16: Introduction to Methods. How do we use Methods in Java? Let us start by creating one which displays “hello” on Dos Prompt.

Let us program

• Let us create a program where we pass 2 integers and the program will tell us which is the largest of the 2 integers

Page 17: Introduction to Methods. How do we use Methods in Java? Let us start by creating one which displays “hello” on Dos Prompt.

How?

• Let us create a method called larger which takes 2 parameters of type int.

• This method will use a boolean operator to check which is the largest

• From the main method we call this method and pass 2 different numbers

Page 18: Introduction to Methods. How do we use Methods in Java? Let us start by creating one which displays “hello” on Dos Prompt.

Methods are typed as everything else –

this method is of type int

The return keyword stops the method

being executed and returns the value

which follows

The values of a and b in the main

method are copied in the method

parameters a and b respectively

Page 19: Introduction to Methods. How do we use Methods in Java? Let us start by creating one which displays “hello” on Dos Prompt.

Can we return more than 1 value?

• No• A method can only return one value• But you can create complex objects which

hold all the information you would like to return. More about objects later in the course.

Page 20: Introduction to Methods. How do we use Methods in Java? Let us start by creating one which displays “hello” on Dos Prompt.

Let us program!

• Write a method which returns the vowels within a string– Example for “Hello” will return “eo”

Page 21: Introduction to Methods. How do we use Methods in Java? Let us start by creating one which displays “hello” on Dos Prompt.

Let us understand the problem first

• We need to check every character that the given string has and– Ignore them if they are consonants– Save them if they are vowels

– A loop can be used to go through each character to determine which is a vowel and which is not

Page 22: Introduction to Methods. How do we use Methods in Java? Let us start by creating one which displays “hello” on Dos Prompt.

Which loop

• Java has 3 loops• The for loop is ideal for this problem as it

has a built-in counter which we require• Assume that the string variable is called

input

Page 23: Introduction to Methods. How do we use Methods in Java? Let us start by creating one which displays “hello” on Dos Prompt.

What is the function of the method length?

• The string method length returns the number of characters the string has– This method will return 5 if the string was

“hello”– Try out the following code:

Page 24: Introduction to Methods. How do we use Methods in Java? Let us start by creating one which displays “hello” on Dos Prompt.

Let us display each character first

• Create the following class:

Page 25: Introduction to Methods. How do we use Methods in Java? Let us start by creating one which displays “hello” on Dos Prompt.

Now we need to check the vowels

• We can either use a switch statement OR• We can use an if statement

• It is better to use a switch since we are matching values not checking for ranges

Page 26: Introduction to Methods. How do we use Methods in Java? Let us start by creating one which displays “hello” on Dos Prompt.

Using case

Page 27: Introduction to Methods. How do we use Methods in Java? Let us start by creating one which displays “hello” on Dos Prompt.

Using If

Page 28: Introduction to Methods. How do we use Methods in Java? Let us start by creating one which displays “hello” on Dos Prompt.

How can we return all vowels?

• I want to return the String of all the vowels concatenated to each other not display each vowel one by one

• Create an empty string• Concatenate each vowel to the String• Return the String

Page 29: Introduction to Methods. How do we use Methods in Java? Let us start by creating one which displays “hello” on Dos Prompt.

The final touch

Page 30: Introduction to Methods. How do we use Methods in Java? Let us start by creating one which displays “hello” on Dos Prompt.

Was it tough?

THE MAGIC WORD IS

PRACTICE

Page 31: Introduction to Methods. How do we use Methods in Java? Let us start by creating one which displays “hello” on Dos Prompt.

Questions?