This Program needs to be Written in Java. Thank you so much! I need help constru
ID: 3798312 • Letter: T
Question
This Program needs to be Written in Java. Thank you so much!
I need help constructing the following classes. It would be a great help if you can describe what you did so I can understand it fully. Thank you so much!
The classes Media, Audio and Video would be abstract classes, as one would not instantiate objects of
those types. The class Artist is used to describe artists of any kind from producers, directors, band
members or movie actors. While is-a inheritance is shown through the specialization hierarchy of Media,
Audio, Video, Cd, and DVD; has-a relationship is shown through the class Artist with Media, Audio, and
Video all having Artists as data members. It might be necessary to create other methods for any of the
classes.
The toString methods should return a String containing all of the data members for the particular media.
The playMedia methods should display “Now playing:” followed by the media’s title and playtime if it is
an Audio and if it is a Video, followed by title, playing Time and rating. It should also increment the
number of plays.
Your application class should be able to
Create Cds and DVDs
List all of the media in your collection (Title, media type and Number of plays only)
List all of the data for a particular Media
List all of the data elements for the major artist of a particular media
Play a particular Media
List the number of plays for a particular media
You should create a collection of Artists and an Artist should appear in the collection only once
regardless of how many of his/her CD’s or DVD’s you may have.
Explanation / Answer
The code for the above question is given below. Comments are in code. Some general comments are as follow-
1. As mentioned in the question, we make the Media , Audio and Video classes abstract since we dont want to allow object creation of these classes.
2. There are getter/setter functions to access /modify the data members of these classes. These are apart from the functions showin in the figure
3. The toString() shows all the details of all data members of a class.
4.In the main function, we create Artist object and store in hashmap so that we avoid recreating same artist again and again , if he will appear in more than one CD / DVD. We need to get the artist object based on full name by calling Artist.getName() function. After creating artists, we can create CD, DVD using these artists (actors,producers, directors, supproting actors etc) and add it to list of all media. We randomly play media from this list and finally display the media details and check the number of plays for a media
5. playMedia() is an abstract method in Media and implemented in Audio, Video class as described in question
==========Please do rate the answer if it helped you. Thanks.
------------
Media.java
-----------
//An abstract class which encapsulates all GENERAL details of any media
public abstract class Media {
//data members. All protected members can be accessed from inherited class
protected String title; // the title of the media
protected Artist majorArtist;
protected int hours,minutes,seconds; //playing time of media
protected int numPlays;
public Media(String title,Artist a)
{
this.title=title;
this.majorArtist=a;
}
public void setPlayTime(int h,int m,int s)
{
hours=h;
minutes=m;
seconds=s;
}
public String getTitle() {
return title;
}
public Artist getMajorArtist() {
return majorArtist;
}
//returns a string in the format hours:minutes:seconds of media playing time
public String getPlayingTime() {
return hours+":"+minutes+":"+seconds;
}
public int getNumPlays() {
return numPlays;
}
abstract void playMedia();//abstract function to be implemented by inheriting classes
public String toString()
{
return "Title:"+title+"|Artist:"+majorArtist.toString()+"|Playing Time:"+getPlayingTime()+"|Number of Plays:"+numPlays;
}
}
-------
Artist.java
------------
import java.text.SimpleDateFormat;
import java.util.Date;
//A class representing any kind of artists like actors, producers , band etc
public class Artist {
String first,last;
Date birthDate;
String twitterHandle,url;
private static SimpleDateFormat dateFormatter=new SimpleDateFormat("dd MMM yyyy");
public Artist(String fname,String lname)
{
first=fname;
last=lname;
}
public String getUrl() {
return url;
}
public void setUrl(String url) {
this.url = url;
}
public String getFirstName() {
return first;
}
public String getLastName() {
return last;
}
public String getTwitterHandle() {
return twitterHandle;
}
public Date getBirthDate() {
return birthDate;
}
public void setBirthDate(Date birthDate) {
this.birthDate = birthDate;
}
public void setTwitterHandle(String handle)
{
twitterHandle=handle;
}
public void setURL(String u)
{
url=u;
}
//return the full name of the artist <firstname> space <lastname>
public String getName()
{
return first+(last==null?"":(" "+last));
}
//returns all the details of the artist
public String toString()
{
return "Name : "+first + " "+last+"|DOB : "+(birthDate==null?"":dateFormatter.format(birthDate))
+"|Twitter : "+(twitterHandle==null?"":twitterHandle)+"|URL : "+(url==null?"":url);
}
}
-------------
Audio.java
-------------
public abstract class Audio extends Media{
protected Artist groupMembers[];
protected int numMembers;
protected Artist producer;
public Audio(String title,Artist major,Artist producer)
{
super(title,major);
this.producer=producer;
}
public Artist getProducer()
{
return producer;
}
public int getGroupSize()
{
return numMembers;
}
public void setGroupMembers(Artist group[])
{
groupMembers=group;
numMembers=group.length;
}
public Artist getGroupMember(int i)
{
return groupMembers[i];
}
public void playMedia()
{
System.out.println("Now playing "+title+" |Playtime:"+getPlayingTime());
//increment the number of plays
numPlays++;
}
//displays all the data members of the Audio object
public String toString()
{
StringBuffer buf=new StringBuffer();
//get the string representation of all group members and producer and append it to
//media's tostring
if(producer!=null)
buf.append(" Producer:"+producer.getName());
if(numMembers>0)
{
buf.append(" Group:");
for(int i=0;i<numMembers;i++)
buf.append(groupMembers[i].getName()+",");
}
return super.toString()+buf.toString();
}
}
--------
CD.java
--------
public class CD extends Audio{
protected int numTracks;
protected String tracks[];
//Constructor with the reuqired minimum details for the CD
public CD(String title,Artist artist,Artist producer)
{
super(title,artist,producer);
}
public void setTracks(String t[])
{
tracks=t;
numTracks=t.length;
}
public void playMedia()
{
super.playMedia();
for(int i=0;i<numTracks;i++)
{
System.out.println("Playing Track : "+tracks[i]);
}
}
//displays all the data members of CD object
public String toString()
{
StringBuffer buf=new StringBuffer();
if(numTracks>0)
{
buf.append(" Tracks:");
for(int i=0;i<numTracks;i++)
buf.append(" "+(i+1)+"."+tracks[i]);
}
return super.toString()+buf.toString();
}
}
---------------
Video.java
-----------
public abstract class Video extends Media{
protected Artist supportingActors[];
protected int numActors;
protected Artist director;
protected int rating;
public Video(String title,Artist major,Artist director)
{
super(title,major);
this.director=director;
}
public int getRating() {
return rating;
}
public void setRating(int rating) {
this.rating = rating;
}
public Artist getDirector()
{
return director;
}
public int supportingActorsCount()
{
return numActors;
}
public void setSupportingActors(Artist actors[])
{
supportingActors=actors;
numActors=actors.length;
}
public Artist getSupportingActor(int i)
{
return supportingActors[i];
}
public void playMedia()
{
System.out.println("Now playing "+title+" |Playtime:"+getPlayingTime()+"|Rating : "+rating);
//increment the number of plays
numPlays++;
}
//displays all the data members of the video object
public String toString()
{
StringBuffer buf=new StringBuffer();
//get the string representation of rating all suppporting actors and director and append it to
//media's tostring
buf.append("| Rating : "+rating);
if(director!=null)
buf.append(" Director :"+director.getName());
if(numActors>0)
{
buf.append(" Supporting Actors:");
for(int i=0;i<numActors;i++)
buf.append(supportingActors[i].getName()+",");
}
return super.toString()+buf.toString();
}
}
-------------
DVD.java
-----------
public class DVD extends Video
{
String specialFeatures[];
int numFeatures;
String TVFormat;
String wideScreenFrmat;
String soundOptions[];
int numSoundOptions;
public DVD(String title, Artist major, Artist director) {
super(title, major, director);
TVFormat="MPEG-2";//default tv format
}
public String[] getSpecialFeatures() {
return specialFeatures;
}
public void setSpecialFeatures(String[] specialFeatures) {
this.specialFeatures = specialFeatures;
numFeatures=specialFeatures.length;
}
public int getNumFeatures() {
return numFeatures;
}
public String getTVFormat() {
return TVFormat;
}
public void setTVFormat(String tVFormat) {
TVFormat = tVFormat;
}
public String getWideScreenFrmat() {
return wideScreenFrmat;
}
public void setWideScreenFrmat(String wideScreenFrmat) {
this.wideScreenFrmat = wideScreenFrmat;
}
public String[] getSoundOptions() {
return soundOptions;
}
public void setSoundOptions(String[] soundOptions) {
this.soundOptions = soundOptions;
this.numSoundOptions=soundOptions.length;
}
public int getNumSoundOptions() {
return numSoundOptions;
}
public String toString()
{
StringBuffer buf=new StringBuffer();
if(TVFormat!=null)
buf.append(" TV Format:"+TVFormat);
if(wideScreenFrmat!=null)
buf.append(" Widescreen Format:"+wideScreenFrmat);
if(numFeatures>0)
{
buf.append(" Features:");
for(int i=0;i<numFeatures;i++)
buf.append(specialFeatures[i]+",");
}
if(numSoundOptions>0)
{
buf.append(" Sound options:");
for(int i=0;i<numSoundOptions;i++)
buf.append(soundOptions[i]);
}
return super.toString()+buf.toString();
}
}
------------
MediaTest.java
---------
import java.util.ArrayList;
import java.util.Calendar;
import java.util.GregorianCalendar;
import java.util.HashMap;
import java.util.Random;
public class MediaTest {
public static void main(String[] args) {
HashMap<String, Artist> artistList=new HashMap<String,Artist>();
Calendar calendar=Calendar.getInstance();
//maintain all artists in a hashmap key is their name , value is the artist object.
//we want to reuse the artist object if he appears in more than one cd/dvd
Artist artist=new Artist("Michael","Jackson");
calendar.set(1958, 7, 29); //January is 0, so 7 means august
artist.setBirthDate(calendar.getTime());
artist.setTwitterHandle("https://twitter.com/michaeljackson");
artist.setUrl("www.michaeljackson.com/");
artistList.put(artist.getName(), artist);
artist=new Artist("Whitney","Houston");
calendar.set(1963, 7, 9);
artist.setBirthDate(calendar.getTime());
artist.setTwitterHandle("https://twitter.com/officialwhitney?lang=en");
artist.setUrl("www.whitneyhouston.com/");
artistList.put(artist.getName(), artist);
artist=new Artist("Clive","Davis");
calendar.set(1932, 3, 4);
artist.setBirthDate(calendar.getTime());
artist.setTwitterHandle("https://twitter.com/clivedavis?lang=en");
artist.setUrl("https://www.facebook.com/MrCliveDavis/");
artistList.put(artist.getName(), artist);
artist=new Artist("Quincy","Jones");
calendar.set(1933, 2, 14);
artist.setBirthDate(calendar.getTime());
artist.setTwitterHandle("https://twitter.com/QuincyDJones");
artist.setUrl("www.quincyjones.com/");
artistList.put(artist.getName(), artist);
artist=new Artist("Chris","Evans"); //actor
calendar.set(1981, 5, 13);
artist.setBirthDate(calendar.getTime());
artistList.put(artist.getName(),artist);
artist=new Artist("Scarlett","Johansson"); //supporting actor
calendar.set(1984, 10, 22);
artist.setBirthDate(calendar.getTime());
artistList.put(artist.getName(),artist);
artist=new Artist("Sebastian","Stan"); //supportingg actor
calendar.set(1982, 7, 13);
artist.setBirthDate(calendar.getTime());
artistList.put(artist.getName(),artist);
artist=new Artist("Anthony","Russo"); //director
calendar.set(1981, 0, 1);
artist.setBirthDate(calendar.getTime());
artistList.put(artist.getName(), artist);
ArrayList<Media> media=new ArrayList<Media>();
//Putting just a few tracks...
CD cd=new CD("Thriller",artistList.get("Michael Jackson"),artistList.get("Quincy Jones"));
cd.setPlayTime(0, 42, 18);
cd.setTracks(new String[]{"Beat it","Thriller","Baby be mine"});
media.add(cd);
cd=new CD("The Bodyguard",artistList.get("Whitney Houston"),artistList.get("Clive Davis"));
cd.setPlayTime(0, 50,0 );
cd.setTracks(new String[]{"I'm every woman","Someday"});
media.add(cd);
DVD dvd=new DVD("Captian America : Civil War",artistList.get("Chris Evans"),artistList.get("Anthony Russo"));
dvd.setRating(5);
dvd.setPlayTime(2, 27, 0);
Artist support[]=new Artist[2];
support[0]=artistList.get("Scarlett Johansson");
support[1]=artistList.get("Sebastian Stan");
dvd.setSupportingActors(support);
//please set other dvd options like widescreenformat, sound optiosn etc...using setter methods
media.add(dvd);
//display all artists
System.out.println("List of all artists");
System.out.println("-----------------------------------");
for(String a :artistList.keySet())
{
System.out.println(artistList.get(a));
}
//display title type and playtime of media
System.out.println(" Type Title Playtime");
System.out.println("-----------------------------------");
Media m;
for(int i=0;i<media.size();i++)
{
m=media.get(i);
if(m instanceof CD)
System.out.print(" CD");
else
System.out.print(" DVD");
System.out.print(" "+m.getTitle()+" "+m.getPlayingTime());
}
System.out.println(" Randomly playing the media available");
//play 5 times choosing from the media collection randomly
Random random=new Random();
for(int i=0;i<5;i++)
{
System.out.println("-----------------------------------");
media.get(random.nextInt(media.size())).playMedia(); //play a randomly selected media
}
//display all details of cd and dvd ,also check the number of plays
System.out.println(" List of all CD / DVDs");
for(int i=0;i<media.size();i++)
{
System.out.println("-----------------------------------");
System.out.println(media.get(i));
}
}
}
-------------------
Sample output
-------------
List of all artists
-----------------------------------
Name : Sebastian Stan|DOB : 13 Aug 1982|Twitter : |URL :
Name : Anthony Russo|DOB : 01 Jan 1981|Twitter : |URL :
Name : Michael Jackson|DOB : 29 Aug 1958|Twitter : https://twitter.com/michaeljackson|URL : www.michaeljackson.com/
Name : Whitney Houston|DOB : 09 Aug 1963|Twitter : https://twitter.com/officialwhitney?lang=en|URL : www.whitneyhouston.com/
Name : Quincy Jones|DOB : 14 Mar 1933|Twitter : https://twitter.com/QuincyDJones|URL : www.quincyjones.com/
Name : Chris Evans|DOB : 13 Jun 1981|Twitter : |URL :
Name : Scarlett Johansson|DOB : 22 Nov 1984|Twitter : |URL :
Name : Clive Davis|DOB : 04 Apr 1932|Twitter : https://twitter.com/clivedavis?lang=en|URL : https://www.facebook.com/MrCliveDavis/
Type Title Playtime
-----------------------------------
CD Thriller 0:42:18
CD The Bodyguard 0:50:0
DVD Captian America : Civil War 2:27:0
Randomly playing the media available
-----------------------------------
Now playing Captian America : Civil War |Playtime:2:27:0|Rating : 5
-----------------------------------
Now playing Thriller |Playtime:0:42:18
Playing Track : Beat it
Playing Track : Thriller
Playing Track : Baby be mine
-----------------------------------
Now playing Thriller |Playtime:0:42:18
Playing Track : Beat it
Playing Track : Thriller
Playing Track : Baby be mine
-----------------------------------
Now playing Captian America : Civil War |Playtime:2:27:0|Rating : 5
-----------------------------------
Now playing The Bodyguard |Playtime:0:50:0
Playing Track : I'm every woman
Playing Track : Someday
List of all CD / DVDs
-----------------------------------
Title:Thriller|Artist:Name : Michael Jackson|DOB : 29 Aug 1958|Twitter : https://twitter.com/michaeljackson|URL : www.michaeljackson.com/|Playing Time:0:42:18|Number of Plays:2
Producer:Quincy Jones
Tracks:
1.Beat it
2.Thriller
3.Baby be mine
-----------------------------------
Title:The Bodyguard|Artist:Name : Whitney Houston|DOB : 09 Aug 1963|Twitter : https://twitter.com/officialwhitney?lang=en|URL : www.whitneyhouston.com/|Playing Time:0:50:0|Number of Plays:1
Producer:Clive Davis
Tracks:
1.I'm every woman
2.Someday
-----------------------------------
Title:Captian America : Civil War|Artist:Name : Chris Evans|DOB : 13 Jun 1981|Twitter : |URL : |Playing Time:2:27:0|Number of Plays:2| Rating : 5
Director :Anthony Russo
Supporting Actors:Scarlett Johansson,Sebastian Stan,
TV Format:MPEG-2
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.