Difference between revisions of "Java/C2/Methods/English-timed"
From Script | Spoken-Tutorial
PoojaMoolya (Talk | contribs) |
|||
(7 intermediate revisions by 4 users not shown) | |||
Line 2: | Line 2: | ||
|| '''Time''' | || '''Time''' | ||
|| '''Narration''' | || '''Narration''' | ||
− | |||
|- | |- | ||
− | | 00:02 | + | |00:02 |
− | | Welcome to the Spoken Tutorial on '''methods | + | | Welcome to the Spoken Tutorial on '''methods''' in java. |
|- | |- | ||
|00:06 | |00:06 | ||
− | | | + | |In this tutorial, we will learn: |
|- | |- | ||
|00:08 | |00:08 | ||
− | | | + | |To create a '''method'''. |
|- | |- | ||
|00:10 | |00:10 | ||
− | | And | + | |And to '''call''' a '''method'''. |
|- | |- | ||
− | | 00:13 | + | |00:13 |
− | | | + | |Here we are using,'''Ubuntu version 11.10''' |
− | |||
− | |||
− | |||
|- | |- | ||
|00:17 | |00:17 | ||
− | | | + | |'''Java Development kit 1.6''' and |
+ | |||
|- | |- | ||
|00:20 | |00:20 | ||
− | | | + | |'''Eclipse 3.7.0''' |
− | + | ||
− | + | ||
|- | |- | ||
− | | | + | | 00:24 |
− | | | + | | To follow this tutorial, you must know how to write, compile and run a simple java program in '''eclipse'''. |
− | + | ||
|- | |- | ||
|00:32 | |00:32 | ||
− | | | + | |If not, for relevant tutorials, please visit our website which is as shown. |
− | + | http://www.spoken-tutorial.org | |
− | + | ||
|- | |- | ||
|00:40 | |00:40 | ||
− | + | |A java '''method''' is a collection of statements that performs a specified operation. | |
− | + | ||
− | |A java method is a collection of statements that performs a specified operation. | + | |
|- | |- | ||
− | | 00:46 | + | |00:46 |
− | |Let us now write a method. | + | |Let us now write a '''method'''. |
− | + | ||
|- | |- | ||
|00:50 | |00:50 | ||
− | | | + | |So, in the '''eclipse,''' I have already created a project '''Methods'''. |
|- | |- | ||
− | | | + | |00:57 |
− | | | + | |In the project, I have created a java class named '''MethodDemo'''. |
|- | |- | ||
− | | 01:06 | + | |01:06 |
− | | In the class outside the main method we will write a method. | + | |In the '''class''', outside the '''main method''', we will write a '''method'''. |
− | + | ||
|- | |- | ||
|01:13 | |01:13 | ||
− | | | + | |So, type: '''void''' name of the '''method''' |
|- | |- | ||
|01:19 | |01:19 | ||
− | | | + | |Let us name it as '''displayMessage''' parentheses '''Enter'''. |
|- | |- | ||
|01:29 | |01:29 | ||
− | | | + | |And curly brackets. |
|- | |- | ||
− | | 01:32 | + | |01:32 |
− | | A method can return a value. | + | |A '''method''' can return a value. |
|- | |- | ||
|01:34 | |01:34 | ||
− | | | + | |But if you don’t want the method to return a value then the keyword '''void''' is used. |
|- | |- | ||
− | | | + | |01:42 |
− | | Alright now inside the curly brackets, let us print a message. | + | |Alright, now inside the curly brackets, let us print a message. |
|- | |- | ||
|01:47 | |01:47 | ||
− | | | + | |So, type: '''System '''dot''' out '''dot '''println Hello Method'''. |
|- | |- | ||
− | | 02:06 | + | |02:06 |
− | | | + | |So, we have written a '''method'''. |
|- | |- | ||
|02:10 | |02:10 | ||
− | |Now we will call this method. | + | |Now, we will '''call''' this '''method'''. |
|- | |- | ||
|02:13 | |02:13 | ||
− | | | + | |So, inside the '''main''' method, we will create an object of the '''class''' '''MethodDemo'''. |
|- | |- | ||
|02:21 | |02:21 | ||
− | | | + | |So, '''MethodDemo''' object name. |
|- | |- | ||
|02:26 | |02:26 | ||
− | | | + | |Let's name it as '''md =new ''' '''MethodDemo''' parentheses semicolon. |
|- | |- | ||
− | | | + | |02:37 |
− | | | + | |So, we have created an object '''md''' of the class '''MethodDemo''' using the '''new''' operator. |
|- | |- | ||
− | | | + | |02:48 |
− | | Now let us call the method '''displayMessage.''' | + | | Now, let us call the method '''displayMessage.''' |
|- | |- | ||
− | | | + | | 02:51 |
− | | So type | + | | So, type: '''md''' dot '''displayMessage();'''. |
− | + | ||
− | + | ||
|- | |- | ||
− | | | + | |03:00 |
− | | | + | |The 'dot' operator is used to '''call''' the '''method'''. |
|- | |- | ||
− | | | + | | 03:06 |
− | | Now let us '''Run''' this application by clicking on '''Run'''icon. | + | | Now, let us '''Run''' this application by clicking on '''Run''' icon. |
|- | |- | ||
| 03:14 | | 03:14 | ||
− | | We see the output '''Hello | + | | We see the output '''Hello Method '''on the '''console'''. |
|- | |- | ||
| 03:20 | | 03:20 | ||
− | |Now | + | |Now, let us return an '''integer ''' instead of '''void'''. |
|- | |- | ||
|03:26 | |03:26 | ||
− | | | + | | So, type: '''int'''. |
|- | |- | ||
|03:32 | |03:32 | ||
− | | | + | |Also, make the method '''public''', that is accessible everywhere. |
|- | |- | ||
|03:37 | |03:37 | ||
− | | | + | |By default, it is '''private''', that is accessible only within the class where it is written. |
− | + | ||
− | + | ||
|- | |- | ||
− | | 03:45 | + | |03:45 |
− | | | + | |Now, inside the method, type '''return''' seven semicolon. |
|- | |- | ||
− | | 03:55 | + | |03:55 |
− | | Remember that we write the '''return''' statement at the end of all in the method. | + | |Remember that we write the '''return''' statement at the end of all statements in the '''method'''. |
|- | |- | ||
|04:02 | |04:02 | ||
− | | | + | |This is because after '''return''' statement no other statements are executed. |
|- | |- | ||
− | | | + | |04:08 |
− | | | + | |Now, inside the '''main''' method, at the end, type the 'print' statement. |
|- | |- | ||
|04:15 | |04:15 | ||
− | | | + | |So, type: '''System''' dot '''out''' dot '''println();''' |
|- | |- | ||
− | | | + | |04:23 |
− | | | + | |Within parentheses, we will '''call''' the '''method'''. |
|- | |- | ||
|04:28 | |04:28 | ||
− | | | + | |So, put '''md''' dot '''method''' inside the parentheses, remove the semicolon. |
|- | |- | ||
|04:37 | |04:37 | ||
− | | | + | |This will print the '''return''' value of the method. |
|- | |- | ||
− | | | + | |04:42 |
− | | | + | |'''Run''' the application. |
|- | |- | ||
− | | 04:45 | + | |04:45 |
− | | We see in the output, the value '''7''' is printed. | + | |We see in the output, the value '''7''' is printed. |
|- | |- | ||
− | | | + | |04:51 |
− | | Now we will write another method and call this | + | |Now we will write another method and call this method in '''displayMessage.''' |
|- | |- | ||
|04:59 | |04:59 | ||
− | | | + | | So, type: '''public void ''' method name '''square ''' within parentheses '''int a'''. |
|- | |- | ||
− | | | + | |05:15 |
− | + | |Here, we are giving '''int a''' as a parameter to our method. | |
|- | |- | ||
− | | | + | | 05:20 |
− | | Now within | + | | Now, within curly brackets type: '''System '''dot '''out '''dot '''println''' within parentheses '''a''' into '''a'''. |
|- | |- | ||
| 05:37 | | 05:37 | ||
− | | | + | | So, we have written a '''square''' method |
|- | |- | ||
|05:40 | |05:40 | ||
− | | | + | | that will display a square of an integer which is given as a parameter. |
|- | |- | ||
− | | | + | |05:48 |
− | | Let us call this method in the '''displayMessage''' method. | + | |Let us call this method in the '''displayMessage''' method. |
|- | |- | ||
|05:53 | |05:53 | ||
− | | | + | | So, type: '''square''' within parentheses an integer '''5''' semicolon. |
|- | |- | ||
− | | | + | | 06:07 |
− | + | | '''Run''' this application. | |
|- | |- | ||
|06:12 | |06:12 | ||
− | | | + | |We see that the output displays the square of '''5''' that is '''25.''' |
|- | |- | ||
|06:19 | |06:19 | ||
− | | Now let us understand the flow of the application. | + | |Now, let us understand the flow of the application. |
|- | |- | ||
|06:24 | |06:24 | ||
− | | | + | |The starting point is the '''main''' '''method'''. |
|- | |- | ||
− | | 06:29 | + | |06:29 |
− | | | + | |In the '''Main '''method''', '''we have first called the '''displayMessage'''. |
|- | |- | ||
− | | | + | |06:34 |
− | | | + | |So, the control goes to the '''displayMessage''' |
|- | |- | ||
|06:40 | |06:40 | ||
− | | | + | |and all the statements in the '''displayMessage''' are executed. |
|- | |- | ||
|06:45 | |06:45 | ||
− | | | + | |The first one is the '''print''' statement. |
|- | |- | ||
− | | 06:50 | + | |06:50 |
− | | | + | |Then it comes across the '''square''' method. |
|- | |- | ||
− | | | + | |06:54 |
− | | | + | | So the control jumps to the '''square''' method. |
|- | |- | ||
|06:57 | |06:57 | ||
− | | | + | | The square method takes an '''integer 5''' and returns the square of the integer i.e. '''25'''. |
|- | |- | ||
| 07:06 | | 07:06 | ||
− | | | + | | Then the control goes back to the '''displayMessage'''. |
|- | |- | ||
− | | | + | | 07:10 |
|And it returns the value '''7'''. | |And it returns the value '''7'''. | ||
|- | |- | ||
| 07:14 | | 07:14 | ||
− | | Then the control jumps back to the ''' | + | | Then the control jumps back to the '''main''' function. |
|- | |- | ||
| 07:20 | | 07:20 | ||
− | | | + | | Since there are no statements left to execute, in the '''main''' method, the application terminates. |
|- | |- | ||
− | | | + | | 07:29 |
− | | Alright | + | | Alright! Now let us make '''displayMessage''' as '''static'''. |
|- | |- | ||
|07:35 | |07:35 | ||
− | | | + | | So, after '''public''' type '''static.''' |
|- | |- | ||
| 07:40 | | 07:40 | ||
− | | | + | | We see that we cannot call a 'non static' method inside the '''static''' method. |
|- | |- | ||
− | | | + | | 07:47 |
− | | So we will comment this call. | + | | So, we will '''comment''' this '''call'''. |
|- | |- | ||
| 07:52 | | 07:52 | ||
− | | Since ''' | + | | Since '''main''' is a static method, we can call the '''static displayMessage '''inside this. |
|- | |- | ||
− | | | + | |08:02 |
− | | | + | | Now, for '''static method''', we do not need to create an object. |
|- | |- | ||
|08:07 | |08:07 | ||
− | | | + | | So, we will '''comment''' this object creation. |
|- | |- | ||
− | | | + | | 08:11 |
− | | | + | | Also we will delete '''md.''' |
|- | |- | ||
− | | | + | |08:18 |
− | | | + | |'''Run''' the application. |
|- | |- | ||
|08:22 | |08:22 | ||
− | | | + | |We see the output '''Hello Method '''and '''7'''. |
|- | |- | ||
|08:27 | |08:27 | ||
− | | | + | |We do not see 25 because we have commented the call to '''square''' method. |
|- | |- | ||
− | | | + | |08:34 |
− | | We can also call method from other class. | + | |We can also call '''method''' from other '''class'''. |
|- | |- | ||
|08:38 | |08:38 | ||
− | | For that I have created a class '''Demo.''' | + | |For that, I have created a class '''Demo.''' |
|- | |- | ||
− | | | + | |08:45 |
− | | | + | |Inside the '''class''', create a '''method'''. |
|- | |- | ||
|08:48 | |08:48 | ||
− | | | + | |So, type: '''public void show''' parentheses '''Enter'''. |
|- | |- | ||
|08:56 | |08:56 | ||
− | | | + | |Inside curly brackets, '''System '''dot''' out '''dot''' println''' |
|- | |- | ||
|09:07 | |09:07 | ||
− | | | + | | '''I am from other class.''' |
− | + | ||
|- | |- | ||
Line 370: | Line 354: | ||
|- | |- | ||
|09:16 | |09:16 | ||
− | | Go back to | + | | Go back to '''MethodDemo''' class. |
|- | |- | ||
| 09:19 | | 09:19 | ||
− | | Now we will call this '''show''' method inside the method '''MethodDemo''' class. | + | | Now we will '''call''' this '''show''' method, inside the method '''MethodDemo''' class. |
|- | |- | ||
|09:28 | |09:28 | ||
− | | | + | | For that we need to create the object of the class '''Demo.''' |
|- | |- | ||
− | |09: | + | |09:32 |
− | | | + | |This is because the '''show''' method belongs to the class '''Demo.''' |
|- | |- | ||
|09:38 | |09:38 | ||
− | | | + | | So, type: '''Demo d=new Demo''' parentheses semicolon. |
|- | |- | ||
− | | 09:48 | + | |09:48 |
− | | | + | |Then call the method '''show'''. |
|- | |- | ||
− | | | + | | 09:54 |
− | | | + | | Lets '''Run''' this application. |
|- | |- | ||
|09:58 | |09:58 | ||
− | | | + | | We see on the console: '''I am from other class'''. |
|- | |- | ||
|10:04 | |10:04 | ||
− | | This is how methods are used in java. | + | | This is how '''methods''' are used in java. |
|- | |- | ||
|10:09 | |10:09 | ||
− | | The method name and the parameters | + | | The '''method''' name and the parameters form the signature of the '''method''' |
|- | |- | ||
|10:14 | |10:14 | ||
− | | | + | | while the curly brackets and the statements form the body of the '''method'''. |
|- | |- | ||
|10:23 | |10:23 | ||
− | | | + | | So, in this tutorial, we have learnt: |
|- | |- | ||
|10:25 | |10:25 | ||
− | | | + | |To create a '''method''' |
+ | |||
|- | |- | ||
|10:27 | |10:27 | ||
− | | | + | |To call a '''method''' |
+ | |||
|- | |- | ||
|10:29 | |10:29 | ||
− | | And | + | |And different signatures of '''methods'''. |
− | + | ||
− | + | ||
|- | |- | ||
| 10:32 | | 10:32 | ||
− | + | | For self assessment, create a '''method''' which prints the cube of an integer. | |
|- | |- | ||
− | | | + | | 10:38 |
− | | To know more about the Spoken Tutorial | + | | To know more about the Spoken Tutorial project, |
+ | |||
|- | |- | ||
|10:41 | |10:41 | ||
− | | | + | |watch the video available at http://spoken-tutorial.org/What_is_a_Spoken_Tutorial. |
+ | |||
|- | |- | ||
|10:47 | |10:47 | ||
− | | | + | |It summarizes the Spoken Tutorial project. |
|- | |- | ||
|10:50 | |10:50 | ||
− | | | + | |If you do not have good bandwidth, you can download and watch it. |
− | + | ||
− | + | ||
|- | |- | ||
− | | | + | |10:54 |
− | | | + | |The Spoken Tutorial project team: |
|- | |- | ||
|10:56 | |10:56 | ||
− | | | + | |Conducts workshops using spoken tutorials. |
|- | |- | ||
|10:58 | |10:58 | ||
− | | | + | |Gives certificates to those who have passed an online test. |
|- | |- | ||
|11:02 | |11:02 | ||
− | | | + | |For more details, please write to contact@spoken-tutorial.org |
− | + | ||
− | + | ||
|- | |- | ||
− | | 11:08 | + | |11:08 |
− | | | + | |Spoken Tutorial project is a part of the Talk to a Teacher project. |
|- | |- | ||
|11:12 | |11:12 | ||
− | | | + | |It is supported by the National Mission on Education through ICT, MHRD, Government of India. |
|- | |- | ||
|11:18 | |11:18 | ||
− | | | + | |More information on this mission is available at http://spoken-tutorial.org/NMEICT-Intro. |
− | + | ||
− | + | ||
− | + | ||
|- | |- | ||
− | | 11:27 | + | |11:27 |
− | | We have come to the end of this tutorial. | + | |We have come to the end of this tutorial. |
|- | |- | ||
|11:29 | |11:29 | ||
− | | | + | |Thanks for joining.This is Prathamesh Salunke, signing off. |
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
Jai Hind. | Jai Hind. | ||
|} | |} |
Latest revision as of 15:26, 28 March 2017
Time | Narration |
00:02 | Welcome to the Spoken Tutorial on methods in java. |
00:06 | In this tutorial, we will learn: |
00:08 | To create a method. |
00:10 | And to call a method. |
00:13 | Here we are using,Ubuntu version 11.10 |
00:17 | Java Development kit 1.6 and |
00:20 | Eclipse 3.7.0 |
00:24 | To follow this tutorial, you must know how to write, compile and run a simple java program in eclipse. |
00:32 | If not, for relevant tutorials, please visit our website which is as shown. |
00:40 | A java method is a collection of statements that performs a specified operation. |
00:46 | Let us now write a method. |
00:50 | So, in the eclipse, I have already created a project Methods. |
00:57 | In the project, I have created a java class named MethodDemo. |
01:06 | In the class, outside the main method, we will write a method. |
01:13 | So, type: void name of the method |
01:19 | Let us name it as displayMessage parentheses Enter. |
01:29 | And curly brackets. |
01:32 | A method can return a value. |
01:34 | But if you don’t want the method to return a value then the keyword void is used. |
01:42 | Alright, now inside the curly brackets, let us print a message. |
01:47 | So, type: System dot out dot println Hello Method. |
02:06 | So, we have written a method. |
02:10 | Now, we will call this method. |
02:13 | So, inside the main method, we will create an object of the class MethodDemo. |
02:21 | So, MethodDemo object name. |
02:26 | Let's name it as md =new MethodDemo parentheses semicolon. |
02:37 | So, we have created an object md of the class MethodDemo using the new operator. |
02:48 | Now, let us call the method displayMessage. |
02:51 | So, type: md dot displayMessage();. |
03:00 | The 'dot' operator is used to call the method. |
03:06 | Now, let us Run this application by clicking on Run icon. |
03:14 | We see the output Hello Method on the console. |
03:20 | Now, let us return an integer instead of void. |
03:26 | So, type: int. |
03:32 | Also, make the method public, that is accessible everywhere. |
03:37 | By default, it is private, that is accessible only within the class where it is written. |
03:45 | Now, inside the method, type return seven semicolon. |
03:55 | Remember that we write the return statement at the end of all statements in the method. |
04:02 | This is because after return statement no other statements are executed. |
04:08 | Now, inside the main method, at the end, type the 'print' statement. |
04:15 | So, type: System dot out dot println(); |
04:23 | Within parentheses, we will call the method. |
04:28 | So, put md dot method inside the parentheses, remove the semicolon. |
04:37 | This will print the return value of the method. |
04:42 | Run the application. |
04:45 | We see in the output, the value 7 is printed. |
04:51 | Now we will write another method and call this method in displayMessage. |
04:59 | So, type: public void method name square within parentheses int a. |
05:15 | Here, we are giving int a as a parameter to our method. |
05:20 | Now, within curly brackets type: System dot out dot println within parentheses a into a. |
05:37 | So, we have written a square method |
05:40 | that will display a square of an integer which is given as a parameter. |
05:48 | Let us call this method in the displayMessage method. |
05:53 | So, type: square within parentheses an integer 5 semicolon. |
06:07 | Run this application. |
06:12 | We see that the output displays the square of 5 that is 25. |
06:19 | Now, let us understand the flow of the application. |
06:24 | The starting point is the main method. |
06:29 | In the Main method, we have first called the displayMessage. |
06:34 | So, the control goes to the displayMessage |
06:40 | and all the statements in the displayMessage are executed. |
06:45 | The first one is the print statement. |
06:50 | Then it comes across the square method. |
06:54 | So the control jumps to the square method. |
06:57 | The square method takes an integer 5 and returns the square of the integer i.e. 25. |
07:06 | Then the control goes back to the displayMessage. |
07:10 | And it returns the value 7. |
07:14 | Then the control jumps back to the main function. |
07:20 | Since there are no statements left to execute, in the main method, the application terminates. |
07:29 | Alright! Now let us make displayMessage as static. |
07:35 | So, after public type static. |
07:40 | We see that we cannot call a 'non static' method inside the static method. |
07:47 | So, we will comment this call. |
07:52 | Since main is a static method, we can call the static displayMessage inside this. |
08:02 | Now, for static method, we do not need to create an object. |
08:07 | So, we will comment this object creation. |
08:11 | Also we will delete md. |
08:18 | Run the application. |
08:22 | We see the output Hello Method and 7. |
08:27 | We do not see 25 because we have commented the call to square method. |
08:34 | We can also call method from other class. |
08:38 | For that, I have created a class Demo. |
08:45 | Inside the class, create a method. |
08:48 | So, type: public void show parentheses Enter. |
08:56 | Inside curly brackets, System dot out dot println |
09:07 | I am from other class. |
09:13 | Save the file. |
09:16 | Go back to MethodDemo class. |
09:19 | Now we will call this show method, inside the method MethodDemo class. |
09:28 | For that we need to create the object of the class Demo. |
09:32 | This is because the show method belongs to the class Demo. |
09:38 | So, type: Demo d=new Demo parentheses semicolon. |
09:48 | Then call the method show. |
09:54 | Lets Run this application. |
09:58 | We see on the console: I am from other class. |
10:04 | This is how methods are used in java. |
10:09 | The method name and the parameters form the signature of the method |
10:14 | while the curly brackets and the statements form the body of the method. |
10:23 | So, in this tutorial, we have learnt: |
10:25 | To create a method |
10:27 | To call a method |
10:29 | And different signatures of methods. |
10:32 | For self assessment, create a method which prints the cube of an integer. |
10:38 | To know more about the Spoken Tutorial project, |
10:41 | watch the video available at http://spoken-tutorial.org/What_is_a_Spoken_Tutorial. |
10:47 | It summarizes the Spoken Tutorial project. |
10:50 | If you do not have good bandwidth, you can download and watch it. |
10:54 | The Spoken Tutorial project team: |
10:56 | Conducts workshops using spoken tutorials. |
10:58 | Gives certificates to those who have passed an online test. |
11:02 | For more details, please write to contact@spoken-tutorial.org |
11:08 | Spoken Tutorial project is a part of the Talk to a Teacher project. |
11:12 | It is supported by the National Mission on Education through ICT, MHRD, Government of India. |
11:18 | More information on this mission is available at http://spoken-tutorial.org/NMEICT-Intro. |
11:27 | We have come to the end of this tutorial. |
11:29 | Thanks for joining.This is Prathamesh Salunke, signing off.
Jai Hind. |
Contributors and Content Editors
Arya Ratish, Gaurav, Jyotisolanki, PoojaMoolya, Priyacst, Sandhya.np14, Sneha