Latihan Inheritance

1. Item.java 
  1. public class Item {
  2.     private String title;
  3.     private int playingtime;
  4.     private boolean gotit;
  5.     private String comment;
  6.     public Item(String title, int playingtime) //constructor
  7.     {
  8.         this.title=title;
  9.         this.playingtime=playingtime;
  10.         this.gotit=false;
  11.         this.comment = "";
  12.     }
  13.     public void setComment(String comment)//method untuk memasukkan comment item
  14.     {
  15.         this.comment = comment;
  16.     }
  17.     public String getComment() //method untuk return comment item
  18.     {
  19.         return this.comment;
  20.     }
  21.     public void setGotIt(boolean gotit) //method untuk memasukkan ketersediaan item
  22.     {
  23.         this.gotit = gotit;
  24.     }
  25.     public boolean getGotIt() //method untuk return ketersediaan item
  26.     {
  27.         return this.gotit;
  28.     }
  29.     public void Print() //method untuk mencetak item
  30.     {
  31.         System.out.println("Title: " + title + "( " + playingtime + " mins )" );
  32.         //cek item tersedia atau tidak
  33.         if(gotit)
  34.         {
  35.             System.out.println("Available");
  36.         }
  37.         else
  38.         {
  39.             System.out.println("Not Available");
  40.         }
  41.         System.out.println(comment);
  42.     }
  43. }

2. CD.java
  1. public class CD extends Item
  2. {
  3.     private String artist;
  4.     private int numberoftracks;
  5.     public CD(String title, int playingtime, String artist, int numberoftracks) //constructor
  6.     {
  7.         super(title,playingtime);
  8.         this.artist=artist;
  9.     }
  10.      
  11.     public void setComment(String comment) //method untuk memasukkan comment CD
  12.     {
  13.         super.setComment(comment);
  14.     }
  15.     public void setGotIt(boolean gotit) //method untuk memasukkan nilai ketersediaan CD
  16.     {
  17.         super.setGotIt(gotit);
  18.     }
  19.     public String getArtist() //method untuk return nama artist
  20.     {
  21.         return artist;
  22.     }
  23.     public int getNumber() //method untuk return jumlah tracks
  24.     {
  25.         return numberoftracks;
  26.     }
  27.     public void printArtist() //method untuk mencetak nama artist
  28.     {
  29.         System.out.println("Artist: " + artist);
  30.     }
  31. }
3. DVD.java

  1. public class DVD extends Item
  2. {
  3.     private String director;
  4.     public DVD(String title, int playingtime, String director) //constructor
  5.     {
  6.         super(title,playingtime);
  7.         this.director=director;
  8.     }
  9.     public void setComment(String comment) //method untuk memasukkan comment DVD
  10.     {
  11.         super.setComment(comment);
  12.     }
  13.     public void setGotIt(boolean gotit) //method untuk memasukkan nilai ketersediaan DVD
  14.     {
  15.         super.setGotIt(gotit);
  16.     }
  17.     public String getDirector() //method untuk return nama director
  18.     {
  19.         return director;
  20.     }
  21.     public void print() //method untuk mencetak nama director
  22.     {
  23.         System.out.println("Director: " + director);
  24.     }
  25. }
4. Database.java



Komentar

Postingan populer dari blog ini

PBKK - Pengenalan Framework CodeIgniter

Framework Desktop Electron

ETS PBO - I-KIOS INDOMARET