Ini adalah Class Kalender.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package programjava; | |
/** | |
* Created by ahmad on 18/04/17. | |
*/ | |
public class ClassKalender { | |
private int tanggal = 1; | |
private int bulan = 1; | |
private int tahun = 2000; | |
public ClassKalender(int tanggal){ | |
this.tanggal = tanggal; | |
} | |
public ClassKalender(int bulan, int tahun){ | |
this.bulan = bulan; | |
this.tahun = tahun; | |
} | |
public ClassKalender(int tanggal, int bulan, int tahun){ | |
this.tanggal = tanggal; | |
this.bulan = bulan; | |
this.tahun = tahun; | |
} | |
public int getTanggal(){ | |
return tanggal; | |
} | |
public int getBulan(){ | |
return bulan; | |
} | |
public int getTahun(){ | |
return tahun; | |
} | |
public void setTanggal(int tanggal){ | |
this.tanggal = tanggal; | |
} | |
public void setBulan(int bulan){ | |
this.bulan = bulan; | |
} | |
public void setTahun(int tahun){ | |
this.tahun = tahun; | |
} | |
} |
Dan ini adalah Class Main Kalender.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package programjava; | |
/** | |
* Created by ahmad on 18/04/17. | |
*/ | |
public class MainKalender { | |
public static String getTime(ClassKalender kalender) { | |
String tampilan; | |
tampilan = kalender.getTanggal() + "-" + | |
kalender.getBulan() + "-" + | |
kalender.getTahun(); | |
return tampilan; | |
} | |
public static void main(String args[]) { | |
ClassKalender kalender = new ClassKalender(8); | |
System.out.println("Waktu awal : " + getTime(kalender)); | |
kalender.setTanggal(9); | |
System.out.println("1 hari setelah waktu awal : "+getTime(kalender)); | |
kalender = new ClassKalender(6, 2003); | |
System.out.println("Waktu berubah : " + getTime(kalender)); | |
kalender.setBulan(7); | |
System.out.println("1 bulan setelah itu : " + getTime(kalender)); | |
kalender = new ClassKalender(20, 10, 2004); | |
System.out.println("Waktu berubah : " + getTime(kalender)); | |
kalender.setTahun(2005); | |
System.out.println("1 tahun setelah itu : " + getTime(kalender)); | |
} | |
} |
Hasil dari program diatas adalah seperti pada gambar dibawah ini.
Encapsulation Truck
Ini adalah Class Truck.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package programjava; | |
/** | |
* Created by ahmad on 18/04/17. | |
*/ | |
public class ClassTruck { | |
private double muatan; | |
private double muatanmax; | |
public ClassTruck(double beratmax){ | |
this.muatanmax = beratmax; | |
} | |
public double getMuatan(){ | |
return muatan; | |
} | |
public double getMuatanmax(){ | |
return muatanmax; | |
} | |
public boolean tambahMuatan(double berat){ | |
if ((muatan + berat) < muatanmax) { | |
muatan = muatan + berat; | |
return true; | |
}else { | |
return false; | |
} | |
} | |
public double newtsToKilo(double berat){ | |
double k = 9.8 % 1; | |
return k; | |
} | |
public double kiloToNewts(double berat){ | |
double n = berat * 9.8; | |
return n; | |
} | |
} |
Dan ini adalah Class Main Truck.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package programjava; | |
/** | |
* Created by ahmad on 18/04/17. | |
*/ | |
public class MainTruck { | |
public static void main(String args[]) { | |
boolean status; | |
ClassTruck truk = new ClassTruck(900); | |
System.out.println("Muatan maksimal = " + truk.getMuatanmax()); | |
status = truk.tambahMuatan(500.0); | |
System.out.println("Tambah muatan : 500"); | |
if (status) | |
System.out.println("Ok"); | |
else | |
System.out.println("Gagal"); | |
status = truk.tambahMuatan(300.0); | |
System.out.println("Tambah muatan : 300"); | |
if (status) | |
System.out.println("Ok"); | |
else | |
System.out.println("Gagal"); | |
status = truk.tambahMuatan(150.0); | |
System.out.println("Tambah muatan : 150"); | |
if (status) | |
System.out.println("Ok"); | |
else | |
System.out.println("Gagal"); | |
status = truk.tambahMuatan(50.0); | |
System.out.println("Tambah muatan : 50"); | |
if (status) | |
System.out.println("Ok"); | |
else | |
System.out.println("Gagal"); | |
System.out.println("Muatan sekarang = " + truk.getMuatan()); | |
System.out.println("Newtons To Kilo : " + truk.newtsToKilo(truk.getMuatan())); | |
System.out.println("Kilo To Newtons : " + truk.kiloToNewts(truk.getMuatan())); | |
} | |
} |
Hasil dari program diatas adalah seperti pada gambar dibawah ini.
Encapsulation Tabungan
Ini adalah Class Tabungan.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package programjava; | |
/** | |
* Created by ahmad on 18/04/17. | |
*/ | |
public class ClassTabungan { | |
int saldo; | |
public ClassTabungan(int saldo) { | |
this.saldo = saldo; | |
} | |
public int getSaldo1() { | |
return saldo; | |
} | |
public void simpanUang1(int jumlah) { | |
saldo = saldo + jumlah; | |
} | |
public boolean ambilUang1(int jumlah) { | |
if (saldo > jumlah) { | |
saldo = saldo - jumlah; | |
return true; | |
} else { | |
return false; | |
} | |
} | |
public double idrTOAud(double jumlah){ | |
return jumlah / 10000; | |
} | |
public double idrToUsd(double jumlah){ | |
return jumlah / 9000; | |
} | |
} |
Dan ini adalah Class Main Tabungan.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package programjava; | |
import java.util.Scanner; | |
/** | |
* Created by ahmad on 18/04/17. | |
*/ | |
public class MainMultiTabungan { | |
public static void main(String[] args) { | |
Scanner input = new Scanner(System.in); | |
ClassTabungan tabungan = new ClassTabungan(5000); | |
boolean status; | |
String satuanMataUang; | |
System.out.println("Saldo awal :"+tabungan.getSaldo1()); | |
tabungan.simpanUang1(3000); | |
System.out.println("Jumlah uang yang disimpan : 3000"); | |
status = tabungan.ambilUang1(6000); | |
System.out.println("Jumlah uang yang diambil : 6000"); | |
if (status) | |
System.out.println("Ok"); | |
else | |
System.out.println("Gagal"); | |
tabungan.simpanUang1(3500); | |
System.out.println("Jumlah uang yang disimpan :3500"); | |
status = tabungan.ambilUang1(4000); | |
System.out.println("Jumlah uang yang diambil : 4000"); | |
if (status) | |
System.out.println("Ok"); | |
else | |
System.out.println("Gagal"); | |
status = tabungan.ambilUang1(1600); | |
System.out.println("Jumlah uang yang diambil : 1600"); | |
if (status) | |
System.out.println("Ok"); | |
else | |
System.out.println("Gagal"); | |
tabungan.simpanUang1(2000); | |
System.out.println("Jumlah uang yang disimpan :2000"); | |
System.out.println("Saldo sekarang = " + tabungan.getSaldo1()); | |
System.out.print("1.IDR\t|\t2.USD\t|\t3.AUD\nSilahkan pilih mata Uang diatas : "); | |
satuanMataUang = input.nextLine(); | |
int saldo = tabungan.getSaldo1(); | |
if (satuanMataUang.equalsIgnoreCase("1")){ | |
System.out.println(saldo); | |
} | |
else if (satuanMataUang.equalsIgnoreCase("2")){ | |
System.out.println(tabungan.idrToUsd(saldo)); | |
} | |
else if (satuanMataUang.equalsIgnoreCase("3")){ | |
System.out.println(tabungan.idrTOAud(saldo)); | |
} | |
} | |
} |
Hasil dari program diatas adalah seperti pada gambar dibawah ini.
0 komentar:
Posting Komentar