Design, implement, and test a class that represents a song on a CD or in an MP3
ID: 3559307 • Letter: D
Question
Design, implement, and test a class that represents a song on a CD or in an MP3 library. It should have members for title, album, artist, playing time in minutes and seconds, and music category. The music category is represented by an enumeration type called Category; make up an enumeration of your favorite categories. The class should have a contructor that allows all of the data members to be set, and a default constructor that sets all of the data members to appropriate empty values. It should have an observer operation for each data member, and an observer that returns all of the data for the song as a string. An observer that compares two songs for equality should be developed as well.
Explanation / Answer
public abstract class Media{
private String title; //global title variable
private int year; //global year variable
private int quantity; //global quantity variable
public int MediaItem(String t, int yr, int q){
if(t == null)
throw new IllegalArgumentException("No title given");
else if(yr < 0 || yr > 2010)
throw new IllegalArgumentException("Bad year");
else if(q < 0)
throw new IllegalArgumentException("Bad quantity");
else{
title = t;
year = yr;
quantity = q;
}
} //constructor
public String toString(){
return("Title: " + title
+ " Year: " + year
+ " Quantity: " + quantity);
} //toString()
} //class
public class CD extends Media{
public CD(String t, int yr, int q){
super(t, yr, q);
} //constructor
} //class
public class DVD extends Media{
public DVD(String t, int yr, int q){
super(t, yr, q);
} //constructor
} //class
public class VHS extends Media{
public VHS(String t, int yr, int q){
super(t, yr, q);
} //constructor
} //class
public class Store{
public static void main(String args[]){
Media items[] = new Media[5];
for(int i = 0; i < 5; i++){
try{
if(i % 3 == 0)
items[i] = new CD("Im a CD", 1993, i*3);
else if(i % 3 == 1)
items[i] = new DVD("Im a DVD", 1999, i*2);
else
items[i] = new VHS("Im old!", 1987, i*4);
}catch(IllegalArgumentException iae){}
}
System.out.println("Inventory: ");
for(int i = 0; i < 5; i++)
System.out.println("Item: " + items[i] + " ");
} //main
} //class
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.