Program Persegi Panjang
//class PersegiPanjang
public class PersegiPanjang {
public int panjang;
public int lebar;
public int luas;
public int getPanjang() {
return panjang;
}
public void setPanjang(int panjang) {
this.panjang = panjang;
}
public int getLebar() {
return lebar;
}
public void setLebar (int lebar) {
this.lebar = lebar;
}
public int getLuas() {
return panjang * lebar;
}
public int getLuas (int jumlah) {
return (panjang * lebar) * jumlah;
}
}
import javax.swing.JOptionPane;
public class Main {
public static void main (String [] args) {
PersegiPanjang p = new PersegiPanjang();
p.setLebar (Integer.parseInt (JOptionPane.showInputDialog ("Masukkan Nilai Lebar : ")));
p.setPanjang (Integer.parseInt (JOptionPane.showInputDialog ("Masukkan Nilai Panjang : ")));
JOptionPane.showMessageDialog(null, p.getLuas());
int luastotal = p.getLuas(5);
JOptionPane.showMessageDialog(null, "Total luas 5 Persegi Panjang adalah " + luastotal);
balok b = new balok();
b.setPanjang(90);
b.setLebar(10);
b.setTinggi(30);
JOptionPane.showMessageDialog(null, b.getVolume());
}
}
//class balok
public class balok extends PersegiPanjang {
private int tinggi;
public void setTinggi (int tinggi) {
this.tinggi = tinggi;
}
public int getTinggi() {
return tinggi;
}
public int getVolume() {
return panjang * lebar * tinggi;
}
}
Program atlit
//class atlit
public class atlit {
private String nama;
private String medali;
private int point;
public atlit() {
}
public void setNama(String nama){
this.nama=nama;
}
public String getNama(){
return nama;
}
public void setPoint(int point){
this.point = point;
}
public int getPoint(){
return point;
}
public String getMedali(){
if (point == 3){
medali="MENDAPATKAN MEDALI EMAS";
}else if(point == 2){
medali="MENDAPATKAN MEDALI PERAK";
}else if(point==1){
medali="Perunggu";
}else{medali="TIDAK MENDAPAT MEDALI";
}
return medali;
}
}
// class Demoatlit
import javax.swing.JOptionPane;
public class Demoatlit {
public static void main(String args[]){
atlit a = new atlit();
a.setNama (JOptionPane.showInputDialog ("Masukkan Nama : "));
a.setPoint(Integer.parseInt(JOptionPane.showInputDialog ("Masukkan Point : ")));
JOptionPane.showMessageDialog(null, "========Hasil========"+""
+ "\nNama Atlit : " +a.getNama()+"\nPerolehan Point : " +a.getPoint()
+ "\nStatus : "+a.getMedali()
+"\n=====================");
}
}
Program sepeda
//clss sepeda
public class sepeda {
int rpm = 0;
int speed = 0;
int gear = 1;
public sepeda() {
}
public int getRpm() {
return rpm;
}
public void setRpm(int newValue) {
rpm = newValue;
}
public int getGear() {
return gear;
}
public void setGear(int newValue) {
gear = newValue;
}
public int getspeedUp() {
return speed;
}
public void setspeedUp(int increment) {
speed = speed + increment;
}
public int ubahRem() {
return speed;
}
public void ubahRem(int decrement) {
speed = speed - decrement;
}
}
// class tes
import javax.swing.JOptionPane;
public class tes {
public static void main(String[] args) {
sepeda m1 = new sepeda();
sepeda m2 = new sepeda();
// Method dalam objek tsb
m1.setRpm (Integer.parseInt(JOptionPane.showInputDialog ("Masukkan Nilai Rpm pada m1 : ")));
m1.setspeedUp (Integer.parseInt(JOptionPane.showInputDialog ("Masukkan Nilai Speed pada m1 : ")));
m1.setGear (Integer.parseInt(JOptionPane.showInputDialog ("Masukkan Nilai Gear pada m1 : ")));
m2.setRpm (Integer.parseInt(JOptionPane.showInputDialog ("Masukkan Nilai Rpm pada m2 : ")));
m2.setspeedUp (Integer.parseInt(JOptionPane.showInputDialog ("Masukkan Nilai Speed pada m2 : ")));
m2.setGear (Integer.parseInt(JOptionPane.showInputDialog ("Masukkan Nilai Gear pada m2 : ")));
JOptionPane.showMessageDialog(null, "===Sepeda1==="+"\nRpm : "+m1.getRpm()
+"\nSpeed : "+m1.getspeedUp()
+"\nGear : "+m1.getGear()+ "\n \n ===Sepeda2==="+"\nRpm : "+m2.getRpm()
+"\nSpeed : "+m2.getspeedUp()
+"\nGear : "+m2.getGear());
}
}
Program disket
//class disket
public class disket {
int panjang = 10;
final int LEBAR = 10;
String warna = "kuning";
String bentuk = "segiempat";
String data = "";
disket() {
panjang= 100 ;
}
disket(int panjang1) {
this.panjang = panjang1;
}
void simpanData(String data1) {
this.data += data1;
}
void hapusData() {
data = "";
}
String bacaData() {
return data;
}
}
// class MainDisket
class MainDisket{
public static void main (String[] args) {
disket disk1 = new disket(50);
System.out.println("Panjang Disket = "+ disk1.panjang);
System.out.println("Lebar Disket = "+ disk1.LEBAR);
System.out.println("Warna Disket = "+ disk1.warna);
System.out.println("Bentuk Disket = "+ disk1.bentuk);
System.out.println("data mula-mula = "+ disk1.bacaData());
disk1.simpanData("Belajar java");
System.out.println("Setelah SimpanData = "+ disk1.bacaData());
disk1.hapusData();
System.out.println("Setelah hapusData = "+ disk1.bacaData());
}
}
Program datadiri
//class datadiri
public class datadiri {
private String nama;
private String kelas;
private String npm;
public String getNama() {
return nama;
}
public void setNama(String nama) {
this.nama = nama;
}
public String getKelas() {
return kelas;
}
public void setKelas(String kelas) {
this.kelas = kelas;
}
public String getNpm() {
return npm;
}
public void setNpm(String npm) {
this.npm = npm;
}
}
//class biodata
import javax.swing.JOptionPane;
public class biodata {
public static void main (String[]args){
datadiri bio = new datadiri();
bio.setNama (JOptionPane.showInputDialog("Nama Anda : "));
bio.setKelas (JOptionPane.showInputDialog("Kelas Anda : "));
bio.setNpm (JOptionPane.showInputDialog("Npm Anda : "));
JOptionPane.showMessageDialog(null,"Nama : "+bio.getNama()+"\nKelas : "+bio.getKelas()
+"\nNpm : "+bio.getNpm());
}
}
Jika ada kesalahan, hubungi Admin, atau silahkan berkomentar..
2 komentar
be, codingnya ini doang ? yang kata bikin class baru engga ? bingung :-?
ada ko yang pewariskan?
pewarisnya di class balok
Silahkan berkomentar sesuai topik bahasan and no spam, no iklan, no ribut, no live link !
Konversi KodeEmoticon