TUGAS 4 PBO :: INHERITANCE

Berikut merupakan contoh pemrograman inheritance dengan program kasus rental CD dan DVD.

Screen Shot 2019-10-09 at 23.13.19

/**
* Write a description of class Item here.
*
* @author Chintya PD
*/
public class Item
{
private String title; private int playingTime; private boolean gotIt; private String comment;
/**
* Initialise the fields of the item.
* @param theTitle The title of this item.
* @param time The running time of this item. */
public Item(String theTitle, int time) {
title = theTitle; playingTime = time; gotIt = false; comment=””;
}

/**
* Enter a comment for this item.
* @param comment The comment to be entered. */
public void setComment(String comment) {
this.comment = comment; }
/**
* @return The comment for this item. */
public String getComment() {
return comment;

}

/**
* Write a description of class CD here.
*
* @author Chintya PD
*/
public class CD extends Item {
private String artist;
private int numberOfTracks;
/**
* Initialize the CD.
* @param theTitle The title of the CD.
* @param theArtist The artist of the CD.
* @param tracks The number of tracks on the CD. * @param time The playing time of the CD.
*/
public CD(String theTitle, String theArtist, int tracks, int time) {
super(theTitle, time); artist = theArtist; numberOfTracks = tracks;
}
/**
* @return The artist for this CD. */

public String getArtist() {
return artist; }
/**
* @return The number of tracks on this CD. */
public int getNumberOfTracks() {
return numberOfTracks; }
}

/**
* Write a description of class CD here.
*
* @author Chintya PD
*/
public class DVD extends Item
{
private String director;
private int price;

public DVD (String title, String director, int time, int price){
super(title, time);
this.director = director;
this.price = price;
}

public String getDirector(){
return director;
}

@Override
public void setOwn(boolean gotIt){
super.setOwn(gotIt);
}

@Override
public void setComment(String comment){
super.setComment(comment);
}

public int getPrice(){
return price;
}
}

/**
* Write a description of class DATABASE here.
*
* @author Chintya PD
*/

import java.util.ArrayList;
public class Database {
private ArrayList items;
/**
* Construct an empty Database. */
public Database() {
items = new ArrayList();
}
/**
* Add an item to the database.
* @param theItem The item to be added. */

public void addItem(Item theItem) {
items.add(theItem); }
/**
* Print a list of all currently stored items to the * text terminal.
*/
public void list() {
for(Item item : items) {
item.print();
System.out.println();
}
}
}

 

Tinggalkan komentar