csc 201-lecture 9
TRANSCRIPT
8/14/2019 CSC 201-Lecture 9
http://slidepdf.com/reader/full/csc-201-lecture-9 1/19
CSC 201
Lecture - 9
8/14/2019 CSC 201-Lecture 9
http://slidepdf.com/reader/full/csc-201-lecture-9 2/19
Basic Java Program Structure
Class example
{
public static void main (String[] args){
System.out.println(“Hello world!”);
}}
8/14/2019 CSC 201-Lecture 9
http://slidepdf.com/reader/full/csc-201-lecture-9 3/19
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 aname 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.
8/14/2019 CSC 201-Lecture 9
http://slidepdf.com/reader/full/csc-201-lecture-9 4/19
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
}
}
8/14/2019 CSC 201-Lecture 9
http://slidepdf.com/reader/full/csc-201-lecture-9 5/19
Defining Methods
• The only required elements of a method declaration are themethod’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 themethod or ‘void’ if the method does not return a value.
3) Method name
4) The parameter list in parenthesis ( ) - A comma delimitedlist of input parameters preceded by their data type.
5) Method body – enclosed between { and }.
8/14/2019 CSC 201-Lecture 9
http://slidepdf.com/reader/full/csc-201-lecture-9 6/19
Hello world method
public static void hello_world()
{
System.out.println(“Hello world:”);}
Modifier Return type Method name
Parameters if any
Inside the parenthesis
Body of the method
What is meant by static?
8/14/2019 CSC 201-Lecture 9
http://slidepdf.com/reader/full/csc-201-lecture-9 7/19
First program
class method_sample
{
public static void hello_world_method()
{
System.out.println(“Hello world”);
}
public static void main(String args[])
{
hello_world_method();
}
}
Method defined
Method called
8/14/2019 CSC 201-Lecture 9
http://slidepdf.com/reader/full/csc-201-lecture-9 8/19
Multiple methods in a programclass multiple_methods
{public static void calculate_area()
{
int length = 10, breadth = 20, area=0;
area = length * breadth;
System.out.println(area);}
public static void hello_world()
{
System.out.println(“Hello world”);
}
Public static void main(String[] args)
{
hello_world();
calculate_area();
}
}
Defining
calculate_area
method
Defining
hello_world
method
8/14/2019 CSC 201-Lecture 9
http://slidepdf.com/reader/full/csc-201-lecture-9 9/19
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 { }.
8/14/2019 CSC 201-Lecture 9
http://slidepdf.com/reader/full/csc-201-lecture-9 10/19
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.
8/14/2019 CSC 201-Lecture 9
http://slidepdf.com/reader/full/csc-201-lecture-9 11/19
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?
8/14/2019 CSC 201-Lecture 9
http://slidepdf.com/reader/full/csc-201-lecture-9 12/19
Same Program but with Return
typesclass 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?
8/14/2019 CSC 201-Lecture 9
http://slidepdf.com/reader/full/csc-201-lecture-9 13/19
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?
8/14/2019 CSC 201-Lecture 9
http://slidepdf.com/reader/full/csc-201-lecture-9 14/19
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?
8/14/2019 CSC 201-Lecture 9
http://slidepdf.com/reader/full/csc-201-lecture-9 15/19
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);
}
}
8/14/2019 CSC 201-Lecture 9
http://slidepdf.com/reader/full/csc-201-lecture-9 16/19
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.
8/14/2019 CSC 201-Lecture 9
http://slidepdf.com/reader/full/csc-201-lecture-9 17/19
Advantages of Functions
• You can customize your functions according toprogrammer’s needs.
• You can minimize the coding time, because youdon’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 toprogram.
• Bug fixing is easy.
8/14/2019 CSC 201-Lecture 9
http://slidepdf.com/reader/full/csc-201-lecture-9 18/19
Terminology
Concept Java Construct description
Function
Domain
Range
Formula
Static method
Argument type
Return type
Method body
Mapping
Set of values
where function is
defined
Set of values a
function can
return
Function
definition
8/14/2019 CSC 201-Lecture 9
http://slidepdf.com/reader/full/csc-201-lecture-9 19/19
Announcements
• Homework – 2 due date extended to 10/15/09since 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.