Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "Introduction to Methods. How do we use Methods in Java? Let us start by creating one which displays “hello” on Dos Prompt."— Presentation transcript:

1 Introduction to Methods

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

3 Why it is not working? We are missing the start point of our program –The main method –Add the main method

4 Still Not working!!  Our main method does not have any code We need to call the new method, sayHello(), from the main method!

5 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

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

7 Can we add more methods? Let us add a method which says bye

8

9 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)

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

11 What about numbers? Yes you can pass numbers like everything else as long as the method being called is expecting a number

12 What about displaying a real number?

13 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

14 Can we have more than one method parameter? Yes – methods can receive as many parameters as they require. The following has 2 parameters

15 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

16 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

17 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

18 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

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

20 Let us program! Write a method which returns the vowels within a string –Example for “Hello” will return “eo”

21 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

22 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

23 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:

24 Let us display each character first Create the following class:

25 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

26 Using case

27 Using If

28 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

29 The final touch

30 Was it tough? THE MAGIC WORD IS PRACTICE

31 Questions?


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

Similar presentations


Ads by Google