Difference between revisions of "Java/C2/Methods/English-timed"
From Script | Spoken-Tutorial
Sandhya.np14 (Talk | contribs) |
|||
Line 5: | Line 5: | ||
|- | |- | ||
| 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 | + | | Here we are using, |
|- | |- | ||
|00:14 | |00:14 | ||
− | | | + | |* '''Ubuntu version 11.10''' |
|- | |- | ||
|00:17 | |00:17 | ||
− | | | + | |* '''Java Development kit 1.6''' and |
|- | |- | ||
|00:20 | |00:20 | ||
− | | | + | |* '''Eclipse 3.7.0''' |
− | + | ||
− | + | ||
|- | |- | ||
| 00:24 | | 00:24 | ||
− | | To follow this tutorial you must know how to write, compile and run a simple java program in eclipse. | + | | 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 | + | | 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. | + | | So, in the '''eclipse,''' I have already created a project '''Methods'''. |
|- | |- | ||
| 00:57 | | 00:57 | ||
− | | | + | | In the project, I have created a java class name '''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 | + | | So, type: '''void''' name of the '''method''' |
|- | |- | ||
|01:19 | |01:19 | ||
− | | Let us name it as | + | | Let us name it as '''displayMessage''' parentheses '''Enter'''. |
|- | |- | ||
|01:29 | |01:29 | ||
− | | And | + | | 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. | + | | But if you don’t want the method to return a value then the keyword '''void''' is used. |
|- | |- | ||
| 01:42 | | 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 | + | | So, type: '''System '''dot''' out '''dot '''println Hello Method'''. |
|- | |- | ||
| 02:06 | | 02:06 | ||
− | | So we have written a method. | + | | 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'''. | + | | So, inside the '''Main''' method, we will create an object of the '''class''' '''MethodDemo'''. |
|- | |- | ||
|02:21 | |02:21 | ||
− | | So | + | | So, '''MethodDemo''' object name. |
|- | |- | ||
|02:26 | |02:26 | ||
− | | Let's name it as '''md =new ''' '''MethodDemo | + | | Let's name it as '''md =new ''' '''MethodDemo''' parentheses semicolon. |
|- | |- | ||
| 02:37 | | 02:37 | ||
− | | So we have created an object '''md'''of the class '''MethodDemo''' using the '''New''' operator. | + | | So, we have created an object '''md''' of the class '''MethodDemo''' using the '''New''' operator. |
|- | |- | ||
Line 128: | Line 121: | ||
|- | |- | ||
| 02:51 | | 02:51 | ||
− | | So type | + | | So, type: '''md''' dot '''displayMessage'''. |
− | + | ||
− | + | ||
|- | |- | ||
| 03:00 | | 03:00 | ||
− | | The ''' | + | | The '''dot''' operator is used to '''call''' the '''method'''. |
|- | |- | ||
| 03:06 | | 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 | + | | So, type: '''int'''. |
|- | |- | ||
|03:32 | |03:32 | ||
− | | Also make the method '''public''', that is accessible everywhere. | + | | 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. | | 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 | + | | Now, inside the method type '''return''' seven semicolon. |
|- | |- | ||
Line 176: | Line 165: | ||
|- | |- | ||
| 04:08 | | 04:08 | ||
− | | | + | | Now, inside the '''Main''' method, at the end, type the 'print' statement. |
|- | |- | ||
|04:15 | |04:15 | ||
− | | So type '''System | + | | So, type: '''System''' dot '''out''' dot '''println();''' |
|- | |- | ||
| 04:23 | | 04:23 | ||
− | | Within '' | + | | Within parentheses, we will '''call''' the '''method'''. |
|- | |- | ||
|04:28 | |04:28 | ||
− | | So put | + | | So, put '''md''' dot '''method''' inside the parentheses, remove the semicolon. |
|- | |- | ||
Line 208: | Line 197: | ||
|- | |- | ||
|04:59 | |04:59 | ||
− | | So type | + | | So, type: '''public void ''' method name '''square ''' within parentheses '''int a'''. |
|- | |- | ||
Line 216: | Line 205: | ||
|- | |- | ||
| 05:20 | | 05:20 | ||
− | | Now within | + | | Now, within curly brackets type: '''System '''dot '''out '''dot '''println''' within parentheses '''a''' into '''a'''. |
|- | |- | ||
Line 232: | Line 221: | ||
|- | |- | ||
|05:53 | |05:53 | ||
− | | So type | + | | So, type: '''square''' within parentheses an integer '''5''' semicolon. |
|- | |- | ||
Line 244: | Line 233: | ||
|- | |- | ||
|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. | + | | The starting point is the '''Main''' '''method'''. |
|- | |- | ||
Line 292: | Line 281: | ||
|- | |- | ||
| 07:20 | | 07:20 | ||
− | | Since there are no statements left to execute, in the '''main''' method the application terminates | + | | Since there are no statements left to execute, in the '''main''' method, the application terminates. |
|- | |- | ||
| 07:29 | | 07:29 | ||
− | | Alright | + | | Alright! Now let us make '''displayMessage''' as '''static'''. |
|- | |- | ||
|07:35 | |07:35 | ||
− | | So after | + | | So, after '''public''' type '''static.''' |
|- | |- | ||
| 07:40 | | 07:40 | ||
− | | We see that we cannot call a non static method inside the '''static''' method | + | | We see that we cannot call a non static method inside the '''static''' method. |
|- | |- | ||
| 07:47 | | 07:47 | ||
− | | So we will comment this call. | + | | So we will '''comment''' this '''call'''. |
|- | |- | ||
| 07:52 | | 07:52 | ||
− | | Since '''Main''' is a static method, we can call the '''static displayMessage '''inside this | + | | Since '''Main''' is a static method, we can call the '''static displayMessage '''inside this. |
|- | |- | ||
| 08:02 | | 08:02 | ||
− | | Now for static method we do not need to create an object. | + | | Now for '''static method''', we do not need to create an object. |
|- | |- | ||
|08:07 | |08:07 | ||
− | | So we will comment this object creation. | + | | So we will '''comment''' this object creation. |
|- | |- | ||
| 08:11 | | 08:11 | ||
− | | Also we will | + | | Also we will delete '''md.''' |
|- | |- | ||
Line 336: | Line 325: | ||
|- | |- | ||
|08:27 | |08:27 | ||
− | | We do not see 25 because we have commented the call to square method | + | | We do not see 25 because we have commented the call to '''square''' method. |
|- | |- | ||
Line 344: | Line 333: | ||
|- | |- | ||
|08:38 | |08:38 | ||
− | | For that I have created a class '''Demo.''' | + | | For that, I have created a class '''Demo.''' |
|- | |- | ||
| 08:45 | | 08:45 | ||
− | | Inside the class create a method. | + | | Inside the '''class''', create a '''method'''. |
|- | |- | ||
|08:48 | |08:48 | ||
− | | So type | + | | 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 369: | Line 357: | ||
|- | |- | ||
|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. |
|- | |- | ||
Line 385: | Line 373: | ||
|- | |- | ||
|09:38 | |09:38 | ||
− | | So type '''Demo d=new Demo | + | | So, type: '''Demo d=new Demo''' parentheses semicolon. |
|- | |- | ||
Line 397: | Line 385: | ||
|- | |- | ||
|09:58 | |09:58 | ||
− | | We see on the console '''I am from other class'''. | + | | We see on the console: '''I am from other class'''. |
|- | |- | ||
|10:04 | |10:04 | ||
− | | This is how | + | | This is how '''method'''s 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 | + | | 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. | + | | | For self assessment, create a '''method''' which prints the cube of an integer. |
|- | |- | ||
| 10:38 | | 10:38 | ||
− | | To know more about the Spoken Tutorial Project | + | | To know more about the Spoken Tutorial Project, |
|- | |- | ||
|10:41 | |10:41 | ||
− | | Watch the video available at [http://spoken- | + | | Watch the video available at [http://spoken-tutorial.org/What_is_a_Spoken_Tutorial] |
|- | |- | ||
|10:47 | |10:47 | ||
− | | It summarizes the Spoken Tutorial project | + | | It summarizes the Spoken Tutorial project. |
|- | |- | ||
|10:50 | |10:50 | ||
− | | If you do not have good bandwidth, you can download and watch it | + | | If you do not have good bandwidth, you can download and watch it. |
− | + | ||
− | + | ||
|- | |- | ||
| 10:54 | | 10:54 | ||
− | | The Spoken Tutorial | + | | The Spoken Tutorial project team: |
|- | |- | ||
|10:56 | |10:56 | ||
− | | Conducts workshops using spoken tutorials | + | | 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 | | For more details, please write to contact@spoken-tutorial.org | ||
− | |||
− | |||
|- | |- | ||
| 11:08 | | 11:08 | ||
− | | Spoken Tutorial | + | | 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 | + | | It is supported by the National Mission on Education through ICT, MHRD, Government of India. |
|- | |- | ||
|11:18 | |11:18 | ||
− | | More information on this | + | | More information on this mission is available at |
− | + | [http://spoken-tutorial.org/NMEICT-Intro] | |
− | + | ||
− | + | ||
|- | |- | ||
Line 490: | Line 470: | ||
|- | |- | ||
|11:30 | |11:30 | ||
− | | This is Prathamesh Salunke signing off. | + | | This is Prathamesh Salunke, signing off. |
Jai Hind. | Jai Hind. | ||
|} | |} |
Revision as of 11:47, 31 March 2015
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, |
00:14 | * 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 name 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 methd 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:22 | 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 [1] |
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 |
11:27 | We have come to the end of this tutorial. |
11:29 | Thanks for joining. |
11:30 | This is Prathamesh Salunke, signing off.
Jai Hind. |
Contributors and Content Editors
Arya Ratish, Gaurav, Jyotisolanki, PoojaMoolya, Priyacst, Sandhya.np14, Sneha