I need help with a OOP exercise for school

I’m really lost trying to solve this exercise for school, I’m supposed to create a set of classes to represent and manipulate a media playlist (no actual media used, just text). There should be child classes like music, podcasts, and movies. All should be able to report their name and duration.

I need to create a “next()” method and checked with a “playing()” method. Also, I need another method to sort the playlist by the length of the media.

So far I only have the skeleton but I’m not sure how to go about the methods… Any help?

class Playlist {
  constructor (name, duration) {
    this._name = name;
    this._duration = duration;
    this._playlist = [];
    // add an index that indicates the position of the media that is playing
  }

  playing(str){ //displays information about the media being played at this time
    return console.log("right now " + str._name + " is playing");

  }

  next(){ //select and play the next media from the playlist. Check if it´s not the last media from the playlist!
    return console.log("right now " + " is playing");
  }

  newMedia(media_object){ //add NEW  media to the playlist -->  the parameter is an instance of a child class from media (see below)
    this._playlist.push(media_object);

    return this._playlist;
    console.log(this._playlist)
  }

  sortByLength(){ // sorting 
    this._duration.sort(function(a,b){return a - b})
  }

}

class Music extends Playlist {
  constructor (name, duration, artistName) {
    super(name, duration) ;
    this._artistName = artistName
  }

  get artistName(){
    return this._artistName;
  }
}

class Movies extends Playlist {
  constructor (name, duration, rating) {
    super(name, duration) ;
    this._rating = rating
  }

  get rating(){
    return this._rating;
  }
}

class Podcast extends Playlist {
  constructor (name, duration, genre) {
    super(name, duration) ;
    this._genre = genre
  }

  get genre() {
    return this._genre;
  }
}


let theBeatles = new Music("Yellow Submarine", 300, "The Beatles")

let theShining = new Movies("The Shining", 1000, 4.3)

let randomOrder = new Podcast("Random Order", 600, "Comedy")

theBeatles.newMedia(theBeatles);


// TESTING
// playing()  --> console.log  -- Currently playing: some_media (at position)
// This is playlist is empty.

// next() --> increment the index
// check if the index at the end of the playlist array// This media MMMMMM is at the end of the playlist

Thank you for your help everyone!

You have a class defined for a Playlist, but then every other class you define is extending Playlist. Is a Music, Movie, or Podcast object really a type of Playlist? These three media types do seem to have a lot of things in common, so I’m not saying they can’t all be a subclass of the same base class, I’m just not sure that Playlist makes sense. What do you think?

Yeah it can be misleading to use the term playlist but this is the skeleton that was instructed to create! I just need help with the methods to be able to connect the different media like a playlist

The instructions explicitly said that the media objects MUST be a subclass of the playlist? That doesn’t really make sense to me and I’m not sure how that would teach good OOP design principles. Besides, the way you have it set up now, each media object will have it’s own copy of a playlist, and I’m really thinking this is not how it is supposed to work.

Before I continue on with this discussion, I just want to verify one more time. Do the instructions require that the media objects be a subclass of Playlist? I know you said that “There should be child classes like music, podcasts, and movies” but this doesn’t say that they have to be children of the Playlist, just that they should be child classes of a common base class.

1 Like

Right, what you are saying makes sense and to be honest maybe I didn’t understand that part in the instructions fully. If it’s of any help for you to get a better idea of the exercise maybe you can take a look at the guide given by our instructor?

class playlist {

  constructor()
  {
    this._playlist = [];
    // add an index that indicates the position of the media that is playing
  }


  playing()  //displays information about the media being played at this time
  {
    // your code
  }

  next()  //select and play the next media from the playlist. Check if it´s not the last media from the playlist!
  {
    // your code
  }

  new_media(media_object) //add NEW  media to the playlist -->  the parameter is
  // an instance of a child class from media (see below)
  {
    // your code
  }

  sort_by_property() //sorts the playlist according to some object property (check whats happens to index!)
  {
      // your code
  }

  // other possible methods: previous() , get index , get playlist ,

}



//PARENT class
class media {
// constructor (common poperties)

//methods

//getters


}

//child classes
class Music {
  // constructor  (common poperties, particularProperty1 , particularProperty2, etc)
  // super(common poperties - inherited)
  //this._particularProperty1 = particularProperty1
  //this._particularProperty2 = particularProperty2
  //this._etc

  //own methods

  //own getters

}

class Movie{
  // constructor  (common poperties, particularProperty1 , particularProperty2, etc)
  // super(common poperties  - inherited)
  //this._particularProperty1 = particularProperty1
  //this._particularProperty2 = particularProperty2
  //this._etc

  //own methods

  //own getters

}

class Podcast{
  // constructor  (common poperties, particularProperty1 , particularProperty2, etc)
  // super(common poperties  - inherited)
  //this._particularProperty1 = particularProperty1
  //this._particularProperty2 = particularProperty2
  //this._etc

  //own methods

  //own getters

}




// TESTING
// playing()  --> console.log  -- Currently playing: some_media (at position)
// This is playlist is empty.

// next() --> increment the index
// check if the index at the end of the playlist array// This media MMMMMM is at the end of the playlist

What do you think?

I’d take it as media being the parent of music, movies, etc and a playlist containing media…

Oh ok. That makes sense! Thanks! I’ve rearranged my version to make it similar to what you’re saying but I’m still pretty lost when it comes to the next() and playing() methods, any ideas?

class Playlist {
  constructor()
  {
    this._playlist = [];
    // add an index that indicates the position of the media that is playing
  }


  playing()  //displays information about the media being played at this time
  {
    // your code
  }

  next()  //select and play the next media from the playlist. Check if it´s not the last media from the playlist!
  {
    // your code
  }

  newMedia(mediaObject) //add NEW  media to the playlist -->  the parameter is
  // an instance of a child class from media (see below)
  {
    // your code
  }

  sortByProperty() //sorts the playlist according to some object property (check whats happens to index!)
  {
      // your code
  }

}



class Media {
constructor (name, duration) {
  this._name = name;
  this._duration = duration;
}

//methods

get name(){
  return this._name;
}

get duration(){
  return this._duration;
}

get playlist(){
  return this._playlist
}

}

class Music extends Media {
  constructor (name, duration, artistName) {
    super(name, duration) ;
    this._artistName = artistName
  }

  get artistName(){
    return this._artistName;
  }
}

class Movies extends Media {
  constructor (name, duration, rating) {
    super(name, duration) ;
    this._rating = rating
  }

  get rating(){
    return this._rating;
  }
}

class Podcast extends Media {
  constructor (name, duration, genre) {
    super(name, duration) ;
    this._genre = genre
  }

  get genre() {
    return this._genre;
  }
}

let theBeatles = new Music("Yellow Submarine", 300, "The Beatles")

let theShining = new Movies("The Shining", 1000, 4.3)

let randomOrder = new Podcast("Random Order", 600, "Comedy")


// TESTING
// playing()  --> console.log  -- Currently playing: some_media (at position)
// This is playlist is empty.

// next() --> increment the index
// check if the index at the end of the playlist array// This media MMMMMM is at the end of the playlist```

Thanks!

This is looking a bit better now :slight_smile:

Look at the comments that your instructor gave you. For the Playlist constructor:

// add an index that indicates the position of the media that is playing

What data structure is the playlist being stored in. What type of value would you use to store an index in the playlist that represents the media object that is currently playing?

I thought of adding something like “songIndex = 0” but I couldn’t make the connection on how to relate that with the media I add. Any ideas?

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.