CSC 201-Lecture 9
CSC 201-Lecture 9
Lecture - 9
Basic Java Program Structure
Class example
{
public static void main (String[] args)
{
System.out.println(“Hello world!”);
}
}
Functions/Static Methods
• In a simple Java program we have seen a class
which contained a single method ‘main’. Every
Java program must have a main() method.
• Method is a group of instructions that is given a
name and can be called up at any point in a
program simply by quoting that name.
• The power of Object-orientation lies in breaking
tasks down into simpler tasks.
How to define your method?
class sample
{
public static void method_name(parameters if any)
{
//Write your method definition here.
}
public static void main(String[] args)
{
method_name(); // calling your method to execute
}
}
Defining Methods
• The only required elements of a method declaration are the
method’s return type, method name, a pair of parenthesis ( )
and a body between the braces { }.
System.out.println(“Hello world:”);
}
Body of the method
}
}
Multiple methods in a program
class multiple_methods
{
public static void calculate_area() Defining
{ calculate_area
int length = 10, breadth = 20, area=0; method
area = length * breadth;
System.out.println(area);
}
public static void hello_world()
{ Defining
System.out.println(“Hello world”); hello_world
method
}
Public static void main(String[] args)
{
hello_world();
calculate_area();
}
}
Notes
• We can define as many static methods as
we want in a .java file.
• Each method has a body which consists of
a sequence of statements enclosed in { }.
Parameter passing
What are Parameters?
Parameters refer to the list of variables in
a method declaration. They are used in
the method body and will take on the
values of the arguments that are passed
in.
How to pass parameters in Java?
A simple program to demonstrate the method ‘multiply’.
Class sample
{
public static void multiply (int num)
{
num = num * 2;
System.out.println(“Your input number multiplied by 2 is:”+num);
}
public static void main(String[] args)
{
Scanner s = new Scanner(System.in);
System.out.println(“Enter a number and I multiply with 2:”);
int input_num = s.nextInt();
multiply(input_num);
}
}
How does the execution of this program take place? What are arguments?
Same Program but with Return
types
class sample
{
public static int multiply (int num)
{
num = num * 2;
return num;
}
public static void main(String[] args)
{
Scanner s = new Scanner(System.in);
System.out.println(“Enter a number and I multiply with 2:”);
int input_num = s.nextInt();
int result = multiply(input_num);
System.out.println(“The result after multiplying with 2 is:”+result);
}
}
Can we similarly pass ‘float’ values? What about other datatypes?
Programs
1) Write a swap function to swap two numbers.
2) Write a program to calculate an area of a
triangle and rectangle.
3) Is there any limit on the numbers of
parameters a method can have?
4) What would happen if you write some code
after a return statement in a method?
5) What would happen if you do not include a
return statement?
6) Write a static method odd() that takes 3
boolean inputs and returns true if odd
number of inputs are true, and false
otherwise.
7) What is function calling?
Recursion Example
class factorial
{
public static int fact(int num)
{
int result;
if(num == 1)
return 1;
result = num * fact(num – 1);
return result;
}
public static void main(String[] args)
{
int factorial = fact(5);
System.out.println(“Factorial is”+factorial);
}
}
What is Recursion?
• Recursion is the process of defining
something in terms of itself. With relation
to Java programming, recursion is the
attribute that allows a method to call itself.
A method that calls itself is said to be
recursive.
Advantages of Functions
• You can customize your functions according to
programmer’s needs.
• You can minimize the coding time, because you
don’t have to repeat a piece of code every time
you need it.
• Functions can take information and process as
you need.
• They can return data of a certain data type.
• They are easy to understand and best way to
program.
• Bug fixing is easy.
Terminology