Non-Static Method

Download as pdf or txt
Download as pdf or txt
You are on page 1of 13

3.

5 Methods

Non-static Methods

Learning Outcomes:

At the end of this lesson, students should be able to:


1. Construct non-static user defined methods
2. Construct programs that use standard and/or user-defined methods based
on given problem.

Task 1: Understanding the Concept of Methods


Methods must be defined before it is being called.

Method Definitions

Four (4) types of method defintions with examples:

Type 1. A method definition which return no value, receive no parameters


void showWelcome(){
System.out.print("Welcome to Java");
}

Type 2. A method definition which return no value, receive parameters


void sayHello(String name){
System.out.print("Hello "+ name);
}

Type 3. A method definition which return int value, receive no parameters


(this type of definition is rarely used)
int calcProduct(){
return 10*10;
}

Type 4. A method definition which return int value, receive parameters


int calcAdd(int x, int y){
return x+y;
}

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,

ClassName obj = new ClassName();

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:

1. Calling a Type 1 method, receive no value, pass no arguments:


obj.showWelcome();

2. Calling a Type 2 method, receive no value, pass arguments:


obj.sayHello(String name);

3. Calling a Type 3 method, receive value, pass no arguments (rarely used):


result = obj.calcProduct();
or
System.out.print(obj.calcProduct());

4. Calling a Type 4 method, receive value, pass arguments:


result = obj.calcAdd(x, y);
or
System.out.print(obj.calcAdd(x,y));

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();

System.out.print("Enter your name and 2 numbers: ");


String name = sc.next();
int num1 = sc.nextInt();
int num2 = sc.nextInt();

obj.showWelcome();
obj.sayHi(name);
int result = obj.calcAdd(num1, num2);
System.out.print("Addition: "+ result);
}

void showWelcome(){
System.out.println("Welcome to Java");
}

void sayHi(String name){


System.out.println("Hello "+name);
}

int calcAdd(int x, int y){


return x+y;
}
}

Enter your name and 2 numbers: Amran 9 3


Welcome to Java
Hello Amran
Addition: 12

Page 3
3.5 Methods

Template for Method Definitions and Calls

Return No Value Return a Value (int, double, boolean, char, String)


METHOD
Method Definition Method Call Method Definition Method Call

void myMethod1(){ obj.myMethod1() int myMethod3(){ tmp=obj.myMethod3()

Receive //method-body //method-body System.out.print(tmp);


No
Arguments } }

Type 1 Type 3

void myMethod2(int a){ obj.myMethod2(x) int myMethod4(int a){ System.out.print(obj.myMethod4(x));

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

Task 2: A Method Accepting No Arguments and Returning No


Value
Write a complete Java program which print out a message “I Love Computer
Science” from a method named printMessage().

MethodCall1.java
class MethodCall1{

public static void main(String[] args){


1
MethodCall1 mc = new MethodCall1();

mc.printMethod();

} 3
4

void printMethod(){

System.out.print("I Love Computer Science");

I Love Computer Science

*Note: mc is object or instance of class MethodCall1.

Explanation:

No. Description

1 Create object, mc from class MethodCall1

2 Use object, mc to call method, printMethod

3 Pass no arguments, () to printMethod

4 Call method, printMethod

Page 5
3.5 Methods

Task 3: A Method Accepting No Arguments and Returning No


Value
Write a complete Java program which print out the following messages:

Message 1: I Love Computer Science


Message 2: I Like Programming
Message 3: I Learn Java

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();

System.out.println("I Love Computer Science");


obj.methodName1();
obj.methodName2();
}

void methodName1(){
System.out.println("I Like Programming");
}

void methodName2(){
System.out.println("I Learn Java");
}
}

I Love Computer Science


I Like Programming
I Learn Java

*Note: The statement

MethodCall2 obj = new MethodCall2();

is a syntax to create object, obj from its class name MethodCall2.

Page 6
3.5 Methods

Task 4: A Method with Arguments and Returning No Value


Write a complete Java program with method calling which display the following
output:

Hello, <yourname>

MethodCall3.java

class MethodCall3{
public static void main(String[] args){
MethodCall3 mc = new MethodCall3();

String name = "Amran";

mc.printName(name);
}

void printName(String a){


System.out.println("Hello, "+ a);
}
}

Hello, Amran

*Note: The statement

MethodCall3 mc = new MethodCall3();

is a syntax to create object, mc from its class name MethodCall3.

**Here, we can see that the object name can be any valid identifiers.

Page 7
3.5 Methods

Task 5: A Method with Arguments and Returning No Value

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();

System.out.print("Enter name and age: ");


String name = sc.next();
int age = sc.nextInt();
tmp.printName(name,age);
}

void printName(String a, int b){


System.out.println("Hello, "+ a);
System.out.println("Your age now is "+ b +" years old.");
}
}

Enter name: Amran


Enter age: 40
Hello, Amran
Your age now is 40 years old.

 What is the object name for the class 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

Task 6: A Method without Arguments and Returning Value


Write a complete Java program which find the square value of 10.

SquareMain.java

class SquareMain {

public static void main(String[] args) {

SquareMain sm = new SquareMain();

int result;

result = sm.square();

System.out.println("Squared value of 10 is: " + result);

int square() {

return 10 * 10;

Squared value of 10 is: 100

 Name one (1) object in this program.


__________________

 Give one (1) example of method definition and method call exists in this
program.

____________________________

____________________________

Page 9
3.5 Methods

Task 7: A Method Accepting Arguments and Returning Value


Write a complete Java program which return the square of a number 3 and 4.

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

Which type the method square?

Page 10
3.5 Methods

Task 8: A Method Accepting Arguments and Returning Value


Write a complete Java program which return the sum and the product of two
integers.

ArithmeticMain.java

class ArithmeticMain{
public static void main(String[] args){
ArithmeticMain am = new ArithmeticMain();

System.out.println("10 + 20 = " + am.getSum(10, 20));


System.out.println("20 x 40 = " + am.getProduct(20, 40));
}

int getSum(int i, int j){


return i + j;
}

int getProduct(int x, int y){


return x * y;
}
}

10 + 20 = 30
20 x 40 = 800

What is the object name for the class ArithmeticMain?

Which type the method getSum and getProduct?

Page 11
3.5 Methods

Task 9: A Method Accepting Arguments and Returning Value


Write a complete Java program which takes int as a parameter. Based on the
argument passed, the method returns the squared value of it.

SquareMain.java

class SquareMain {
public static void main(String[] args) {
SquareMain sm = new SquareMain();

int result=0;

for (int i = 1; i <= 5; i++) {


result = sm.getSquare(i);
System.out.println("Square of "+ i +" is : "+ result);
}
}

int getSquare(int x){


return x * x;
}
}

What is the output for the above program?

Page 12
3.5 Methods

Task 10: Exercise

1 Create a non-static method,

(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.

2 Create a non-static method,

(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.

3 Write a method called square that takes an integer and returns an


approximation of the square of the parameter. Test the method using
main method.

4 Write a method named isNotDivisible that takes two integers, p and


q, and that returns true if p is not divisible by q, and false otherwise.
Test the method using main method.

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

You might also like