• Tidak ada hasil yang ditemukan

Nama : Ahmad Afifuddin Ghiffari NIM : Prodi : Sistem Informasi (RJM) Semester : 4. Latihan1. GenapGanjil.java

N/A
N/A
Protected

Academic year: 2022

Membagikan "Nama : Ahmad Afifuddin Ghiffari NIM : Prodi : Sistem Informasi (RJM) Semester : 4. Latihan1. GenapGanjil.java"

Copied!
31
0
0

Teks penuh

(1)

Nama : Ahmad Afifuddin Ghiffari NIM : 2070211019

Prodi : Sistem Informasi (RJM) Semester : 4

Praktikum : PBO

Latihan1

GenapGanjil.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 Latihan1;

import java.util.Scanner;

/**

*

* @author LabKom-2 */

public class GenapGanjil {

(2)

/*====================

program : Menentukan Bilangan Ganjil Genap Author : Ahmad Afifuddin Ghiffari

Materi : Brancing

=====================*/

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

(3)

System.out.println("Bilangan Diatas adalah Negatif Genap: ");

} }

Output:

• Positif Ganjil

• Negatif Ganjil

• Positif Genap

• Negatif Genap

(4)

Latihan2

Looping.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 Latihan2;

import java.util.Scanner;

/**

*

* @author LabKom-2 */

public class Looping { /*====================

program : Menentukan Bilangan Ganjil Genap Author : Ahmad Afifuddin Ghiffari

Materi : Looping

=====================*/

(5)

public static void main(String[]args) {

System.out.println("Perintah Looping Do-Whiile");

int a=1;

do {

System.out.println(a);

++a;

}

while (a<=10);

System.out.println("===============");

System.out.println("Perintah Looping While");

int b=10;

while(b>=1) {

System.out.println(b);

b=b-1;

}

System.out.println("===============");

System.out.println("Perintah Looping For");

int c;

for(c=1;c<=10;++c) {

System.out.println(c);

(6)

}

System.out.println("===============");

System.out.println("Penyelesaian Latihan 2 modul 1");

Scanner input=new Scanner (System.in);

int awal=2,beda=3,jml,x=1;

System.out.print("Masukkan Jumlah Kemunculan Deret : ");

jml=input.nextInt();

for(awal=2;x<=jml;++x) {

System.out.println(awal);

awal=awal+beda;

}

System.out.println("===============");

} }

Output:

Latihan3

(7)

Pecahan.java

package Latihan3;

public class Pecahan { String pembil1;

String penyebut1;

String pembil2;

String penyebut2;

public String getPembil1(){

return pembil1;

}

public void setPembil1(String pembil1){

this.pembil1 = pembil1;

}

public String getPenyebut1(){

return penyebut1;

}

public void setPenyebut1 (String penyebut1) { this.penyebut1 = penyebut1;

}

public String getPembil2(){

return pembil2;

}

public void setPembil2 (String pembil2){

this.pembil2 = pembil2;

}

(8)

public String getPenyebut2(){

return penyebut2;

}

public void setPenyebut2 (String penyebut2){

this.penyebut2 = penyebut2;

} }

RumusHitung.java

package Latihan3;

public class RumusHitung { int pembilang1;

int pembilang2;

int penyebut1;

int penyebut2;

int kpk = 0;

int x;

public void setKpk(int penyebut1, int penyebut2){

this.penyebut1 = penyebut1;

this.penyebut2 = penyebut2;

if(penyebut1 == penyebut2){

kpk = penyebut1;

} else if(penyebut1>penyebut2){

if(penyebut1 % penyebut2==0){

(9)

kpk = penyebut1;

} else{

kpk = penyebut1*penyebut2;

}

} else if(penyebut1<penyebut2){

if(penyebut2%penyebut1==0){

kpk = penyebut2;

} else{

kpk = penyebut2*penyebut1;

} } }

public int getKpk(){

return kpk;

} }

MainHitung.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 Latihan3;

(10)

import java.io.BufferedReader;

import java.io.IOException;

import java.io.InputStreamReader;

/**

*

* @author LabKom-2 */

public class MainHitung {

public static void main(String[] args) throws IOException {

BufferedReader baca = new BufferedReader(new InputStreamReader(System.in));

Pecahan nilai = new Pecahan();

RumusHitung hasil = new RumusHitung();

System.out.println("Pecahan pertama : ");

System.out.println("Masukkan nilai pembilang 1 : ");

String a = baca.readLine();

nilai.setPembil1 (a);

int pembilangPertama = Integer.parseInt(nilai.getPembil1());

System.out.println("Masukkan nilai penyebut 1 : ");

String b = baca.readLine();

nilai.setPenyebut1(b);

(11)

int penyebutPertama = Integer.parseInt(nilai.getPenyebut1());

System.out.println("\nPecahan kedua : ");

System.out.println("Masukkan nilai pembilang 2 : ");

String c = baca.readLine();

nilai.setPembil2(c);

int pembilangkedua = Integer.parseInt(nilai.getPembil2());

System.out.println("Masukkan nilai penyebut 2 : ");

String d = baca.readLine();

nilai.setPenyebut2(d);

int penyebutKedua = Integer.parseInt(nilai.getPenyebut2());

if(penyebutPertama == 0 || penyebutKedua==0) { System.out.println("\nPenyebut Error!!!!");

} else {

hasil.setKpk(penyebutPertama, penyebutKedua);

int kpk = hasil.getKpk();

System.out.println("KPK = "+kpk);

int pbSatu = ((kpk/penyebutPertama) *pembilangPertama);

int pbDua = ((kpk/penyebutKedua) *pembilangkedua);

int hasilTambah = pbSatu + pbDua;

System.out.println(pbSatu+"/"+kpk+" + "+pbDua+"/"+kpk+" = "+hasilTambah+"/"+kpk);

}

(12)

} }

