2

These are my instructions:

  • Write a Song class that contains the following properties along with appropriate get/set methods. Please note this is a pretty simple object and not anywhere near the level of completeness required for writing an actual music player program (especially since this program doesn't know how to read and play songs and there are no MP3 files involved!).

    • Name (String) - The name of the song
    • Artist (String) - The name of the band or artist
    • Album (String) - The album from which the song is from
    • Length (int) - The length of the song in milliseconds
  • Now write a class called Playlist that will manage your song objects. It should have the following properties.

    • Songs (an array of data type Song) - The size is hard-coded to 12. That's all the songs we have room for in our collection.
    • Count (int) - Keeps track of the total number of songs stored in the collection and also the next open position in the songs array.
    • Name - A string that holds the name of the playlist. This is incase you ever want to make multiple playlists. Some names for a playlist might be "Gym" or "Podcasts" but the user can enter anything they like here. You should write a getter/setter for this value.

      It should have at least the following behaviors.

    • void add(Song a) - Adds a Song to the collection. If the collection is full it does nothing. This could print an error to the screen if you want it to. We'll cover exceptions later in the semester and you are not expected to use them here.
    • Song get(int i) - Returns the Song at the given index but doesn't remove it. You need to check the value of i to avoid accessing invalid array indices. Song remove(String name) - Removes the first Song in the collection with the given name. If it does not find a Song with that name it returns null. Yes, this requires a linear search of the Songs array.
    • void print() - Prints a nicely formatted list of all the Songs in the collection.
    • int size() - Returns the total number of Songs in the collection. int totalTime() - Returns the cumulative time of all the Songs in the playlist in milliseconds.
    • String formattedTotalTime() Returns the cumulative time of all the Songs in the playlist in the format hh:mm:ss as a String.
    • void clear() - Deletes all Songs from the collection.
  • Finally, write a driver class to test this out. Create a Playlist object and give it a name. Then, make a few Song objects and add them to your Playlist object. Print the collection, remove some Songs, and print the collection again. Do your best to demonstrate the full functionality of the program. This does not need to be an interactive driver. It's fine to hard-code this piece of the assignment.

Additional Notes

When you remove a Song from the list you can't leave an empty spot in the array. How will you deal with this? DO NOT use an ArrayList to manage your Songs, you must use an array. DO NOT extend ArrayList for this assignment. We'll do that later ;-) You are free to write additional "helper" methods if you want to do so. The methods I've listed are the minimum requirements for the assignment. You are always free to add more if it helps.

What to Submit Your .java files for the Song, Playlist, and Driver classes.

This is what I have:

Song.java

    public class Song {

   public String name;
   public String artist;
   public String album;
   public int length;

   public Song(String songName, String artistName, String albumName, int trackLength) {
      this.name = name;
      this.artist = artist;
      this.album = album;
      this.length = length;
   }
   public void setName(String songName) {
      name = songName;
   }
   public String getName() {
      return name;
   }
   public void setArtist(String artistName) {
      artist = artistName;
   }
   public String getArtist() {
      return artist;
   }
   public void setAlbum(String albumName) {
      album = albumName;
   }
   public String getAlbum() {
      return album;
   }
   public void setLength(int h, int m, int s) {
      length = (h*3600 + m*60 + s);
      if(h==0) {
         length = (m*60+s);
      }   
   }
   public int getLength() {
      return length;
   }

   public String toString() {
      return "Title: " + getName() + ", Artist: " + getArtist()
                + ", Album: " + getAlbum() + ", Track Length: " + getLength();
   }         

}       

