Thursday, March 19, 2015

MODUL 2 : ACCESS DETERMINER (PUBLIC AND PRIVATE)


A.                Objectives
Objectives of making this report are :
1.      Students are able to distinguish access determiners between Public and Private.
2.  Students are able to understand the material about how to implement the access determiners (Public and Private) on Java Class scripting.

B.                 Theoretical Basis
On Java Language Programming, it provides access determiners that are for determining whether an instant variable could be accessed from other class or not. The access determiners are Public and Private.
Public means that accessing an instant variable or method is able accessed from other classes. Private means that accessing an instant variable or method is able accessed only if inside of the class created, it’s not able accessed from other classes.

C.                Tools and Material
1.      Laptop
2.      Java Development Kit (JDK 8)
3.      NetBeans IDE 8.0.2

D.                Work Steps

1.      Experiment 1
a.       Creating project and Source Packages required below :
·         Project Name                           :           Modul 1
·         Source Packages Name           :           Percobaan 1
b.      Creating a java class named “Mahasiswa.java” to the Source Packages.

c.       Typing program codes in the java class.

package Percobaan1;

/**
 *
 * @author Jhonny
 */
public class Mahasiswa {
    //Variable instan
    String nama;
  
    //Method
    void isiNama(String nama){
        this.nama = nama;
    }
    //method
    String tampilkanNama(){
        return this.nama;
    }
}
d.      Compiling the program codes.
e.       Creating a new java class named “PenentuAkses.java” to the Source Packages.
f.       Typing program codes to the java class.

package Percobaan1;

/**
 *
 * @author Jhonny
 */
public class PenentuAkses {
    public static void main (String []args){
        Mahasiswa saya = new Mahasiswa();
        //Mengisi variable instan
        saya.isiNama("JONI");
        //Menampilkan nama melalui variable
        System.out.println(saya.nama);
        //Menampilkan nama melalui pemanggilan method
        System.out.println(saya.tampilkanNama());
    }
}
g.      Compiling the program codes and see the result.


2.      Exercise
a.  Changing access determiner at variable of  “nama” in the java class of “Mahsiswa.java” become Private access.
It should be like this :

b.      Saving the new program codes.
c.       Then, compiling the program codes again and seeing the result.


E.                 Analysis
As the result of experiment 1, the program prints the inputed name of  “JONI” twice because there are two printing codes in the java class, they are “System.out.println(saya.nama)” and System.out.println(saya.tampilkanNama())”.
After changing the access determiner of variable “nama”, the result is syntax Error. The reason is access determiner of variable “nama” become Private access. It means the variable could not be accessed in other classes.

F.                 Tasks
1.      Experiment 2
a.       Creating a new Source Packages named “Percobaan 2”.
b.      Creating a java class named “Lingkaran.java” to the Source Packages.

c.       Typing program codes to the java class.

package Percobaan2;

/**
 *
 * @author Jhonny
 */
public class Lingkaran {
    private double radius;
   
    //method
    void isiJariJari(double radius){
        this.radius = radius;
    }
    public double ambilPhi(){
        return 3.14;
    }
    public double hitungKeliling(){
        return 2*ambilPhi()*radius;
    }
}
d.      Compiling the program codes.
e.       Then, creating a new java class named “PenentuAksesMethod.java” to the Source Packages.
f.       Typing program codes to the java class.

package Percobaan2;

/**
 *
 * @author Jhonny
 */
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("Nilai Phi = "+bulatan.ambilPhi());
    }
}
g.      Compiling the program codes and seeing the result.

2.      Exercise
a.   Changing the access determiner from method of “ambilPhi()” become Public access.
It should be like this.

b.      Saving the new program codes.
c.       Then, compiling the program codes and seeing the result.


No comments:

Post a Comment