LAB
Java Language Programming
3.5 Methods (Method Definition and Call)
Objective :
• Construct programs that use standard and/or user-defined methods base on
given problems.
Task 1 : Method Without Argument and Without Return Value
Write a complete Java program that print out message “Java programming is easy”
from a method named printMessage().
File Name: [Link]
class MethodCall1{
public static void main(String[] args){
//create object for class
MethodCall1 mc = new MethodCall1();
//method call
[Link]();
}
//method definition
void printMessage(){
//output statement
[Link](“Java programming is easy”);
}
}
Output from Terminal
Java programming is easy
1
Task 2 : Method Without Argument and Without Return Value
Write a complete Java program that print out the following messages:
Message 1 : Computer Science
Message 2 : One Year Program
Message 3 : SC025
Message 1 : Computer Science will be printed from the main() method.
Message 2 : One Year Program will be printed from calling methodName1().
Message 3 : SC025 will be printed from calling methodName2().
File Name : [Link]
class MethodCall2{
public static void main(String[] args){
//create object for class
MethodCall2 mc = new MethodCall2();
//display first line
[Link](“Computer Science”);
//method call for second line
mc.methodName1();
//method call for third line
mc.methodName2();
}
//first method definition
void methodName1(){
//output statement for second line
[Link](“One Year Program”);
}
//second method definition
void methodName2(){
//output statement for third line
[Link](“SC025”);
}
}
Output from Terminal
Computer Science
One Year Program
SC025
2
Task 3 : Method Without Argument and Without Return Value
Write a complete Java program that print out message “Java programming is easy”
for 5 times (one message per line) from a method named printRepetition().
File Name: [Link]
class MethodCall3{
public static void main(String[] args){
//create object for class
MethodCall3 mc = new MethodCall3();
//for structure
for(int i=1; i<=5 ; i=i+1)
//method call
[Link]();
}
//method definition
void printRepetition(){
//output statement
[Link](“Java programming is easy”);
}
}
Output from Terminal
Java programming is easy
Java programming is easy
Java programming is easy
Java programming is easy
Java programming is easy
3
Exercises
1. Write a complete Java program that print out message “Happy Holiday” from a
method named printHappy().
2. Write a complete Java program that print out 5 lines. First line is your name, second
line is your birthday, third line is your place of birth, fourth line is your hometown
and last line is for your favourite colour. You must use method for first line, third line
and last line. The output will look like this:
Name: Zakaria bin Mujir
Date of Birth: 13 Mac 2000
Place of Birth: Kuala Kangsar
Hometown: Ipoh
Favourite Colour: Blue
3. Write a complete Java program that print hash(#), one hash in line 1, two hashes in
line 2 and so on until line 4 using a method named printHash(). The output will
look like this.
#
##
###
####