Playlist.java

    public class Playlist {

   private Song[] songs;
   private int count;
   private String playlistName;

   public Playlist() {
      songs = new Song[12];
      count = 0;
   }   
   public String getPlaylistName() {
      return playlistName;
   }
   public void setPlayListName() {
      this.playlistName = playlistName;
   }

   public void add(Song a) {
      if(count == songs.length) {
         System.out.println("ERROR: Collection is full. Songs were not added to the Playlist.");
      }
      songs[count] = a;
      count++;
   }   

   public Song get(int i) {
      if(count > i) {
        return songs[i];
      }
      else {
         return null;
      }
   }     
   public Song remove(String name) {
      boolean found = false;
      int indexToRemove = 0;
      while(indexToRemove < count && !found) {
         if(songs[indexToRemove].getName().equals(name)) {
            found = true;
         }
         else {
            indexToRemove++;
         }        
      }
      if(indexToRemove < count) {
         for(int from = indexToRemove + 1; from < count; from++) {
            songs[from-1] = songs[from];
         }
         songs[count-1] = null;
         count--;   
      }
      return null;   
   }         

   public void print() {
      String result = "NumSongs = " + count
            + " / PlayList song limit = " + songs.length + "\n";

         for (int i=0; i<count; i++) {
            result += ("songList[" + i + "] = <"
                        + songs[i] + ">\n");
         }
      System.out.println(result.toString() + "\n");
   }   
   public int size() {
      return count;
   }
   public int totalTime() {
      int totalTime = 0;
      for (int i=0; i<count; i++) { 
         totalTime = songs[i].getLength();
      }      
      return totalTime;
   }
   public String formattedTotalTime() {
      long h, m, s;
      String lengthString;
      s = Song.length;
      m = s/60;
      s = s%60;
      h = m/60;
      m = m%60;
      lengthString = String.format("%02d",h) + ":" +
      String.format("%02d",m) + ":" +
      String.format("%02d",s);
      return lengthString;
   }
   public void clear() {
      for (int i=0; i<songs.length; i++) {
         songs[i] = null;
         count = 0;
      }   
      return ;
   }
}   

Driver class

public class SongDriver {
   public static void main(String[] args) {
      Playlist one = new Playlist();

      Song song1 = new Song("Hotline Bling", "Drake", "Hotline Bing - Single", 267000);
      Song song2 = new Song("What Do You Mean?", "Justin Bieber", "What Do You Mean? - Single", 207000);
      Song song3 = new Song("Watch Me", "Silento", "Watch Me (Whip / Nae Nae) - Single", 185000);
      Song song4 = new Song("679", "Fetty Wap ft. Remy Boyz", "Fetty Wap", 185000);
      Song song5 = new Song("Can't Feel My Face", "The Weeknd", "Beauty Behind the Madness", 213000);
      Song song6 = new Song("Good for You", "Selena Gomez ft. A$AP Rocky", "Good for You - Single", 221000);
      Song song7 = new Song("If You", "Big Bang", "MADE", 264000);

      one.add(song1);               
      one.add(song2);               
      one.add(song3);                
      one.add(song4);                
      one.add(song5);               
      one.add(song6);                
      one.add(song7);


      Playlist.print();

      one.remove("679");            
      one.remove("Watch Me");

      Playlist.print(); 
      Playlist.clear();
   }
}   

My problem is that my output doesn't come out the way I want it to come out...

 ----jGRASP exec: java SongDriver

NumSongs = 7 / PlayList song limit = 12
songList[0] = <Title: null, Artist: null, Album: null, Track Length: 0>
songList[1] = <Title: null, Artist: null, Album: null, Track Length: 0>
songList[2] = <Title: null, Artist: null, Album: null, Track Length: 0>
songList[3] = <Title: null, Artist: null, Album: null, Track Length: 0>
songList[4] = <Title: null, Artist: null, Album: null, Track Length: 0>
songList[5] = <Title: null, Artist: null, Album: null, Track Length: 0>
songList[6] = <Title: null, Artist: null, Album: null, Track Length: 0>


Exception in thread "main" java.lang.NullPointerException
    at Playlist.remove(Playlist.java:38)
    at SongDriver.main(SongDriver.java:24)

 ----jGRASP wedge2: exit code for process is 1.
 ----jGRASP: operation complete.

