Dalam latihan ini, study casenya adalah membuat class untuk menyimpan CD dan DVD dalam Class yang berbeda. Karena adanya properti yang sama dan berbeda di masing-masing item, maka dibuat suatu class "orangtua" (Item).
Kode :
1. Item.java
public class item
{
private String title;
private int playingTime;
private boolean gotlt;
private String comment;
public item(String theTitle, int time)
{
title = theTitle;
playingTime = time;
gotlt = false;
comment = "";
}
public void setComment(String comment)
{
this.comment=comment;
}
public String getComment()
{
return comment;
}
public void setOwn(boolean ownlt)
{
gotlt=ownlt;
}
public boolean getOwn()
{
return gotlt;
}
public void print()
{
System.out.print("title:"+title+"(" + playingTime + "mins)");
if(gotlt)
{
System.out.println("*");
}
else
{
System.out.println(" " + comment);
}
}
}
2. CD.java
/**
* kelas fungsi cd yang tidak ada di item
*/
public class cd extends item
{
private String artist;
private int numberOfTracks;
public cd(String theTitle, String theArtist, int tracks, int time)
{
super(theTitle,time);
artist = theArtist;
numberOfTracks = tracks;
}
public String getArtist()
{
return artist;
}
public int getNumberOfTracks()
{
return numberOfTracks;
}
}
3. DVD.java
/**
* kelas fungsi dvd yang tidak ada di item
* @version 1.0.1
*/
public class dvd extends item
{
private String director;
public dvd(String theTitle, String theDirector, int time)
{
super(theTitle,time);
director=theDirector;
}
public String getDirector()
{
return director;
}
}
4. Database.java
/**
* @author abdi teh Alberto
*/
import java.util.ArrayList;
public class Database
{
// instance variables - replace the example below with your own
private ArrayList<item>items ;
public Database()
{
items=new ArrayList<item>();
}
public void additem(item theItem)
{
items.add(theItem);
}
public void list()
{
for(item item:items)
{
item.print();
System.out.println();
}
}
}
Kode :
1. Item.java
public class item
{
private String title;
private int playingTime;
private boolean gotlt;
private String comment;
public item(String theTitle, int time)
{
title = theTitle;
playingTime = time;
gotlt = false;
comment = "";
}
public void setComment(String comment)
{
this.comment=comment;
}
public String getComment()
{
return comment;
}
public void setOwn(boolean ownlt)
{
gotlt=ownlt;
}
public boolean getOwn()
{
return gotlt;
}
public void print()
{
System.out.print("title:"+title+"(" + playingTime + "mins)");
if(gotlt)
{
System.out.println("*");
}
else
{
System.out.println(" " + comment);
}
}
}
2. CD.java
/**
* kelas fungsi cd yang tidak ada di item
*/
public class cd extends item
{
private String artist;
private int numberOfTracks;
public cd(String theTitle, String theArtist, int tracks, int time)
{
super(theTitle,time);
artist = theArtist;
numberOfTracks = tracks;
}
public String getArtist()
{
return artist;
}
public int getNumberOfTracks()
{
return numberOfTracks;
}
}
3. DVD.java
/**
* kelas fungsi dvd yang tidak ada di item
* @version 1.0.1
*/
public class dvd extends item
{
private String director;
public dvd(String theTitle, String theDirector, int time)
{
super(theTitle,time);
director=theDirector;
}
public String getDirector()
{
return director;
}
}
4. Database.java
/**
* @author abdi teh Alberto
*/
import java.util.ArrayList;
public class Database
{
// instance variables - replace the example below with your own
private ArrayList<item>items ;
public Database()
{
items=new ArrayList<item>();
}
public void additem(item theItem)
{
items.add(theItem);
}
public void list()
{
for(item item:items)
{
item.print();
System.out.println();
}
}
}
Tidak ada komentar:
Posting Komentar