LAPORAN PRAKTIKUM
PEMROGRAMAN BERBASIS OBJEK SEMESTER 4
DISUSUN OLEH :
Nama : Ahmad Faiz Abdillah NIM : 2070231050
Kelas : Reguler Malam B1
PROGRAM STUDI TEKNIK INFORMATIKA
FAKULTAS TEKNIK UNIVERSITAS KRISNADWIPAYANA
2022
KETERANGAN :
Mengikuti Praktikum di Kloter 2 Absen di Kloter 1
NAMA FILE DAN CLASSNYA
FOLDER belajar.java
GANJIL GENAP /*
*
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 belajarjava;
import java.util.Scanner;
/**
*
*
@author Bimo */public class GanjilGenap {
/*=============================================
Program : Menentukan Bilangan Ganjil Genap Author
Materi :
=============================================*/ public static void main(String[]args){
System.out.println("Penyelesaian Latihan 1 Modul 1");
Scanner a =new Scanner(System.in);
System.out.print("Masukan Bilangan : "); int bil=a.nextInt();
if(bil%2==1 && bil>0) {
System.out.println("Bilangan Diatas adalah Positif Ganjil");
}
else if (bil%2==-1 && bil<0) {
System.out.println("Bilangan Diatas adalah Negatif Ganjil");
}
else if (bil%2==0 && bil>0) {
System.out.println("Bilangan Diatas adalah Positif Genap");
} else {
System.out.println("Bilangan Diatas adalah Negatif Genap");
} } }
Output Ganjil Genap
MAHASISWA
/*
*
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 belajarjava;
/**
*
*
@author Bimo */public class Mahasiswa {
private String nim; private String nama; private int tinggi;
private boolean pindahan;
public String getNim() {
return nim;
}
public void setNim(String nim) {
this.nim = nim;
MAIN MAHASISWA }
public String getNama() {
return nama;
}
public void setNama(String nama) {
this.nama = nama;
}
public int getTinggi() {
return tinggi;
}
public void setTinggi(int tinggi) {
this.tinggi = tinggi;
}
public boolean isPindahan() {
return pindahan;
}
public void setPindahan(boolean pindahan) {
this.pindahan = pindahan; }
public Mahasiswa(String nim, String nama, int tinggi) {
this.nim = nim; this.nama
= nama; this.tinggi = tinggi;
}
public Mahasiswa(String nim, String nama, int tinggi, boolean pindahan) {
this(nim,nama,tinggi); //memanggil konstruktor 3 parameter this.pindahan = pindahan;
} }
/*
*
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 belajarjava;
/**
*
*
@author Bimo */public class Mainmhs {
public static void main(String[]args) {
Mahasiswa m1 = new Mahasiswa("6701148000", "Angga", 166);
m1.setPindahan(false);
//menampilkan data
System.out.println("Data Mahasiswa 1");
System.out.println("NIM: "+m1.getNim());
System.out.println("Nama: "+m1.getNama());
System.out.println("Tinggi Badan: "+m1.getTinggi()); if(m1.isPindahan()) {
System.out.println("Mahasiswa pindahan");
} else {
System.out.println("Mahasiswa reguler");
}
Mahasiswa m2 = new Mahasiswa("6701148001", "Rena", 154, true);
//menampilkan data System.out.println();
System.out.println("Data Mahasiswa 2");
System.out.println("NIM: "+m2.getNim());
System.out.println("Nama: "+m2.getNama());
System.out.println("Tinggi Badan: "+m2.getTinggi());
if(m2.isPindahan()) {
System.out.println("Mahasiswa pindahan");
} else {
System.out.println("Mahasiswa reguler");
} } }
Output Mahasiswa
SEPEDA MOTOR /*
*
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 belajarjava;
/**
*
*
@author Bimo */public class SepedaMotor {
private String merk;
private long harga;
public void setMerk(String merkMotor){ merk="Kawasaki";
}
public String getMerk(){
return merk;
}
public long Harga(long hargaMotor){
return harga = hargaMotor;
}
public static void main (String[]args){ SepedaMotor motor = new SepedaMotor();
motor.setMerk("Suzuki");
System.out.println("Motor ini ber-merk : "+motor.getMerk());
System.out.println("Motor ini berharga : "+motor.Harga(11000000));
} }
Output Sepeda Motor
INHERITANCE 1 /*
*
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 belajarjava;
/**
*
* @author Bimo
*/ public class Inheritance1 {
private int nilaiSuper;
public Inheritance1(int nilaiInduk) {
this.nilaiSuper = nilaiInduk; }
public int getNilaiInduk() {
return nilaiSuper;
}
private void methodPrivate() {
System.out.println("Ini method private");
}
protected void methodProtected() {
System.out.println("Ini method protected");
} }
INHERITANCE 2 /*
*
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 belajarjava;
/**
*
*
@author Bimo*/ public class Inheritance2 extends Inheritance1 { private final double nilaiSub;
public Inheritance2(int nilaiSuper, double nilaiSub) {
super(nilaiSuper);
this.nilaiSub = nilaiSub; }
public void methodSub() {
super.methodProtected();
System.out.println("Nilai Super : "+super.getNilaiInduk());
} }
INHERITANCE MAIN
/*
*
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
belajarjava;
/**
*
*
@author Bimo*/ public class
InheritanceMain {
/**
*
@param args the command line arguments */public static void main(String[] args) {
//Bentuk objek Superclass
System.out.println("Objek Superclass");
Inheritance1 sup = new Inheritance1(5);
System.out.println("Nilai Super : "+sup.getNilaiInduk()); sup.methodProtected();
//Bentuk obejk Subclass
System.out.println("\n Objek subclass");
Inheritance2 sub = new Inheritance2(10,9.5);
System.out.println("Pemanggilan method superclass dari objek subclass");
System.out.println("Nilai Super : "+sub.getNilaiInduk()); sub.methodProtected();
} }
Ouput Inheritance
METHOD 1 /*
*
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 belajarjava;
/**
*
*
@author Bimo */public class Method1 {
static void cetak(){
System.out.print("I Love");
} /**
* @param args the command line arguments */
public static void main(String[] args) {
cetak();
System.out.println("Java");
} }
Ouput Method 1
METHOD 2 /*
*
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 belajarjava;
/**
*
*
@author Bimo */public class Method2 {
static void cetak (int a){
System.out.println("Nilai X = "+a);
}
public static void main (String[]args){ int x;
for (x=0;x<3;x++){ cetak(x);
}
System.out.println("Nilai X terakhir : "+x);
} }
Ouput Method 2
BELAJAR ENKAPSULASI /*
*
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
belajarjava;
/**
*
*
@author Bimo*/ public class
BelajarEnkapsulasi {
public static void main(String[]args) {
BelajarEnkapsulasi ls = new BelajarEnkapsulasi();
ls.setAlas(5); ls.setTinggi(7);
System.out.println("Alas Segitiga : "+ls.getAlas());
System.out.println("Tinggi Segitiga : "+ls.getTinggi());
ls.setLuasSegitiga(ls.getAlas(), ls.getTinggi());
System.out.println("Luas Segitiga : "+ls.getLuasSegitiga());
}
private int alas,tinggi;
private double luassegitiga;
public void setAlas(int alas){
this.alas=alas;
}
public int getAlas(){
return alas;
}
public void setTinggi(int tinggi){
this.tinggi=tinggi;
}
public int getTinggi(){
return tinggi;
}
public void setLuasSegitiga(int alas, int tinggi){
luassegitiga=0.5*(double)(alas*tinggi);
}
public double getLuasSegitiga(){
return luassegitiga;
} }
Ouput Enkapsulasi
BELAJAR POLYMORPHISM
/*
*
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 belajarjava;
/**
*
*
@author Bimo */public class BelajarPolymorphism { public void Tampil()
{
System.out.println("I Love Java");
}
public void Tampil(int i) {
System.out.println("Method dengan 1 parameter - "+i);
}
public void Tampil(int i, int j) {
System.out.println("Method dengan 2 parameter - "+i+" Dan "+j);
}
public void Tampil(String kata) {
System.out.println("Saat ini kita sedang "+kata);
}
public static void main(String[]args) {
BelajarPolymorphism overloadingobjek=new BelajarPolymorphism();
overloadingobjek.Tampil(); overloadingobjek.Tampil(8);
overloadingobjek.Tampil(6, 7); overloadingobjek.Tampil("Belajar Overloading");
} }
Ouput Polymorphism
FOLDER Interface.java
DORAEMON /*
*
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 Interface;
/**
*
*
@author Bimo */public interface Doraemon {
public void sayDora(); public void displayKantongAjaib();
}
ROBOT /*
*
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 Interface;
/**
*
* @author Bimo
*/ public interface Robot {
public void setNama(); public void setTahunPembuatan(); public void displayData();
}
DORAMINI /*
*
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 Interface;
/**
*
*
@author Bimo */public class DoraMini implements Robot, Doraemon {
private String nama; private String pemilik;
protected int tahun;
DoraMini(String Nama, String Pemilik, int Tahun) {
nama = Nama;
pemilik = Pemilik; tahun = Tahun;
}
public String getNama() {
return nama;
}
public String getPemilik() {
return pemilik;
}
public void setPemilik(String pemilik) {
this.pemilik = pemilik;
}
protected int getTahun() {
return tahun;
}
@Override
public void setNama() {
System.out.println("Nama Robot : "+getNama());
}
@Override
public void setTahunPembuatan() {
System.out.println("Tahun Pembuatan : "+getTahun());
}
@Override
public void displayData() {
setNama();
System.out.println("Pemilik : "+getPemilik());
setTahunPembuatan(); sayDora(); displayKantongAjaib();
}
@Override
public void sayDora() {
System.out.println("\nHalo, Saya "+getNama());
}
@Override
public void displayKantongAjaib() {
System.out.println("Saya juga seperti Doraemon yang memiliki kantong ajaib");
} }
MAIN /*
*
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 Interface;
/**
*
*
@author Bimo */ public class Main {/**
*
@param args the command line arguments */public static void main(String[] args) {
DoraMini dm = new DoraMini("DoraMini", "Bimo", 2030); dm.displayData();
} }
Ouput File Interface
FOLDER Abstrak.java DORAMINI /*
*
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 Abstrak;
/**
*
*
@author Bimo */public class DoraMini extends Robot
{ public DoraMini(String Nama, String Pemilik, int Tahun) {
super(Nama, Pemilik, Tahun);
}
@Override
public void setNama() {
System.out.println("Nama Robot : "+super.getNama());
}
@Override
public void displayData() {
setNama();
System.out.println("Pemilik : "+getPemilik());
super.setTahunPembuatan(tahun); sayDora();
}
public void sayDora() {
System.out.println("\nHalo, Saya "+getNama()+":D");
} }
ROBOT /*
*
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 Abstrak;
/**
*
*
@author Bimo */ public abstract class Robot{
protected String nama;
protected String pemilik;
protected int tahun;
protected Robot(String Nama, String Pemilik, int Tahun) {
nama = Nama;
pemilik = Pemilik;
tahun = Tahun;
}
protected String getNama() {
return nama;
}
protected String getPemilik() {
return pemilik;
}
protected int getTahun() {
return tahun;
}
public void setTahunPembuatan(int Tahun)
{
System.out.println("Tahun Pembuatan : "+tahun);
}
public abstract void setNama();
public abstract void
displayData(); }
MAIN ROBOT /*
*
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 Abstrak;
/**
*
*
@author Bimo */public class MainRobot {
public static void main(String[]args) {
DoraMini doral = new DoraMini("Dora Mini", "Bimo", 2019); doral.displayData();
} }
Output Folder Abstrak
TUGAS 1 (2.4)
GAME CARACHTER /*
*
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 MortalKombat;
/**
*
*
@author Bimo */public class GameCharacter { private String name; private int lifePoint; private int attackHitPoint;
private int attackKickPoint;
public GameCharacter (String name, int attackHitPoint, int attackKickPoint){
this.name = name; this.lifePoint = 100; this.attackHitPoint = attackHitPoint;
this.attackKickPoint = attackKickPoint;
}
public void hit (GameCharacter karB){
karB.lifePoint -= this.attackHitPoint;
}
public void kick (GameCharacter karB){ karB.lifePoint -=
this.attackKickPoint;
}
public int getLifePoint(){
return lifePoint;
}
public String getName(){
return name;
} }
MAIN /*
*
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 MortalKombat;
/**
*
*
@author Bimo */public class Main {
/**
*
@param args the command line arguments */public static void main(String[] args) {
GameCharacter raiden = new GameCharacter("Raiden",10,20);
GameCharacter subZero = new GameCharacter("Sub-Zero",5,25);
System.out.println("-->Game Character 1<--");
System.out.println("Name : " +raiden.getName());
System.out.println("LifePoint : " +raiden.getLifePoint());
System.out.println("-->Game Character 2<--");
System.out.println("Name : " +subZero.getName());
System.out.println("LifePoint : " +subZero.getLifePoint()+"\n");
System.out.println("-->Objek Raiden memulai pertarungan dengan melakukan tendangan pada objek Sub-Zero.");
raiden.kick(subZero);
System.out.println("LifePoint : " +raiden.getLifePoint());
System.out.println("LifePoint : " +subZero.getLifePoint()+"\n");
System.out.println("-->Objek Sub-Zero melakukan perlawanan dengan menendang balik objek Raiden"); subZero.kick(raiden);
System.out.println("LifePoint : " +raiden.getLifePoint());
System.out.println("LifePoint : " +subZero.getLifePoint()+"\n");
System.out.println("-->Objek Sub-Zero menyerang Raiden dengan pukulan berturut-turut sebanyak 3x");
for(int i=0; i<3; i++){
subZero.hit(raiden);
}
System.out.println("LifePoint : " +raiden.getLifePoint());
System.out.println("LifePoint : " +subZero.getLifePoint()+"\n");
System.out.println("-->Pertarungan diakhiri oleh Raiden dengan melakukan tendangan beruntun 4x pada objek Sub-Zero"); for(int i=0; i<4; i++){
raiden.kick(subZero);
}
System.out.println("LifePoint : " +raiden.getLifePoint());
System.out.println("LifePoint : " +subZero.getLifePoint()+"\n");
System.out.println("Hasil Akhir");
String winner = raiden.getLifePoint() > subZero.getLifePoint()?
"Raiden" : "Sub-Zero";
System.out.println("Pemenangnya Adalah : " +winner);
System.out.println("Dengan LifePoint : " +raiden.getLifePoint());
} }
Ouput Tugas 1
TUGAS 2 (3.4) TIM /*
*
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 Jkt48;
import java.util.ArrayList;
/**
*
*
@author Bimo */public class Tim { private String nama; private ArrayList<Member> m = new ArrayList<Member>();
public String getNama() { return this.nama;
}
public void setNama(String nama) { this.nama = nama;
}
public void setMember(Member m) { this.m.add(m);
}
public void displayFullMember() { for(int i = 0; i <
m.size(); i++){ if(!(m.get(i) instanceof Trainee)){
System.out.println("Nama: " + m.get(i).nama);
System.out.println("Umur: " + m.get(i).umur);
} } }
public void displayTrainee() { for(int i = 0; i <
m.size(); i++){ if(m.get(i) instanceof Trainee){
System.out.println("Nama: " + m.get(i).nama);
System.out.println("Umur: " + m.get(i).umur);
System.out.println("Lama Training: " + ((Trainee)(m.get(i))).getLamaTraining());
} } } }
MEMBER /*
*
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 Jkt48;
/**
*
*
@author Bimo */public class Member { protected String nama;
protected int umur;
Member(String nama, int umur){
this.nama = nama;
this.umur = umur;
}
public void setUmur(int umur){
this.umur = umur;
}
public void display(){
System.out.println("Nama"+this.nama);
System.out.println("Umur"+this.umur);
} }
TRAINEE /*
*
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 Jkt48;
/**
*
*
@author Bimo */public class Trainee extends Member{ private int lamaTraining;
Trainee(String nama, int umur, int lamaTraining) { super(nama, umur); this.lamaTraining = lamaTraining;
}
public int getLamaTraining(){
return this.lamaTraining;
}
public void display() { super.display();
System.out.println("Lama Training: " + this.lamaTraining);
} }
MAIN /*
*
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 Jkt48;
import Jkt48.Member;
/**
*
*
@author Bimo */ public class Main {/**
*
@param args the command line arguments */public static void main(String[] args) { Tim t = new Tim();
t.setNama("Tim T");
Member m1 = new Member("Melody", 23);
Member m2 = new Member("Haruka", 23);
Member m3 = new Member("Kinal", 19);
Trainee t1 = new Trainee("Alicia", 17,3); Trainee t2
= new Trainee("Indah", 17, 5);
t.setMember(m1);
t.setMember(m2);
t.setMember(m3);
t.setMember(t1);
t.setMember(t2);
System.out.println("Full Member:"); t.displayFullMember();
System.out.println("\nTrainee"); t.displayTrainee();
} }
Output Tugas 2