Output:

Latihan 4

Class 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 Latihan4;

/**

(13)

*

* @author LabKom-2 */

public class ClassMahasiswa { 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;

}

public String getNama () { return nama;

}

public void setNama(String nama) { this.nama = nama;

}

(14)

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 ClassMahasiswa(String nim, String nama, int tinggi) { this.nim = nim;

this.nama = nama;

this.tinggi = tinggi;

}

public ClassMahasiswa(String nim, String nama, int tinggi, boolean pindahan) { this(nim,nama,tinggi); //memanggil konstruktor 3 parameter

this.pindahan = pindahan;

(15)

} }

Class MainMhs.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 Latihan4;

/**

*

* @author LabKom-2 */

public class ClassMainMhs {

public static void main(String[] args) {

ClassMahasiswa m1 = new ClassMahasiswa("6701148000", "Angga", 166);

m1.setPindahan(false);

//menampilkan data

System.out.println("Data Mahasiswa 1");

System.out.println("NIM: "+m1.getNim());

(16)

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");

} } }

MainMhs.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.

(17)

*/

package Latihan4;

/**

*

* @author LabKom-2 */

public class MainMhs {

public static void main(String[] args) {

ClassMahasiswa m1 = new ClassMahasiswa("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");

}

ClassMahasiswa m2 = new ClassMahasiswa("6701148001", "Rena", 154, true);

(18)

//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");

} } }

Latihan 5

(19)

ClassChild.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 latihan5;

/*

* 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 latihan5;

/**

*

* @author PC-13 */

public class ClassChild extends ClassParents { }

ClassParent.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 latihan5;

/**

(20)

*

* @author PC-13 */

public class ClassParents { //membuat atribut private String alamat;

//method

public String getAlamat() {

return alamat; //untuk mengembalikan nilai }

public void setAlamat (String alamat) {

this.alamat = alamat; //untuk mengubah nilai }

}

Program.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 latihan5;

/**

*

* @author PC-13 */

(21)

public class Program {

public static void main (String[]args){

ClassChild anak1 = new ClassChild();

anak1.setAlamat("Pemondokan Somer House");

System.out.println("Alamat :" +anak1.getAlamat());

} } Hasil

Latihan 6 DoraMini.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 Latihan6;

(22)

/**

*

* @author LabKom-2 */

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, Say"+getNama()+":D");

} }

MainRobot.Java

/*

(23)

* 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 Latihan6;

/**

*

* @author LabKom-2 */

public class MainRobot {

public static void main(String[] args) {

DoraMini dora1 = new DoraMini ("Dora Mini","Noval",2022);

dora1.displayData();

} }

(24)

Robot.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 Latihan6;

/**

*

* @author LabKom-2 */

public abstract class Robot {

protected String nama;

(25)

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);

}

(26)

public abstract void setNama();

public abstract void displayData();

}

Latihan 7

Doraemon.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 Latihan7;

/**

*

* @author LabKom-2 */

public interface Doraemon { public void sayDora();

public void displayKantongAjaib();

}

(27)

Doramini.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 Latihan7;

/**

*

* @author LabKom-2 */

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;

(28)

}

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());

}

(29)

@Override

public void displayData(){

setNama();

System.out.println("Pemilik :"+getPemilik());

setTahunPembuatan();

sayDora();

displayKantongAjaib();

}

@Override

public void sayDora(){

System.out.println("\nHallo, Saya "+getNama());

}

@Override

public void displayKantongAjaib(){

System.out.println("Saya juga seperti DoraEmon yang memiliki kantung ajaib");

} }

Robot.java

/*

* To change this license header, choose License Headers in Project Properties.

* To change this template file, choose Tools | Templates

(30)

* and open the template in the editor.

*/

package Latihan7;

/**

*

* @author LabKom-2 */

public interface Robot { public void setNama();

public void setTahunPembuatan();

public void displayData();

}

main.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 Latihan7;

/**

*

* @author LabKom-2

(31)

*/

public class Main {

public static void main(String[] args) {

Doramini dm = new Doramini("Doramini", "Noval", 2030);

dm.displayData();

} }

output

Referensi

Dokumen terkait

To use the template files, simply download the zip file containing the set of templates and save them to your templates folder for the next time you create a Revit family or start a

* To change this license header, choose License Headers in Project Properties.. * To change this template file, choose Tools

PLN (Persero) Pengendalian dan Pembangkitan Keramasan Palembang dengan judul “ Perbandingan Tegangan Tembus yang Terjadi Pada Minyak Transformator Berdasarkan Usia

Perilaku permintaan konsumen terhadap barang dan jasa akan dipengaruhi oleh beberapa faktor, diantaranya: pendapatan, selera konsumen, dan harga barang, disaat

Kecerdasan Emosioal Dan Komunikasi Terhadap Kinerja Anggota Satuan Polisi Pamong Praja Kota Pangkalpinang ”, i ni, tidak terdapat karya sebelumnya yang pernah

Siregar (2004) menyebutkan keunggulan ikan lele dibandingkan dengan bahan makanan hewani lain diantaranya adalah komponen daging ikan mudah dicerna dan diserap

Belajar (Learning Cycle) untuk Meningkatkan Keterampilan Proses Sains Siswa Kelas X- 2 SMA Negeri 3 Surakarta Tahun Pelajaran 2010/2011” pada penelitian tersebut

Melihat latar belakang masalah yaitu tingginya angka kejadian karies di kabupaten Sukoharjo berdasarkan riset yang telah di lakukan Dinas Kesehatan Kabupaten