Java/C2/Methods/English

From Script | Spoken-Tutorial
Revision as of 15:46, 27 November 2012 by Chandrika (Talk | contribs)

(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search

Title of script: methods in java.

Author: Prathamesh Salunke

Keywords: video tutorial, methods.


Visual Cue
Narration
Slide 1

Opening slide

Welcome to the Spoken Tutorial on methods java.
Slide 2

Learning Objectives

In this tutorial we will learn

* To create a method.

* To call a method.

Slide 3

System Requirements

Here we are using
  • Ubuntu version 11.10
  • Java Development Environment jdk 1.6 and
  • Eclipse 3.7


Slide 4

Prerequisites

To follow this tutorial you must know how to write, compile and run a simple java program in eclipse.


If not, for relevant tutorials please visit our website which is as shown,

(http://www.spoken-tutorial.org)

Slide 5

Method Definition

A java method is a collection of statements that performs a specified operation.
Switch to eclipse


Point to the Project Methods

Let us now write a method.


So, in the eclipse, I have already created a project Methods.

Point to the MethodDemo class In the project, I have created a java class MethodDemo
Type:

void displayMessage()

{}

In the class outside main method we will write a method.


So type void name of the method

Let us name it as displayMessage parentheses

And curly brackets.

Highlight void A method can return a value.

But if you don’t want the method to return a value then keyword void is used.

== System.out.println(“Hello Method”); == Now inside the curly brackets, let us print a message.

So type System dot out dot println Hello Method.

Type :

Methods md=new Methods();

So we have written a method.

Let us now call this method.

So inside Main method, let us create an object of the class.

So type class name i.e MethodDemo name of the object.

Let us name it as md=new MethodDemo parentheses, semicolon.

Highlight new So we have created an object of the class MethodDemo using the New operator.
So now let us call the method displayMessage.
Type:

md.displayMessage();

So type md dot displayMessage parentheses, semicolon.



Highlight Dot operator. The Dot operator is used to call the method.
Click on Run icon Let us Run the application by clicking on Run icon.
Output:

Hello Method

We see the output Hello Method on the console
Replace void by int Let us now return an integer value.

So in place of void type int.

Type public Also make the method public, that is accessible everywhere.

By default it is private, that is accessible only within the class where it is written.

So type public.

Type:

return 7;

Now inside the method type return seven, semicolon.
Highlight return Remember return is written at the end of all statements in the method.

This is because after return statement no other statements are executed.

Type:

System.out.println();

Now inside the Main method at the end type print statement.

So System dot out dot println();

System.out.println(displayMethod());


Inside parenthesis we will call the method.

So put md dot method inside the parentheses and remove the semi-colon.

This will print the return value of the method.

Click on Run icon Run the application.
Output:

Hello Method

7

We see in the output, the value 7 is printed.
Type:

public void square(int a)


Now we will write another method and call it in displayMessage.

So type public void method name square within parentheses int a.

Highlight int a Here we are giving int a as a parameter to our method.
Type:

{

System.out.println(a*a);

}

Now within curly brackets, System dot out dot println within parentheses a into a.
Highlight square() method So we have written a square method.

It will display a square of the integer given as a parameter.

In the displayMessage

Type:

square(5);

Let us call this method in displayMessage.

So type square within parentheses an integer 5, semicolon.

Click on Run icon Run the application.

We see that the output displays the square of 5 that is 25.

Point to Main method Let us understand the flow of the application.

The starting point is the Main method.

Point to displayMessage() In the Main method, we have first called the displayMessage.
Point to the statements in displayMessage() So the control goes to the displayMessage.

And all the statements in the displayMessage are executed.

The first one is the print statement.

Point to the square method Then it comes across the square method.
Point to the statements in the square method So the control jumps to the square method.

The square method takes an integer 5 and returns the square of the integer i.e. 25.

Point to displayMessage() Then the control goes back to the displayMessage.
Point to 7 And it returns the value 7.
Point to Main method. The control jumps back to Main method.

Since there are no statements to execute, the application terminates

Type:

static

Let us now make displayMessage as static.

So after public type static.

Point to the error We see that we cannot call a non static method in the static one.
//square(5); So we will comment this call.
Point to static of Main Since Main is a static method, we can call the static method displayMessage.
//MethodDemo md=new MethodDemo(); Now for static method we do not need to create an object.

So comment the object creation.

Delete md Also Delete md.
Output:

Hello Method

Run the application.

We see the output Hello Method and 7.

We do not see 25 because we have commented the call to square method

Switch to the class Demo We can also call method from other class.

So for that I have created a class Demo.

Public void show()

{

System.out.println(“I am from other class”);

}

Inside the class create a method.

So type public void show parentheses

Within curly brackets, type System dot out dot println

I am from other class.



Press Ctrl+S Save the file.
Switch to MethodDemo Then go back to the MethodDemo class
Type:

Demo d=new Demo;

Now we will call this show method in MethodDemo class.

For that we need to create the object of the class Demo.

This is because the method show belongs to the class Demo.

So type Demo d=new Demo parentheses, semicolon

d.show(); Then call method show parentheses.
Output:

I am from other class

Lets Run the application.

We see on the console I am from other class.

This is how methods are used in java.

The method name and the parameters forms the signature of the method.

While the statements inside the curly brackets forms the body of the method.

Slide 7

Summary

So in this tutorial, we have learnt
  • To create a method
  • To call a method
  • Different signatures of methods


Slide 8

Assignment

For self assessment, create a method which prints the cube of an integer.
Slide 9

About Slide

To know more about the Spoken Tutorial Project
  • If you do not have good bandwidth, you can download and watch it


Slide 10

About Slide


The Spoken Tutorial Project Team
  • Conducts workshops using spoken tutorials
  • Gives certificates for those who pass an online test
  • For more details, please write to contact@spoken-tutorial.org


Slide 11

Acknowledgment

Spoken Tutorial Project is a part of the Talk to a Teacher project
  • It is supported by the National Mission on Education through ICT, MHRD, Government of India
  • More information on this Mission is available at


Slide 12


We have come to the end of this tutorial.

Thanks for joining.

This is Prathamesh Salunke signing off.

Jai Hind.

Contributors and Content Editors

Chandrika