INHERITANCE/PEWARISAN
Tujuan
Melengkapi laporan praktikum Pemrograman
Berorientasi Objek modul 4.
Landasan
Teori
Modul praktikum Pemrograman
Berorientasi Objek.
Alat
dan Bahan
1 Unit PC
Netbeans
Modul praktikum PBO
Langkah – Langkah Pratikum
Percobaan 1
Bicycle.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 modul4;
/**
*
* @author irwans
*/
public class Bicycle {
int cadance;
int speed;
int gear;
void changeCadance (int newValue){
cadance = newValue;
}
void changeGear (int newValue) {
gear = newValue;
}
void speedUp (int increment){
speed = speed+increment;
}
void applyBreak (int decrement){
speed = speed-decrement;
}
void printStates(){
System.out.println("Speed= "+speed+","+"Gear= "+gear+","+"Cadance= "+cadance);
}
void changCadance(int i) {
throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
}
void changeGea(int i) {
throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
}
}
* 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 modul4;
/**
*
* @author irwans
*/
public class Bicycle {
int cadance;
int speed;
int gear;
void changeCadance (int newValue){
cadance = newValue;
}
void changeGear (int newValue) {
gear = newValue;
}
void speedUp (int increment){
speed = speed+increment;
}
void applyBreak (int decrement){
speed = speed-decrement;
}
void printStates(){
System.out.println("Speed= "+speed+","+"Gear= "+gear+","+"Cadance= "+cadance);
}
void changCadance(int i) {
throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
}
void changeGea(int i) {
throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
}
}
MountainBike.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 modul4;
/**
*
* @author irwans
*/
public class MountainBike extends Bicycle{
static boolean compas = true;
public static void main (String []args){
MountainBike mountBike = new MountainBike();
mountBike.speedUp (100);
mountBike.changeGear(6);
mountBike.changeCadance(9);
mountBike.printStates();
mountBike.lookCompas();
}
void lookCompas (){
System.out.println("ada kompasnya?? "+compas);
}
}
* 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 modul4;
/**
*
* @author irwans
*/
public class MountainBike extends Bicycle{
static boolean compas = true;
public static void main (String []args){
MountainBike mountBike = new MountainBike();
mountBike.speedUp (100);
mountBike.changeGear(6);
mountBike.changeCadance(9);
mountBike.printStates();
mountBike.lookCompas();
}
void lookCompas (){
System.out.println("ada kompasnya?? "+compas);
}
}
Tugas Percobaan 1
Latihan 1
RoadBicycle.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 modul4;
/**
*
* @author irwans
*/
public class RoadBicycle {
int cadance;
int speed;
int gear;
void changeCadance (int newValue){
cadance = newValue;
}
void changeGear (int newValue){
gear = newValue;
}
protected void speedUp (int increment){
speed = speed+increment;
}
void applyBreak (int decrement){
speed = speed - decrement;
}
void printStates(){
System.out.println("Speed = "+speed+" "+ "Gear = "+gear+", "+"Cadance = "+cadance+"");
}
}
* 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 modul4;
/**
*
* @author irwans
*/
public class RoadBicycle {
int cadance;
int speed;
int gear;
void changeCadance (int newValue){
cadance = newValue;
}
void changeGear (int newValue){
gear = newValue;
}
protected void speedUp (int increment){
speed = speed+increment;
}
void applyBreak (int decrement){
speed = speed - decrement;
}
void printStates(){
System.out.println("Speed = "+speed+" "+ "Gear = "+gear+", "+"Cadance = "+cadance+"");
}
}
MountainBikedemo1
/*
* 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 modul4;
/**
*
* @author irwans
*/
public class MountainBikedemo1 extends RoadBicycle{
static int diskBreak = 2;
public static void main (String []args){
MountainBikedemo1 mountBike = new MountainBikedemo1();
mountBike.speedUp (100);
mountBike.changeGear(6);
mountBike.changeCadance(9);
mountBike.printStates();
mountBike.lookCompas();
}
void lookCompas (){
System.out.println("diskBreak nya ada? "+diskBreak);
}
}
* 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 modul4;
/**
*
* @author irwans
*/
public class MountainBikedemo1 extends RoadBicycle{
static int diskBreak = 2;
public static void main (String []args){
MountainBikedemo1 mountBike = new MountainBikedemo1();
mountBike.speedUp (100);
mountBike.changeGear(6);
mountBike.changeCadance(9);
mountBike.printStates();
mountBike.lookCompas();
}
void lookCompas (){
System.out.println("diskBreak nya ada? "+diskBreak);
}
}
Latihan 2
TandemBike.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 modul4;
/**
*
* @author irwans
*/
public class TandemBike {
int cadance;
int speed;
int gear;
void changeCadance (int newValue){
cadance = newValue;
}
void changeGear (int newValue){
gear = newValue;
}
protected void speedUp (int increment){
speed = speed+increment;
}
void applyBreak (int decrement){
speed = speed - decrement;
}
void printStates(){
System.out.println("Speed = "+speed+" "+ "Gear = "+gear+", "+"Cadance = "+cadance+"");
}
}
* 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 modul4;
/**
*
* @author irwans
*/
public class TandemBike {
int cadance;
int speed;
int gear;
void changeCadance (int newValue){
cadance = newValue;
}
void changeGear (int newValue){
gear = newValue;
}
protected void speedUp (int increment){
speed = speed+increment;
}
void applyBreak (int decrement){
speed = speed - decrement;
}
void printStates(){
System.out.println("Speed = "+speed+" "+ "Gear = "+gear+", "+"Cadance = "+cadance+"");
}
}
MountainBikedemo2.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 modul4;
/**
*
* @author irwans
*/
public class MountainBikedemo2 extends TandemBike{
static int sadel = 2;
public static void main (String []args){
MountainBikedemo2 mountBike = new MountainBikedemo2();
mountBike.speedUp (100);
mountBike.changeGear(6);
mountBike.changeCadance(9);
mountBike.printStates();
mountBike.lookCompas();
}
void lookCompas (){
System.out.println("sadel nya ada? "+sadel);
}
}
* 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 modul4;
/**
*
* @author irwans
*/
public class MountainBikedemo2 extends TandemBike{
static int sadel = 2;
public static void main (String []args){
MountainBikedemo2 mountBike = new MountainBikedemo2();
mountBike.speedUp (100);
mountBike.changeGear(6);
mountBike.changeCadance(9);
mountBike.printStates();
mountBike.lookCompas();
}
void lookCompas (){
System.out.println("sadel nya ada? "+sadel);
}
}
Orang.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 modul4.latiahan2;
/**
*
* @author irwans
*/
public class Orang {
private String nama;
private int usia;
public Orang(String nama, int usia){
this.nama = nama;
this.usia = usia;
}
public void info(){
System.out.println("Nama : "+nama);
System.out.println("Usia: "+usia);
}
}
* 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 modul4.latiahan2;
/**
*
* @author irwans
*/
public class Orang {
private String nama;
private int usia;
public Orang(String nama, int usia){
this.nama = nama;
this.usia = usia;
}
public void info(){
System.out.println("Nama : "+nama);
System.out.println("Usia: "+usia);
}
}
Pegawai.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 modul4.latiahan2;
/**
*
* @author irwans
*/
public class Pegawai extends Orang{
protected String noPegawai;
public Pegawai(String noPegawai, String nama, int usia){
super(nama, usia);
this.noPegawai = noPegawai;
}
public void info(){
System.out.println("No.Pegawai : "+this.noPegawai);
super.info();
}
}
* 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 modul4.latiahan2;
/**
*
* @author irwans
*/
public class Pegawai extends Orang{
protected String noPegawai;
public Pegawai(String noPegawai, String nama, int usia){
super(nama, usia);
this.noPegawai = noPegawai;
}
public void info(){
System.out.println("No.Pegawai : "+this.noPegawai);
super.info();
}
}
AksesKonstSuperClass.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 modul4.latiahan2;
/**
*
* @author irwans
*/
public class AksesKonstSuperClass {
public static void main (String[]args){
Pegawai karyawan = new Pegawai("1245", "Mr.Budi", 24);
karyawan.info();
}
}
* 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 modul4.latiahan2;
/**
*
* @author irwans
*/
public class AksesKonstSuperClass {
public static void main (String[]args){
Pegawai karyawan = new Pegawai("1245", "Mr.Budi", 24);
karyawan.info();
}
}
Hasil Percobaan diatas seperti gambar
dibawah ini:
Latihan 3
mahasiswaTi.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 modul4.latihan3;
/**
*
* @author irwans
*/
public class mahasiswaTi {
private String nama;
private String usia;
public mahasiswaTi(String nama, String usia){
this.nama = nama;
this.usia = usia;
}
public void info(){
System.out.println("Nama : "+nama);
System.out.println("Usia: "+usia);
}
}
* 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 modul4.latihan3;
/**
*
* @author irwans
*/
public class mahasiswaTi {
private String nama;
private String usia;
public mahasiswaTi(String nama, String usia){
this.nama = nama;
this.usia = usia;
}
public void info(){
System.out.println("Nama : "+nama);
System.out.println("Usia: "+usia);
}
}
mhasiswaTii.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 modul4.latihan3;
/**
*
* @author irwans
*/
public class mhasiswaTii extends mahasiswaTi{
protected String nim;
protected String jurusan;
public mhasiswaTii(String nim, String jurusan, String nama, String usia){
super(nama, usia);
this.nim = nim;
this.jurusan= jurusan;
}
public void info(){
System.out.println("NIM Mahsiswa : "+this.nim);
System.out.println("Konsentrasi jurusan : "+this.jurusan);
super.info();
}
}
* 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 modul4.latihan3;
/**
*
* @author irwans
*/
public class mhasiswaTii extends mahasiswaTi{
protected String nim;
protected String jurusan;
public mhasiswaTii(String nim, String jurusan, String nama, String usia){
super(nama, usia);
this.nim = nim;
this.jurusan= jurusan;
}
public void info(){
System.out.println("NIM Mahsiswa : "+this.nim);
System.out.println("Konsentrasi jurusan : "+this.jurusan);
super.info();
}
}
AksesMahasiswaTi.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 modul4.latihan3;
/**
*
* @author irwans
*/
public class AksesMahasiswaTi {
public static void main (String[]args){
mhasiswaTii itu = new mhasiswaTii("L200080011", "Rekayasa Perangkat Lunak", "Anggi", "19 Th");
itu.info();
}
}
* 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 modul4.latihan3;
/**
*
* @author irwans
*/
public class AksesMahasiswaTi {
public static void main (String[]args){
mhasiswaTii itu = new mhasiswaTii("L200080011", "Rekayasa Perangkat Lunak", "Anggi", "19 Th");
itu.info();
}
}
Hasil Percobaan diatas seperti gambar
dibawah ini:
TUGAS
Tugas 1
angkaAngka.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 modul4.tugas;
import java.util.Scanner;
/**
*
* @author irwans
*/
public class angkaAngka {
public static void main (String[]args){
Scanner input = new Scanner(System.in);
System.out.println("Angka pertama: ");
int bil1 = input.nextInt();
System.out.println("Angka kedua: ");
int bil2 = input.nextInt();
if ((int) bil1 >(int)bil2){
System.out.println("Urutan yang benar adalah " +bil2+ ", " +bil1);
}
else{
System.out.println("Urutan yang benar adalah " +bil1+ ", " +bil2);
}
}
}
* 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 modul4.tugas;
import java.util.Scanner;
/**
*
* @author irwans
*/
public class angkaAngka {
public static void main (String[]args){
Scanner input = new Scanner(System.in);
System.out.println("Angka pertama: ");
int bil1 = input.nextInt();
System.out.println("Angka kedua: ");
int bil2 = input.nextInt();
if ((int) bil1 >(int)bil2){
System.out.println("Urutan yang benar adalah " +bil2+ ", " +bil1);
}
else{
System.out.println("Urutan yang benar adalah " +bil1+ ", " +bil2);
}
}
}
Hasil Percobaan diatas seperti gambar
dibawah ini:
Tugas 2
Kucing.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 Tugas;
/**
*
* @author irwans
*/
public class Kucing {
String warna;
String nama;
int umur;
public Kucing(String warna, String nama, int umur){
this.warna=warna;
this.nama=nama;
this.umur=umur;
}
public void info(){
System.out.println("Kucing warna:" +warna);
System.out.println("Kucing nama: "+ nama);
System.out.println("Kucing umur:" +umur);
}
}
* 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 Tugas;
/**
*
* @author irwans
*/
public class Kucing {
String warna;
String nama;
int umur;
public Kucing(String warna, String nama, int umur){
this.warna=warna;
this.nama=nama;
this.umur=umur;
}
public void info(){
System.out.println("Kucing warna:" +warna);
System.out.println("Kucing nama: "+ nama);
System.out.println("Kucing umur:" +umur);
}
}
KucingRumahan.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 Tugas;
/**
*
* @author Irwans
*/
public class KucingRumahan extends Kucing {
int kumis;
public KucingRumahan(String warna, String nama, int umur, int kumis){
super(warna, nama, umur);
this.kumis=kumis;
}
public void RawatKumis(){
kumis++;
System.out.println("Panjang kumis:" +kumis );
super.info();
}
}
* 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 Tugas;
/**
*
* @author Irwans
*/
public class KucingRumahan extends Kucing {
int kumis;
public KucingRumahan(String warna, String nama, int umur, int kumis){
super(warna, nama, umur);
this.kumis=kumis;
}
public void RawatKumis(){
kumis++;
System.out.println("Panjang kumis:" +kumis );
super.info();
}
}
AksesKucingRumahan.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 Tugas;
/**
*
* @author Irwans
*/
public class AksesKucingRumahan {
public static void main(String[]args){
KucingRumahan cat = new KucingRumahan("Hitam","pussy",2,5);
cat.RawatKumis();
}
}
* 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 Tugas;
/**
*
* @author Irwans
*/
public class AksesKucingRumahan {
public static void main(String[]args){
KucingRumahan cat = new KucingRumahan("Hitam","pussy",2,5);
cat.RawatKumis();
}
}
Hasil Percobaan diatas seperti gambar
dibawah ini:
0 komentar:
Posting Komentar