Skip to content

Instantly share code, notes, and snippets.

@uluumbch
Last active September 29, 2021 09:13
Show Gist options
  • Save uluumbch/fe737d3c2715d714f16dd6fa501fd372 to your computer and use it in GitHub Desktop.
Save uluumbch/fe737d3c2715d714f16dd6fa501fd372 to your computer and use it in GitHub Desktop.
program java sederhana dengan constructor, encapsulasi, dan input.
package praktikum4.soal1;
public class Buku {
private String judul;
private String penulis;
private int tahun;
public Buku(String j, String p, int t) {
judul = j;
penulis = p;
tahun = t;
}
public void display() {
System.out.println("Detail Buku:");
System.out.println("Judul adalah " + judul);
System.out.println("Penulis adalah " + penulis);
System.out.println("Tahun Terbit adalah " + tahun);
}
}
package praktikum4.soal1;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
String judul;
String penulis;
int tahun;
Scanner sc = new Scanner(System.in);
System.out.println("Judul: ");
judul = sc.nextLine();
System.out.println("Penulis: ");
penulis = sc.nextLine();
System.out.println("Tahun terbit: ");
tahun = sc.nextInt();
Buku buku = new Buku(judul, penulis, tahun);
buku.display();
sc.close();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment