1.
Kelas
“Mahasiswa.java”
/*
* To change this license header, choose
License Headers in Project Properties.
* To change this template file, choose Tools |
Templates
* and open the template in the editor.
*/
package
mahasiswa;
/**
*
* @author Fajar
*/
public
class Mahasiswa {
//variable instan
String nama;
//methode
void isiNama(String nama) {
this.nama=nama;
}
//methode
String tampilkanNama(){
return this.nama;
}
public static void main(String[] args) {
// TODO code application logic here
}
}
1.
Kelas
“PenentuAkses.java
/*
* To change this license header, choose
License Headers in Project Properties.
* To change this template file, choose Tools |
Templates
* and open the template in the editor.
*/
package
mahasiswa;
/**
*
* @author Fajar
*/
public
class PenentuAkses {
public static void main (String [] args){
Mahasiswa saya = new Mahasiswa();
//Mengisi variable instan
saya.isiNama("Hariyadi Faja
Nugroho");
//Menampilkan nama melalui variable
System.out.println(saya.nama);
//menampilkan nama melalui pemanggilan
methode
System.out.println(saya.tampilkanNama());
}
}
1.
Kelas
Lingkaran.java
/*
* To change this license header, choose
License Headers in Project Properties.
* To change this template file, choose Tools |
Templates
* and open the template in the editor.
*/
package mahasiswa;
/**
*
* @author Fajar
*/
public class Lingkaran {
private double radius;
String ambilPhi;
//methode
void isiJariJari(double radius){
this.radius = radius;
}
private double ambilPhi(){
return 3.14;
}
public double hitungKeliling(){
return 2*ambilPhi()*radius;
}
}
}
1.
Kelas
PenentuAksesMethod
/*
* To change this license header, choose
License Headers in Project Properties.
* To change this template file, choose Tools |
Templates
* and open the template in the editor.
*/
package mahasiswa;
/**
*
* @author Fajar
*/
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);
}
}
Tidak ada komentar:
Posting Komentar