The list doesn't show up, everything is just a null which i don't get why. The Track length isn't in hh:mm:ss and it's all just zeros. I converted each length of the song to milliseconds and put it as milliseconds in the Driver class. And I do not get why it keeps throwing nullpointerException at

 if(songs[indexToRemove].getName().equals(name)) {

Any help would be appreaciated! Thanks.

UPDATE I removed the static and made the changes provided but now when i compile the collection, i get errors saying non-static method cannot be referenced from a static context which is why I put the statics in the first place, so i don't understand...

 ----jGRASP exec: javac -g @Playlist_source_files_1673083996069575159jgr

SongDriver.java:22: error: non-static method print() cannot be referenced from a static context
      Playlist.print();
              ^
SongDriver.java:27: error: non-static method print() cannot be referenced from a static context
      Playlist.print(); 
              ^
SongDriver.java:28: error: non-static method clear() cannot be referenced from a static context
      Playlist.clear();
              ^
Playlist.java:78: error: non-static variable length cannot be referenced from a static context
      s = Song.length;
              ^
4 errors

 ----jGRASP wedge2: exit code for process is 1.
 ----jGRASP: operation complete.

UPDATE 2 //track length

CHANGED: Song.java

   public void setLength(int trackLength) {
      length = trackLength; 
   }

CHANGED: Playlist.java

   public String formattedTotalTime() {
      long time = totalTime();
      String lengthString;
      double overflow = time;
      long h = time / HOURS;
      long overFlow = time % HOURS;
      long m = overFlow / MINS;
      overFlow = time % MINS;
      long s = overFlow / SECS;
      lengthString = String.format("%02d",h) + ":" +
      String.format("%02d",m) + ":" +
      String.format("%02d",s);
      return lengthString;
   }

ADDED: Playlist.java

   public static long HOURS = 60 * 60 * 1000;
   public static long MINS = 60 * 1000;
   public static long SECS = 1000;

UPDATE 3 //Still not working

>  ----jGRASP exec: java SongDriver

NumSongs = 7 / PlayList song limit = 12
songList[0] = <Title: Hotline Bling, Artist: Drake, Album: Hotline Bing - Single, Track Length: 267000>
songList[1] = <Title: What Do You Mean?, Artist: Justin Bieber, Album: What Do You Mean? - Single, Track Length: 207000>
songList[2] = <Title: Watch Me, Artist: Silento, Album: Watch Me (Whip / Nae Nae) - Single, Track Length: 185000>
songList[3] = <Title: 679, Artist: Fetty Wap ft. Remy Boyz, Album: Fetty Wap, Track Length: 185000>
songList[4] = <Title: Can't Feel My Face, Artist: The Weeknd, Album: Beauty Behind the Madness, Track Length: 213000>
songList[5] = <Title: Good for You, Artist: Selena Gomez ft. A$AP Rocky, Album: Good for You - Single, Track Length: 221000>
songList[6] = <Title: If You, Artist: Big Bang, Album: MADE, Track Length: 264000>


NumSongs = 6 / PlayList song limit = 12
songList[0] = <Title: Hotline Bling, Artist: Drake, Album: Hotline Bing - Single, Track Length: 267000>
songList[1] = <Title: What Do You Mean?, Artist: Justin Bieber, Album: What Do You Mean? - Single, Track Length: 207000>
songList[2] = <Title: Watch Me, Artist: Silento, Album: Watch Me (Whip / Nae Nae) - Single, Track Length: 185000>
songList[3] = <Title: Can't Feel My Face, Artist: The Weeknd, Album: Beauty Behind the Madness, Track Length: 213000>
songList[4] = <Title: Good for You, Artist: Selena Gomez ft. A$AP Rocky, Album: Good for You - Single, Track Length: 221000>
songList[5] = <Title: If You, Artist: Big Bang, Album: MADE, Track Length: 264000>


NumSongs = 5 / PlayList song limit = 12
songList[0] = <Title: Hotline Bling, Artist: Drake, Album: Hotline Bing - Single, Track Length: 267000>
songList[1] = <Title: What Do You Mean?, Artist: Justin Bieber, Album: What Do You Mean? - Single, Track Length: 207000>
songList[2] = <Title: Can't Feel My Face, Artist: The Weeknd, Album: Beauty Behind the Madness, Track Length: 213000>
songList[3] = <Title: Good for You, Artist: Selena Gomez ft. A$AP Rocky, Album: Good for You - Single, Track Length: 221000>
songList[4] = <Title: If You, Artist: Big Bang, Album: MADE, Track Length: 264000>


NumSongs = 0 / PlayList song limit = 12



 ----jGRASP: operation complete.

Playlist.java

    public class Playlist {

   public static long HOURS = 60 * 60 * 1000;
   public static long MINS = 60 * 1000;
   public static long SECS = 1000;
   private Song[] songs;
   private int count;
   private String playlistName;

   public Playlist() {
      songs = new Song[12];
      count = 0;
   }   
   public String getPlaylistName() {
      return playlistName;
   }
   public void setPlayListName() {
      this.playlistName = playlistName;
   }

   public void add(Song a) {
      if(count == songs.length) {
         System.out.println("ERROR: Collection is full. Songs were not added to the Playlist.");
      }
      songs[count] = a;
      count++;
   }   

   public Song get(int i) {
      if(count > i) {
        return songs[i];
      }
      else {
         return null;
      }
   }     
   public Song remove(String name) {
      boolean found = false;
      int indexToRemove = 0;
      while(indexToRemove < count && !found) {
         if(songs[indexToRemove].getName().equals(name)) {
            found = true;
         }
         else {
            indexToRemove++;
         }        
      }
      if(indexToRemove < count) {
         for(int from = indexToRemove + 1; from < count; from++) {
            songs[from-1] = songs[from];
         }
         songs[count-1] = null;
         count--;   
      }
      return null;   
   }         

   public void print() {
      String result = "NumSongs = " + count
            + " / PlayList song limit = " + songs.length + "\n";

         for (int i=0; i<count; i++) {
            result += ("songList[" + i + "] = <"
                        + songs[i] + ">\n");
         }
      System.out.println(result.toString() + "\n");
   }   
   public int size() {
      return count;
   }
   public int totalTime() {
      int totalTime = 0;
      for (int i=0; i<count; i++) { 
         totalTime += songs[i].getLength();
      }      
      return totalTime;
   }
   public String formattedTotalTime() {
      long time = totalTime();
      String lengthString;
      double overflow = time;
      long h = time / HOURS;
      long overFlow = time % HOURS;
      long m = overFlow / MINS;
      overFlow = time % MINS;
      long s = overFlow / SECS;
      lengthString = String.format("%02d",h) + ":" +
      String.format("%02d",m) + ":" +
      String.format("%02d",s);
      return lengthString;
   }
   public void clear() {
      for (int i=0; i<songs.length; i++) {
         songs[i] = null;
         count = 0;
      }   
      return ;
   }
}   

Song.java

    public class Song {

   public String name;
   public String artist;
   public String album;
   public int length;

   public Song(String songName, String artistName, String albumName, int trackLength) {
      this.name = songName;
      this.artist = artistName;
      this.album = albumName;
      this.length = trackLength;
   }
   public void setName(String songName) {
      name = songName;
   }
   public String getName() {
      return name;
   }
   public void setArtist(String artistName) {
      artist = artistName;
   }
   public String getArtist() {
      return artist;
   }
   public void setAlbum(String albumName) {
      album = albumName;
   }
   public String getAlbum() {
      return album;
   }
   public void setLength(int trackLength) {
      length = trackLength;  
   }
   public int getLength() {
      return length;
   }

   public String toString() {
      return "Title: " + getName() + ", Artist: " + getArtist()
                + ", Album: " + getAlbum() + ", Track Length: " + getLength();
   }         

}               

Driver class:

public class SongDriver {
   public static void main(String[] args) {
      Playlist one = new Playlist();

      Song song1 = new Song("Hotline Bling", "Drake", "Hotline Bing - Single", 267000);
      Song song2 = new Song("What Do You Mean?", "Justin Bieber", "What Do You Mean? - Single", 207000);
      Song song3 = new Song("Watch Me", "Silento", "Watch Me (Whip / Nae Nae) - Single", 185000);
      Song song4 = new Song("679", "Fetty Wap ft. Remy Boyz", "Fetty Wap", 185000);
      Song song5 = new Song("Can't Feel My Face", "The Weeknd", "Beauty Behind the Madness", 213000);
      Song song6 = new Song("Good for You", "Selena Gomez ft. A$AP Rocky", "Good for You - Single", 221000);
      Song song7 = new Song("If You", "Big Bang", "MADE", 264000);

      one.add(song1);               
      one.add(song2);               
      one.add(song3);                
      one.add(song4);                
      one.add(song5);               
      one.add(song6);                
      one.add(song7);


      one.print();

      one.remove("679");   

      one.print();

      one.remove("Watch Me");

      one.print(); 

      one.clear();
      one.print();
       }
    }   
pyuntae
  • 742
  • 3
  • 10
  • 25
  • Start by getting rid of your `static` references to your fields – MadProgrammer Oct 16 '15 at 03:47
  • You MUST use a instance of the class in order to access instance fields, like `one.print()` instead of `Playlist.print()`. For the most part, until you're familiar with what `static` does, don't use it – MadProgrammer Oct 16 '15 at 04:36
  • Take a look at [What does the 'static' keyword do in a class?](http://stackoverflow.com/questions/413898/what-does-the-static-keyword-do-in-a-class) and [Understanding Class Members](https://docs.oracle.com/javase/tutorial/java/javaOO/classvars.html) for more details – MadProgrammer Oct 16 '15 at 04:37
  • In your `formattedTotalTime`, it should be using the `PlayList`'s length, not the `Song`'s, so many be `s = totalTime();` would be more appropriate – MadProgrammer Oct 16 '15 at 04:46
  • ope. Thanks! But even after doing all this my code still gives me the same error i stated for my output :( Everything is still null and the problem is still in the remove(String name) and the tracklength is still 0 – pyuntae Oct 16 '15 at 04:51
  • You didn't fix the `Song`'s constructor as I said, `this.name = name;` is wrong...otherwise it works just fine for me – MadProgrammer Oct 16 '15 at 04:55
  • omg.... u are genius. Thanks so much! – pyuntae Oct 16 '15 at 05:02
  • But now there is just one last thing. The track length time is supposed to be in hh:mm:ss format but it's still in milliseconds. – pyuntae Oct 16 '15 at 05:05

2 Answers2

0

All your fields and getters are static when it should be referring to instance fields :

public static String getName() {
      return name;
   }

Should really be :

public String getName(){
    return name;
}

And

public static String name;
public static String artist;
public static String album;
public static int length;

Should be :

private String name;
private String artist;
private String album;
private int length;

Otherwise, like I said, you were referring to class variables when your logic needs instance variables.

Yassin Hajaj
  • 21,337
  • 9
  • 51
  • 89
0

First, get rid of your static references to you class's fields

public class Song {
    public static String name;
    public static String artist;
    public static String album;
    public static int length;

This basically means that EVERY instance of Song will have the EXACT same values as each other (those which were applied last)

Next, in your Song's constructor, actually assign the parameters to your fields...

public Song(String songName, String artistName, String albumName, int trackLength) {
    this.name = name;
    this.artist = artist;
    this.album = album;
    this.length = length;
}

In this context, this.name = name; is simply assigning the value back to itself. Instead you want to do something more like...

public Song(String songName, String artistName, String albumName, int trackLength) {
    this.name = songName;
    this.artist = artistName;
    this.album = albumName;
    this.length = trackLength;
}

Next, I'd remove all static modifiers to your methods...

public static String getArtist() {

should be

public String getArtist() {

for example.

I strongly recommend that you go back through the rest of your code and make sure you haven't made the same mistakes

But now there is just one last thing. The track length time is supposed to be in hh:mm:ss format but it's still in milliseconds

It's been a long time since I've needed to do this, but in the past, I've used something like...

public static long HOURS = 60 * 60 * 1000;
public static long MINS = 60 * 1000;
public static long SECS = 1000;

public static void main(String[] args) {
    long time = (1 * HOURS) + (30 * MINS);

    double overflow = time;
    long h = time / HOURS;
    long overFlow = time % HOURS;
    long m = overFlow / MINS;
    overFlow = time % MINS;
    long s = overFlow / SECS;
    System.out.printf("%02d:%02d.%02d%n", h, m, s);
}

All the test code...

package javaapplication620;

public class SongDriver {

    public static void main(String[] args) {
        Playlist one = new Playlist();

        Song song1 = new Song("Hotline Bling", "Drake", "Hotline Bing - Single", 267000);
        Song song2 = new Song("What Do You Mean?", "Justin Bieber", "What Do You Mean? - Single", 207000);
        Song song3 = new Song("Watch Me", "Silento", "Watch Me (Whip / Nae Nae) - Single", 185000);
        Song song4 = new Song("679", "Fetty Wap ft. Remy Boyz", "Fetty Wap", 185000);
        Song song5 = new Song("Can't Feel My Face", "The Weeknd", "Beauty Behind the Madness", 213000);
        Song song6 = new Song("Good for You", "Selena Gomez ft. A$AP Rocky", "Good for You - Single", 221000);
        Song song7 = new Song("If You", "Big Bang", "MADE", 264000);

        one.add(song1);
        one.add(song2);
        one.add(song3);
        one.add(song4);
        one.add(song5);
        one.add(song6);
        one.add(song7);

        one.print();

        one.remove("679");
        one.remove("Watch Me");

        one.print();
        one.clear();
    }

    public static long HOURS = 60 * 60 * 1000;
    public static long MINS = 60 * 1000;
    public static long SECS = 1000;

    public static class Playlist {

        private Song[] songs;
        private int count;
        private String playlistName;

        public Playlist() {
            songs = new Song[12];
            count = 0;
        }

        public String getPlaylistName() {
            return playlistName;
        }

        public void setPlayListName() {
            this.playlistName = playlistName;
        }

        public void add(Song a) {
            if (count == songs.length) {
                System.out.println("ERROR: Collection is full. Songs were not added to the Playlist.");
            }
            songs[count] = a;
            count++;
        }

        public Song get(int i) {
            if (count > i) {
                return songs[i];
            } else {
                return null;
            }
        }

        public Song remove(String name) {
            boolean found = false;
            int indexToRemove = 0;
            while (indexToRemove < count && !found) {
                if (songs[indexToRemove].getName().equals(name)) {
                    found = true;
                } else {
                    indexToRemove++;
                }
            }
            if (indexToRemove < count) {
                for (int from = indexToRemove + 1; from < count; from++) {
                    songs[from - 1] = songs[from];
                }
                songs[count - 1] = null;
                count--;
            }
            return null;
        }

        public void print() {
            String result = "NumSongs = " + count
                            + " / PlayList song limit = " + songs.length + "\n";

            for (int i = 0; i < count; i++) {
                result += ("songList[" + i + "] = <"
                                + songs[i] + ">\n");
            }
            result += " / " + formattedTotalTime();
            System.out.println(result.toString());
        }

        public int size() {
            return count;
        }

        public int totalTime() {
            int totalTime = 0;
            for (int i = 0; i < count; i++) {
                totalTime += songs[i].getLength();
            }
            return totalTime;
        }

        public String formattedTotalTime() {
            return formatTime(totalTime());
        }

        public void clear() {
            for (int i = 0; i < songs.length; i++) {
                songs[i] = null;
                count = 0;
            }
            return;
        }
    }

    public static class Song {

        public String name;
        public String artist;
        public String album;
        public int length;

        public Song(String songName, String artistName, String albumName, int trackLength) {
            this.name = songName;
            this.artist = artistName;
            this.album = albumName;
            this.length = trackLength;
        }

        public void setName(String songName) {
            name = songName;
        }

        public String getName() {
            return name;
        }

        public void setArtist(String artistName) {
            artist = artistName;
        }

        public String getArtist() {
            return artist;
        }

        public void setAlbum(String albumName) {
            album = albumName;
        }

        public String getAlbum() {
            return album;
        }

        public void setLength(int h, int m, int s) {
            length = (h * 3600 + m * 60 + s);
            if (h == 0) {
                length = (m * 60 + s);
            }
        }

        public int getLength() {
            return length;
        }

        public String toString() {
            return "Title: " + getName() + ", Artist: " + getArtist()
                            + ", Album: " + getAlbum() + ", Track Length: " + formatTime(getLength());
        }

    }

    public static String formatTime(long time) {
            long overflow = time;
            long h = time / HOURS;
            overflow = time % HOURS;
            long m = overflow / MINS;
            overflow = time % MINS;
            long s = overflow / SECS;
            return String.format("%02d:%02d.%02d", h, m, s);

    }
}

This test code currently prints out

NumSongs = 7 / PlayList song limit = 12
songList[0] = <Title: Hotline Bling, Artist: Drake, Album: Hotline Bing - Single, Track Length: 00:04.27>
songList[1] = <Title: What Do You Mean?, Artist: Justin Bieber, Album: What Do You Mean? - Single, Track Length: 00:03.27>
songList[2] = <Title: Watch Me, Artist: Silento, Album: Watch Me (Whip / Nae Nae) - Single, Track Length: 00:03.05>
songList[3] = <Title: 679, Artist: Fetty Wap ft. Remy Boyz, Album: Fetty Wap, Track Length: 00:03.05>
songList[4] = <Title: Can't Feel My Face, Artist: The Weeknd, Album: Beauty Behind the Madness, Track Length: 00:03.33>
songList[5] = <Title: Good for You, Artist: Selena Gomez ft. A$AP Rocky, Album: Good for You - Single, Track Length: 00:03.41>
songList[6] = <Title: If You, Artist: Big Bang, Album: MADE, Track Length: 00:04.24>
 / 00:25.42
NumSongs = 5 / PlayList song limit = 12
songList[0] = <Title: Hotline Bling, Artist: Drake, Album: Hotline Bing - Single, Track Length: 00:04.27>
songList[1] = <Title: What Do You Mean?, Artist: Justin Bieber, Album: What Do You Mean? - Single, Track Length: 00:03.27>
songList[2] = <Title: Can't Feel My Face, Artist: The Weeknd, Album: Beauty Behind the Madness, Track Length: 00:03.33>
songList[3] = <Title: Good for You, Artist: Selena Gomez ft. A$AP Rocky, Album: Good for You - Single, Track Length: 00:03.41>
songList[4] = <Title: If You, Artist: Big Bang, Album: MADE, Track Length: 00:04.24>
 / 00:19.32
MadProgrammer
  • 343,457
  • 22
  • 230
  • 366
  • I updated but now i am getting error: non-static method print() cannot be referenced from a static context – pyuntae Oct 16 '15 at 04:32
  • http://stackoverflow.com/questions/413898/what-does-the-static-keyword-do-in-a-class – Yassin Hajaj Oct 16 '15 at 04:34
  • So i do not use the codes that i already formatted in setLength and in formattedTotalTime ? – pyuntae Oct 16 '15 at 05:25
  • Well, they're aren't working (apparently), you could give my code a try and see if it helps – MadProgrammer Oct 16 '15 at 05:27
  • I don't think you need to, as the method is converting the value to milliseconds, my code simply takes a millisecond value and converts it to h:m.s format – MadProgrammer Oct 16 '15 at 05:37
  • I used your code instead but now all the songs are 01:30:00 instead of each being different – pyuntae Oct 16 '15 at 05:41
  • Assign the total length of the play list to `time`, so instead of `long time = (1 * HOURS) + (30 * MINS);`, you would use `time = totalTime();` – MadProgrammer Oct 16 '15 at 05:42
  • Now every song is 00:04:24 x___x – pyuntae Oct 16 '15 at 05:50
  • i think it's only converting the last added song and then making all the songs have the same time – pyuntae Oct 16 '15 at 05:56
  • `totalTime = songs[i].getLength();` should be `totalTime += songs[i].getLength();` in your `totalTime` method, other wise you are just getting the last songs length, which `4.24` – MadProgrammer Oct 16 '15 at 05:58
  • Now unless i change delete the + getLength(); from my toString() in Song.java and add + formattedTotalTime() to the print() result format in Playlist.java it doesn't convert the time at all. If i do the changes i just mentioned, The first time i print all the time is 00:35:42 and the second time i print they are 00:22:37 and the third time i print they are 00:19:32 – pyuntae Oct 16 '15 at 06:18
  • I've posted my test code, which I've added the formatted length to the `Song#toString` and `Playlist#print` methods, which prints `00:25.42` for the first `print` and `00:19.32` for the second, which seems to be inline with what you code is doing (removing two songs) – MadProgrammer Oct 16 '15 at 06:25
  • Thanks, finally got it!!! my problem was that I was formatting the date in the playlist.java instead of adding it to the getLength in the Song.java class Thank you again!!!! – pyuntae Oct 16 '15 at 06:40