Non-Static Method
Non-Static Method
Non-Static Method
5 Methods
Non-static Methods
Learning Outcomes:
Method Definitions
Page 1
3.5 Methods
Calling a Method
Before a method is called, we must create the object in order to call the specific
method. Object created using the statement,
where, ClassName is the name of the class, and obj can be any valid identifiers
to represent instance of the class.
Four (4) types of method calls based on method definitions with examples:
For calling Type 3 and Type 4 method, usually these types of methods call either:
Assign to a variable then display the result using the output statement, or
Directly use output statement to display the output.
Page 2
3.5 Methods
For example:
import java.util.Scanner;
class Arithmetic{
public static void main(String[] args){
Scanner sc = new Scanner(System.in);
Arithmetic obj = new Arithmetic();
obj.showWelcome();
obj.sayHi(name);
int result = obj.calcAdd(num1, num2);
System.out.print("Addition: "+ result);
}
void showWelcome(){
System.out.println("Welcome to Java");
}
Page 3
3.5 Methods
Type 1 Type 3
//method-body //method-body
Receive
} }
Arguments Please read Page 55, for calling
Type 3 and Type 4 method.
Type 2 Type 4
Page 4
3.5 Methods
MethodCall1.java
class MethodCall1{
mc.printMethod();
} 3
4
void printMethod(){
Explanation:
No. Description
Page 5
3.5 Methods
Message 1: I Love Computer Science will be printed from the main() method.
Message 2: I Like Programming will be printed from calling methodName1().
Message 3: I Learn Java will be printed from calling methodName2().
MethodCall2.java
class MethodCall2{
public static void main(String[] args){
MethodCall2 obj = new MethodCall2();
void methodName1(){
System.out.println("I Like Programming");
}
void methodName2(){
System.out.println("I Learn Java");
}
}
Page 6
3.5 Methods
Hello, <yourname>
MethodCall3.java
class MethodCall3{
public static void main(String[] args){
MethodCall3 mc = new MethodCall3();
mc.printName(name);
}
Hello, Amran
**Here, we can see that the object name can be any valid identifiers.
Page 7
3.5 Methods
Problem Statement
Write a complete Java program which accept name and age of a person, then
display the following output from the method named printNameAge():
Hello, <yourname>
Your age now is <age> years old
MethodCall4.java
import java.util.Scanner;
class MethodCall4{
public static void main(String[] args){
Scanner sc = new Scanner(System.in);
MethodCall4 tmp = new MethodCall4();
Is the method call, in this program passed any arguments? If yes, name the
arguments. __________ ____________ ____________
Name the parameters that receive the arguments passed. ______ ______
Page 8
3.5 Methods
SquareMain.java
class SquareMain {
int result;
result = sm.square();
int square() {
return 10 * 10;
Give one (1) example of method definition and method call exists in this
program.
____________________________
____________________________
Page 9
3.5 Methods
SquareMain.java
class SquareMain {
public static void main(String[] args) {
SquareMain sm = new SquareMain();
int result, n;
n = 3;
result = sm.square(n);
System.out.println("Square of 3 is: " + result);
n = 4;
result = sm.square(n);
System.out.println("Square of 4 is: " + result);
}
int square(int i) {
return i * i;
}
}
Square of 3 is: 9
Square of 4 is: 16
Page 10
3.5 Methods
ArithmeticMain.java
class ArithmeticMain{
public static void main(String[] args){
ArithmeticMain am = new ArithmeticMain();
10 + 20 = 30
20 x 40 = 800
Page 11
3.5 Methods
SquareMain.java
class SquareMain {
public static void main(String[] args) {
SquareMain sm = new SquareMain();
int result=0;
Page 12
3.5 Methods
(a) Write the first line of a method named myMethod that takes three
parameters: an int and two Strings.
(b) Write a line of code that calls myMethod, passing as arguments the
value 501, the name of your favorite colour, and the name of the
state you grew up on.
(a) Write a method called printAmerican that takes the day, date,
month and year as parameters and that displays them in American
format.
(b) Test your method by invoking it from main and passing
appropriate arguments. The output should look something like this
(except that the date might be different):
Saturday, July 22, 2015
(c) Once you have debugged printAmerican, write another method
called printEuropean that displays the date in European format.
5 Write a method named oddSum that takes a positive odd integer n and
returns the sum of odd integers from 1 to n. Test the method using main
method.
Page 13