You are on page 1of 9

1

Implementasi Algoritma

Method

Tim Dosen Implementasi Algoritma

D3 RPLA – Fakultas Ilmu Terapan


2
import java.util.Scanner;
import java.lang.Math;

public class DemoMethod {


public static void main(String[] args) {
Scanner in = new Scanner(System.in);

System.out.println("Masukkan jari-jari lingkaran


pertama:");
int jari1 = in.nextInt();

System.out.println("Masukkan jari-jari lingkaran kedua:");


int jari2 = in.nextInt();

final double PI = 3.14;


double hasil1 = PI * Math.pow(jari1,2);
final double PI = 3.14;
double hasil2 = PI * Math.pow(jari2,2);

System.out.println("Luas lingkaran pertama " + hasil1);


System.out.println("Luas lingkaran kedua " + hasil2);
}
}

2
3

What is a Function?
4

Function Definition

Access modifier Return type Function name Parameter

Function name ditulis


dalam lowerCamelCase

void digunakan ketika tidak


ada nilai yang dikembalikan
5

Function Call

Arguments: spesific values passed into function call


6

Parameter and Method

Parameter 1
(5) Method
(Sum) Return
(11)
Parameter 2
(6)
7

Variable scope (Again)

• Local variables and method parameters are defined within a method. They’re
accessible only in the method that defines them.

• The scope of a variable specifies its life span. If the scope of a variable is
limited to a method, its life span is also limited to that method.

• The task of reclaiming unused memory is taken care of by Java’s garbage


collector. It runs periodically and frees up space occupied by unused objects.
8

Latihan

• Buatlah program yang meminta 2 buah nama, kemudian


menyapa kedua nama tadi dengan:
“Hai, [nama]. Semangat pagi!”

• Buatlah program yang meminta suhu minimal, suhu rata-rata


dan suhu maksimal di suatu hari dalam Celcius, kemudian
menampilkan ketiga suhu tadi dalam Fahrenheit.
9

Referensi
• Java Programming Basics
https://www.udacity.com/course/ud282

• OCA Java SE 7 Certification Guide


https://www.manning.com/books/oca-java-se-7-programmer-i-certification-g
uide

You might also like