You are on page 1of 19

CSC 201

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 { }.

• Usually methods have 5 components:


1) Modifiers – such as public, private and others ( later
chapter).
2) The return type – The data type of the value returned by the
method or ‘void’ if the method does not return a value.
3) Method name
4) The parameter list in parenthesis ( ) - A comma delimited
list of input parameters preceded by their data type.
5) Method body – enclosed between { and }.
Hello world method
public static void hello_world()
Parameters if any
{ Modifier
Return type Method name Inside the parenthesis

System.out.println(“Hello world:”);
}
Body of the method

What is meant by static?


First program
class method_sample
{
public static void hello_world_method()
{
System.out.println(“Hello world”);
Method defined
}
public static void main(String args[])
{
hello_world_method(); Method called

}
}
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

Concept Java Construct description

Function Static method Mapping

Domain Argument type Set of values


where function is
defined
Range Return type Set of values a
function can
return
Formula Method body
Function
definition
Announcements
• Homework – 2 due date extended to 10/15/09
since 10/13/09 is non-working day.
• Special office hours on 10/15/09 from 1:00pm
to 3:00pm before midterm.
• Midterm examination CSC201 : 10/20/09
Tuesday 11:00am – 12:50pm.
• CSC 185 lab in-class quiz on 10/27/09
between 10:00am – 11:00am.

You might also like