Praktikum Ke Dua PSBO

Di Praktikum kedua ini kami belajar tentang “Konstruksi Class“, kemudian dijelaskan tentang UML dengan membahas Class Diagram, kemudian dijelaskan tentang-tentang struktur dalam penulisan program di java, misalnya: constructor, method, dll….
Kemudian latihan dengan membuat program dengan java, mulai nyoba pake netbeans, tidak pake notepad lagi…..
nih sourcecode programnya sbb:

public class Book {

    private String title;
    private String author;
    private int year;

    public Book() {
        title = "A Book";
        author = "someone";
        year = 1000;
        System.out.println("Object Book dengan konstruktor standar telah dibuat");
    }

    public Book(String bookTitle, String bookAuthor, int bookYear) {
        title = bookTitle;
        author = bookAuthor;
        year = bookYear;
        System.out.println("Objek dengan konstruktor nonstandar telah dibuat");
    }

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public String getAuthor() {
        return author;
    }

    public void setAuthor(String author) {
        this.author = author;
    }

    public int getYear() {
        return year;
    }

    public void setYear(int year) {
        this.year = year;
    }
}
import javax.swing.JOptionPane;

public class BookTest {

    public static void main(String[] args) {

        Student student = new Student();
        student.readBook();

        /*Book bukuku = new Book();

        //soal 2
        Book bukumu = new Book("UML Destilled", "Martin Flowler", 2004);
        System.out.println(bukumu.getTitle() + " " +
        bukumu.getAuthor() + " " + bukumu.getYear());

        //tambahan
        Book book2 = new Book("Java Programming", "Eko Z", 2008);
        //akhir tambahan

        //soal 3
        bukuku.setTitle("Programmer Java Guides");
        bukuku.setAuthor("Eko Zulkaryanto");
        bukuku.setYear(2009);
        //soal 4
        System.out.println("Judul Buku : " + bukuku.getTitle() + "\nPenulis : " +
        bukuku.getAuthor() + "\nTahun : " + bukuku.getYear());

        //tambahan
        String print = "Judul Buku : " + bukuku.getTitle() + "\nPenulis : " +
        bukuku.getAuthor() + "\nTahun : " + bukuku.getYear();
        String print2 = "Judul Buku : " + book2.getTitle() + "\nPenulis : " +
        book2.getAuthor() + "\nTahun : " + book2.getYear();
        System.out.println(print + print2);
        //akhir tambahan

        //tambahan
        JOptionPane.showMessageDialog(null, print + "\n\n" + print2,
        "Informasi Buku", JOptionPane.INFORMATION_MESSAGE);
        //akhir tambahan

        System.exit(0);

    }
}

kemudian dilanjutkan dengan ini :

public class Student {

    private String name;
    private String StudenID;
    private int age;
    private Book favoritBook;

    public Student() {
        //Jawaban #1 : Lengkapi Koding standar konstruktor ini
        name = "Eko Zulkaryanto";
        StudenID = "G64062250";
        age = 0;
        favoritBook = new Book();
    }

    public Student(String name, String StudenID, int age, String title, String author, int year) {
        //Jawaban #2 : Lengkapi Koding modified constructor ini
        this.name = name;
        this.StudenID = StudenID;
        this.age = age;
        this.favoritBook = new Book(title, author, year);

    }

    //Jawaban #3 : Method getter dan setter untuk atribut name, studentID, dan age

    public String getStudenID() {
        return StudenID;
    }

    public void setStudenID(String StudenID) {
        this.StudenID = StudenID;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public void readBook() {
    System.out.println("Buku yang sedang saya baca : ");
    System.out.println(favoritBook.getTitle() + ", " + favoritBook.getAuthor() + ", " + favoritBook.getYear());
    }

    public void borrowBook(String title, String author, int year) {
        favoritBook.setTitle(title);
        favoritBook.setAuthor(author);
        favoritBook.setYear(year);
    }
}
public class StudentTest {

    public static void main(String[] args) {
        Student teman = new Student();

        //Jawaban #4 : Buat atribut "saya" pd kelas Student, lalu isi atribut dengan modified constructor, isi dengan data Anda dan data buku favorit Anda
        Student saya = new Student("Eko Zulkaryanto", "G64062250", 20, "Head First Java", "PrenHall", 2008);

        //Jawaban #5 : Tampilkan atribut dari Student "saya"
        System.out.println(saya.getName() + ", " + saya.getStudenID() + ", " + saya.getAge());
        saya.readBook();
        System.out.println();

        //Jawaban #6 : Modifikasi teman, lalu tampilkan
        teman.setName("Fredi");
        teman.setStudenID("G6589000");
        teman.setAge(19);
        teman.borrowBook("The First Course in Probability", "Roni Wijaya", 2008);
        System.out.println(teman.getName() + ", " + teman.getStudenID() + ", " + teman.getAge());
        teman.readBook();
        System.out.println();

        //Jawaban #7 : Baca buku favoritnya
        saya.readBook();
        System.out.println();

        // Jawaban #8 : Pinjam Buku Favorit Lainnya, lalu baca buku itu
        saya.borrowBook("Fisika", "Bob Foster", 1999);
        saya.readBook();

    }
}
About

Seseorang yang terus berusaha menjadi seorang programmer dan blogger sejati di jagad Raya ini.

Tagged with: , ,
Posted in Java Programming
3 comments on “Praktikum Ke Dua PSBO
  1. a3u5z1i says:

    pertamax!!
    *membawa pesan sponsor :)*

  2. It’s unexpected for a number of of us when you find excellent important information available on the web. iphone 4 sim free

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Community

INDC

MUGI

WSS-ID

SQLServer-Indo

Indonesia Student Portal

Pengunjung
  • 120,657 orang
Yg lg OL di sini
site statistics
%d bloggers like this: