Question

This is my playlist class: class Playlist{       private String name;    private int numberOfRecordings...

This is my playlist class:

class Playlist{
  
   private String name;
   private int numberOfRecordings = 0;
   private int durationInSeconds = 0;
   private int MAX_PLAYLIST_SIZE;
   Recording recordingslist[];
  
   Playlist(){
       name = "Unknown";
       MAX_PLAYLIST_SIZE = 5;
       recordingslist = new Recording[MAX_PLAYLIST_SIZE];
       durationInSeconds = 0;
   }
  
   Playlist(String name, int size){
       this.name = name;
       this.MAX_PLAYLIST_SIZE = size;
       recordingslist = new Recording[MAX_PLAYLIST_SIZE];
       durationInSeconds = 0;
      
   }
  
   public String getName(){
       return this.name;
   }
  
   public int getNumberOfRecordings(){
       return this.numberOfRecordings;
   }
  
  
   public int getMAX_PLAYLIST_SIZE(){
       return this.MAX_PLAYLIST_SIZE;
   }
  
   public Recording[] getRecordings(){
       return recordingslist;
   }
  
   public void setName(String name){
       if(name!=null)
           this.name = name;
   }
  
   public boolean add(Recording r){
       if(r==null)
           return false;
      
       else if(numberOfRecordings>=MAX_PLAYLIST_SIZE)
           return false;
           recordingslist[numberOfRecordings]=r;
           numberOfRecordings++;
           durationInSeconds+=r.getDuration();
           return true;
   }
  
   public void play(){
       for(int i=0; i            recordingslist[i].play();
       }
   }
  
   @Override
   public String toString(){
           int s = durationInSeconds % 60;
           int h = durationInSeconds / 60;
           int m = h % 60;
           String str="";
           str+="\nPlaylist : "+name+" ["+m+"m"+s+"s]\n";
               for(int i=0;i                    str+=recordingslist[i]+"\n";
               }
               return str;
   }
          
}

Here is the Recording Class:

public class Recording{

   public String ARTIST;
   public String NAME;
   public int DURATION_IN_SECONDS;
     
   Recording(){
       ARTIST = "Unknown";
       NAME = "Unknown";
       DURATION_IN_SECONDS = 0;
   }
     
   Recording(String ARTIST, String NAME, int DURATION_IN_SECONDS){
       if (ARTIST != null && NAME != null && DURATION_IN_SECONDS >0){
           this.ARTIST = ARTIST;
           this.NAME = NAME;
           this.DURATION_IN_SECONDS = DURATION_IN_SECONDS;
       } else {
           this.ARTIST = "Unknown";
           this.NAME = "Unknown";
           this.DURATION_IN_SECONDS = 0;
       }
   }

  
   public String getArtist(){
       return this.ARTIST;
   }
  
   public String getName(){
       return this.NAME;
   }
  
   public int getDuration(){
       return this.DURATION_IN_SECONDS;
   }
  
   public void play(){
       if (DURATION_IN_SECONDS > 0){
           int s = DURATION_IN_SECONDS % 60;
           int h = DURATION_IN_SECONDS / 60;
           int m = h % 60;
          
       System.out.println("Now Playing:"+ARTIST+"-" +NAME+ "[" +m+"m" +s+"s]");
      
       }
       else {
           System.out.println("ERROR: cannot play this recording");
       }
   }
  
   @Override
   public String toString(){
           int s = DURATION_IN_SECONDS % 60;
           int h = DURATION_IN_SECONDS / 60;
           int m = h % 60;
       return ARTIST +"-"+NAME +"[" +m+"m" +s+"s]";
}
  

}

Please add the following to the playlist class:

  • add a void shuffle method such that:

    • if the playlist is not empty, will RANDOMLY play numberOfRecordingsToPlay (an argument for shuffle method),

    • it is fine if if the same recording is played two or more times in a row,

  • Add a void load method which:

    • will open a data file (fileName passed as an argument to this method),

      • Use try-catch to handle FileNotFoundException,

        • if caught display ERROR: File not found! Message, where fileName is a variable

      • File class does not have a close() method - don’t worry about closing it

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Currently you are calling play() without any parameter and you don't have play() in your Recording so you cannot call recordinglist[i].play().

I have modified the play() and added the shuffle() method to the code.

Shuffle method will play the song number received in the argument.

Here are two methods:

public void play(Recording r){
for(int i=0; i<recordingslist.length;i++) {
   play(recordingslist[i]);
}
}

public void shuffle(int numberOfRecToPlay) {
   play(recordingslist[numberOfRecToPlay]);
}

To play random song on shuffle, you can pass random number while calling shuffle. You can do that using following code:

Random number = new Random();

song = number.nextInt(50)+1; // This will genarate random number from 1-51

suffle(song); // shuffle will play the song number passed as argument

Add a comment
Know the answer?
Add Answer to:
This is my playlist class: class Playlist{       private String name;    private int numberOfRecordings...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Use inheritance to create a new class AudioRecording based on Recording class that: it will retain...

    Use inheritance to create a new class AudioRecording based on Recording class that: it will retain all the members of the Recording class, it will have an additional field bitrate (non-integer numerical; it cannot be modified once set), its constructors (non-parametrized and parametrized) will set all the attributes / fields of this class (reuse code by utilizing superclass / parent class constructors): Non-parametrized constructor should set bitrate to zero, If arguments for the parametrized constructor are illegal or null, it...

  • public class Player { private String name; private int health; public Player(String name) { this.name =...

    public class Player { private String name; private int health; public Player(String name) { this.name = name; } } Write a complete method using java to find a Player by name in an array of Player objects. Use a linear search algorithm. The method should either return the first Player object with the requested name, or null if no player with that name is found.

  • 4. Command pattern //class Stock public class Stock { private String name; private double price; public...

    4. Command pattern //class Stock public class Stock { private String name; private double price; public Product(String name, double price) { this.name = name; this.price = price; } public void buy(int quantity){ System.out.println(“BOUGHT: “ + quantity + “x “ + this); } public void sell(int quantity){ System.out.println(“SOLD: “ + quantity + “x “ + this); } public String toString() { return “Product [name=” + name + “, price=” + price + “]”; } } a. Create two command classes that...

  • Assignment (to be done in Java): Person Class: public class Person extends Passenger{ private int numOffspring;...

    Assignment (to be done in Java): Person Class: public class Person extends Passenger{ private int numOffspring; public Person() {    this.numOffspring = 0; } public Person (int numOffspring) {    this.numOffspring = numOffspring; } public Person(String name, int birthYear, double weight, double height, char gender, int numCarryOn, int numOffspring) {    super(name, birthYear, weight, height, gender, numCarryOn);       if(numOffspring < 0) {        this.numOffspring = 0;    }    this.numOffspring = numOffspring; } public int getNumOffspring() {   ...

  • Java Do 72a, 72b, 72c, 72d. Code & output required. public class Employee { private int...

    Java Do 72a, 72b, 72c, 72d. Code & output required. public class Employee { private int id; private String name; private int sal; public Employee(int id, String name, int sal) { super(); this.id = id; this.name = name; this.sal = sal; } public int getid) { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; public void setName(String name) { this.name = name; } public int get Sall) { return sal;...

  • public class Song { private String title; private String artist; private int duration; public Song() {...

    public class Song { private String title; private String artist; private int duration; public Song() { this("", "", 0, 0); } public Song(String t, String a, int m, int s) { title = t; artist = a; duration = m * 60 + s; } public String getTitle() { return title; } public String getArtist() { return artist; } public int getDuration() { return duration; } public int getMinutes() { return duration / 60; } public int getSeconds() { return...

  • GIVEN CODES *****Boat.java***** import java.util.HashSet; import java.util.Set; public class Boat { private String name; //private instance...

    GIVEN CODES *****Boat.java***** import java.util.HashSet; import java.util.Set; public class Boat { private String name; //private instance variable name of type String private String boatClass; //private instance variable boatClass of type String private int regNum; //private instance variable regNum of type int private Set<String> crew = new HashSet<String>(); public void setName(String name) { this.name = name; } public void setBoastClass(String boatClass) { this.boatClass = boatClass; } public void setRegNum(int regNum) { this.regNum = regNum; } public String getName() { return name;...

  • public class Animal {    private String name; //line 1    private int weight; //line 2...

    public class Animal {    private String name; //line 1    private int weight; //line 2    private String getName(){       return name;    } //line 3    public int fetchWeight(){       return weight; } //line 4 } public class Dog extends Animal {    private String food; //line 5    public void mystery(){       //System.out.println("Name = " + name); //line 6            System.out.println("Food = " + food); //line 7    } } I want to know the super...

  • public class ConsCell { private int head; private ConsCell tail; public ConsCell(int h, ConsCell t) {...

    public class ConsCell { private int head; private ConsCell tail; public ConsCell(int h, ConsCell t) { head = h; tail = t; } public int getHead() { return head; } public ConsCell getTail() { return tail; } public void setTail(ConsCell t) { tail = t; } } public class IntList { private ConsCell start; public IntList (ConsCell s) { start = s; } public IntList cons(int h) { return new IntList(new ConsCell(h, start)); } public int length() { int len...

  • What is the final value of the count field? public class Person { private String name;...

    What is the final value of the count field? public class Person { private String name; private int age; private static int count=0; public Person(String name, int age) { this.name = name; this.age age; count++; } public static void main(String[] args) { Person a - new Person("John Doe". 35): Person b new Person("Jane Doe", 38): for(int i=0;i<5;i++) Person tempa

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT