Introduction to Methods. How do we use Methods in Java? Let us start by creating one which displays...
-
Upload
lilian-osborne -
Category
Documents
-
view
218 -
download
0
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.](https://reader035.fdocuments.us/reader035/viewer/2022062421/56649da75503460f94a93b86/html5/thumbnails/1.jpg)
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.](https://reader035.fdocuments.us/reader035/viewer/2022062421/56649da75503460f94a93b86/html5/thumbnails/2.jpg)
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.](https://reader035.fdocuments.us/reader035/viewer/2022062421/56649da75503460f94a93b86/html5/thumbnails/3.jpg)
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.](https://reader035.fdocuments.us/reader035/viewer/2022062421/56649da75503460f94a93b86/html5/thumbnails/4.jpg)
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.](https://reader035.fdocuments.us/reader035/viewer/2022062421/56649da75503460f94a93b86/html5/thumbnails/5.jpg)
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.](https://reader035.fdocuments.us/reader035/viewer/2022062421/56649da75503460f94a93b86/html5/thumbnails/6.jpg)
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.](https://reader035.fdocuments.us/reader035/viewer/2022062421/56649da75503460f94a93b86/html5/thumbnails/7.jpg)
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.](https://reader035.fdocuments.us/reader035/viewer/2022062421/56649da75503460f94a93b86/html5/thumbnails/8.jpg)
![Page 9: Introduction to Methods. How do we use Methods in Java? Let us start by creating one which displays “hello” on Dos Prompt.](https://reader035.fdocuments.us/reader035/viewer/2022062421/56649da75503460f94a93b86/html5/thumbnails/9.jpg)
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.](https://reader035.fdocuments.us/reader035/viewer/2022062421/56649da75503460f94a93b86/html5/thumbnails/10.jpg)
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.](https://reader035.fdocuments.us/reader035/viewer/2022062421/56649da75503460f94a93b86/html5/thumbnails/11.jpg)
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.](https://reader035.fdocuments.us/reader035/viewer/2022062421/56649da75503460f94a93b86/html5/thumbnails/12.jpg)
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.](https://reader035.fdocuments.us/reader035/viewer/2022062421/56649da75503460f94a93b86/html5/thumbnails/13.jpg)
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.](https://reader035.fdocuments.us/reader035/viewer/2022062421/56649da75503460f94a93b86/html5/thumbnails/14.jpg)
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.](https://reader035.fdocuments.us/reader035/viewer/2022062421/56649da75503460f94a93b86/html5/thumbnails/15.jpg)
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.](https://reader035.fdocuments.us/reader035/viewer/2022062421/56649da75503460f94a93b86/html5/thumbnails/16.jpg)
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.](https://reader035.fdocuments.us/reader035/viewer/2022062421/56649da75503460f94a93b86/html5/thumbnails/17.jpg)
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.](https://reader035.fdocuments.us/reader035/viewer/2022062421/56649da75503460f94a93b86/html5/thumbnails/18.jpg)
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.](https://reader035.fdocuments.us/reader035/viewer/2022062421/56649da75503460f94a93b86/html5/thumbnails/19.jpg)
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.](https://reader035.fdocuments.us/reader035/viewer/2022062421/56649da75503460f94a93b86/html5/thumbnails/20.jpg)
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.](https://reader035.fdocuments.us/reader035/viewer/2022062421/56649da75503460f94a93b86/html5/thumbnails/21.jpg)
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.](https://reader035.fdocuments.us/reader035/viewer/2022062421/56649da75503460f94a93b86/html5/thumbnails/22.jpg)
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.](https://reader035.fdocuments.us/reader035/viewer/2022062421/56649da75503460f94a93b86/html5/thumbnails/23.jpg)
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.](https://reader035.fdocuments.us/reader035/viewer/2022062421/56649da75503460f94a93b86/html5/thumbnails/24.jpg)
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.](https://reader035.fdocuments.us/reader035/viewer/2022062421/56649da75503460f94a93b86/html5/thumbnails/25.jpg)
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.](https://reader035.fdocuments.us/reader035/viewer/2022062421/56649da75503460f94a93b86/html5/thumbnails/26.jpg)
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.](https://reader035.fdocuments.us/reader035/viewer/2022062421/56649da75503460f94a93b86/html5/thumbnails/27.jpg)
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.](https://reader035.fdocuments.us/reader035/viewer/2022062421/56649da75503460f94a93b86/html5/thumbnails/28.jpg)
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.](https://reader035.fdocuments.us/reader035/viewer/2022062421/56649da75503460f94a93b86/html5/thumbnails/29.jpg)
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.](https://reader035.fdocuments.us/reader035/viewer/2022062421/56649da75503460f94a93b86/html5/thumbnails/30.jpg)
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.](https://reader035.fdocuments.us/reader035/viewer/2022062421/56649da75503460f94a93b86/html5/thumbnails/31.jpg)
Questions?