Posted by : Octo The Azura
Selasa, 07 April 2015
SECOND
MEETING : ACCESS DETERMINING (PRIVATE AND PUBLIC)
|
|
NAMA
|
RAHMATUL
MAHDALINA
|
STUDENT NUMBER
|
L200134004
|
CLASS
|
X
|
A.
Objective
1. Students are understand about private and public
2. Students are understand about private and public in variable
and method
B. Basic Teory
In
object-oriented programming, access to a variable instan outside the class
usually are not allowed. Instead, there is provided a method which is required to access
the instant variable.
Relates
to whether or not a variable instan be accessed from outside the class, Java
provides access determinant.
1. Public means that access an instant variable or method can be do of the outer class.
2. Private means that access an instant variable or method only can be conducted in class, unbiased accessible of the outer class.
Differences of the
public, private and without access to the variable determinant are:
1. Without determinant Variable access can accessible by the classes are in the same package.
2. Public Variables can be accessible at the all of class that used class that contains the variable.
3. Private only can be accessible by the methods in the class it own.
Differences in public,
private, and without deciding on the access method are:
1. Without determinant access method only can be accessible by the method in the class it own or method at the another class is located at the same package.
2. Public methods can be accessible by the Random class.
3. Private only can be accessible by the methods in the same class.
C. Tools and Materials
· Laptop
· Practice
Object Oriented Programming
Modul
· Software
NetBeans IDE 8.0.2
· Java
Development Kit (JDK) 1.8.0_31
D. Work Step
EXPERIMENT
1
|
Steps
of experiment:
1.
Create java class with name “Mahasiswa.java”, write the
program code:
package Modul2;
/**
*
* @author
Octopus_girl
*/
public class Mahasiswa {
//variable
instan
String nama;
//methode
void
isiNama(String nama){
this.nama = nama;
}
//methode
String
tampilkanNama(){
return
this.nama;
}
}
2.
Compile code
3.
Create new
class with name “PenentuAkses.java”, and change the name by your name,
package Modul2;
/**
*
* @author Octopus_girl
*/
public class PenentuAkses {
public static void main
(String[]args){
Mahasiswa saya = new
Mahasiswa ();
//mengisi variable instan
saya.isiNama("Rahmatul Mahdalina");
//menampilkan nama melalui
variable
System.out.println(saya.nama);
//menampilkan nama melalui
pemanggilan methode
System.out.println(saya.tampilkanNama());
}
}
4.
Compile and
analysis the result
run:
Rahmatul Mahdalina
Rahmatul Mahdalina
BUILD SUCCESSFUL (total time: 0 seconds)
EXPERIMENT
2
|
Steps
of experiment:
1.
Create java class with name “Lingkaran.java”, write the
program code:
package Modul2;
/**
*
* @author Octopus_girl
*/
public class Lingkaran {
private double radius;
//methode
void isiJariJari(double
radius){
this.radius = radius;
}
private double ambilPhi(){
return 3.14;
}
public double
hitungKeliling(){
return
2*ambilPhi()*radius;
}
}
2.
Compile code
3.
Create new
class with name “PenentuAksesMethod.java”, and change the name by your name,
package Modul2;
/**
*
* @author
Octopus_girl
*/
public class PenentuAksesMethod {
public
static void main (String []args){
Lingkaran
bulatan = new Lingkaran ();
//mengisi
jari-jari lingkaran
bulatan.isiJariJari(10);
//menampilkan keliling lingkaran
System.out.println("Keliling = "+bulatan.hitungKeliling());
//menampilkan nilai Phi
System.out.println("niai Phi = "+bulatan.ambilPhi());
}
}
4.
Compile and
analysis the result
run:
Keliling = 62.800000000000004
Exception in thread "main" java.lang.RuntimeException: Uncompilable
source code - ambilPhi() has private access in Modul2.Lingkaran
at
Modul2.PenentuAksesMethod.main(PenentuAksesMethod.java:24)
Java Result: 1
BUILD SUCCESSFUL (total time: 3 seconds)
E. Analysis
· Experiment 1
The
first line found out result from calling the variable and the second line found
out result from calling the method.
Cause
the variable and method are public so, can be access by another class. In the
experiment the another class is class “PenentuAkses”
· Experiment 2
Cause the method of double ambilPhi() is private so,
“PenentuAksesMethod.java” just can show “keliling” of the “lingkaran”. And
caused it is method, another method in same variable that’s not private can be
access by another class.
· Exercise 1
The experiment 1 has a variable and in the exercise
the variable changed become private so, other class can’t access class
Mahasiswa and because that’s the variable automatically the method can’t
accessed too.
· Exercise 2
Method double ambilPhi(); change become public so
can be access another class and can be show in the result.
F. Assignment
EXERCISE
1
|
1.
Changed penentu akses from variable nama become private
access, the method by add keyword like
private String nama;
package Modul2;
/**
*
* @author
Octopus_girl
*/
public class Mahasiswa {
//variable
instan
private
String nama;
//methode
void
isiNama(String nama){
this.nama = nama;
}
//methode
String
tampilkanNama(){
return
this.nama;
}
}
2.
Save by same
name then compilation
3.
Compile file
“PenentuAkses.java”
4.
Watch the
result and compare with result from the beginning before variable akses changed
become private
run:
Exception
in thread "main" java.lang.RuntimeException: Uncompilable source code
- nama has private access in Modul2.Mahasiswa
at
Modul2.PenentuAkses.main(PenentuAkses.java:18)
Java
Result: 1
BUILD
SUCCESSFUL (total time: 3 seconds)
EXERCISE
2
|
1.
Changed penentu akses from method ambilPhi() become
public access, the method by add keyword like
public double ambilPhi() ;
package Modul2;
/**
*
* @author Octopus_girl
*/
public class Lingkaran {
private double radius;
//methode
void isiJariJari(double
radius){
this.radius = radius;
}
public double ambilPhi(){
return 3.14;
}
public double
hitungKeliling(){
return
2*ambilPhi()*radius;
}
}
2.
Save by same
name then compilation
3.
Compile file
“PenentuAksesMethod.java”
4.
Watch the
result and compare with result from the beginning before variable akses changed
become public
Keliling =
62.800000000000004
niai Phi =
3.14
BUILD
SUCCESSFUL (total time: 1 second)