title: Playlist and Song and Driver
body: <p>These are my instructions:</p>
  
  <ul>
  <li><p>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!).</p>
  
  <ul>
  <li>Name (String) - The name of the song</li>
  <li>Artist (String) - The name of the band or artist</li>
  <li>Album (String) - The album from which the song is from</li>
  <li>Length (int) - The length of the song in milliseconds</li>
  </ul></li>
  <li><p>Now write a class called Playlist that will manage your song objects. It should have the following properties.</p>
  
  <ul>
  <li>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.</li>
  <li>Count (int) - Keeps track of the total number of songs stored in the collection and also the next open position in the songs array.</li>
  <li><p>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.</p>
  
  <p>It should have at least the following behaviors.</p></li>
  <li>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.</li>
  <li>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.</li>
  <li>void print() - Prints a nicely formatted list of all the Songs in the collection.</li>
  <li>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.</li>
  <li>String formattedTotalTime() Returns the cumulative time of all the Songs in the playlist in the format hh:mm:ss as a String.</li>
  <li>void clear() - Deletes all Songs from the collection.</li>
  </ul></li>
  <li><p>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.</p></li>
  </ul>
  
  <p><strong>Additional Notes</strong> </p>
  
  <p>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. </p>
  
  <p><strong>What to Submit</strong> Your .java files for the Song, Playlist, and Driver classes.</p>
  
  <p>This is what I have:</p>
  
  <p><strong>Song.java</strong></p>
  
  <pre><code>    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();
     }         
  
  }       
  </code></pre>
  
  <p><strong>Playlist.java</strong></p>
  
  <pre><code>    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 &gt; i) {
          return songs[i];
        }
        else {
           return null;
        }
     }     
     public Song remove(String name) {
        boolean found = false;
        int indexToRemove = 0;
        while(indexToRemove &lt; count &amp;&amp; !found) {
           if(songs[indexToRemove].getName().equals(name)) {
              found = true;
           }
           else {
              indexToRemove++;
           }        
        }
        if(indexToRemove &lt; count) {
           for(int from = indexToRemove + 1; from &lt; 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&lt;count; i++) {
              result += ("songList[" + i + "] = &lt;"
                          + songs[i] + "&gt;\n");
           }
        System.out.println(result.toString() + "\n");
     }   
     public int size() {
        return count;
     }
     public int totalTime() {
        int totalTime = 0;
        for (int i=0; i&lt;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&lt;songs.length; i++) {
           songs[i] = null;
           count = 0;
        }   
        return ;
     }
  }   
  </code></pre>
  
  <p><strong>Driver class</strong></p>
  
  <pre><code>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();
     }
  }   
  </code></pre>
  
  <p>My problem is that my output doesn't come out the way I want it to come out...</p>
  
  <pre><code> ----jGRASP exec: java SongDriver
  
  NumSongs = 7 / PlayList song limit = 12
  songList[0] = &lt;Title: null, Artist: null, Album: null, Track Length: 0&gt;
  songList[1] = &lt;Title: null, Artist: null, Album: null, Track Length: 0&gt;
  songList[2] = &lt;Title: null, Artist: null, Album: null, Track Length: 0&gt;
  songList[3] = &lt;Title: null, Artist: null, Album: null, Track Length: 0&gt;
  songList[4] = &lt;Title: null, Artist: null, Album: null, Track Length: 0&gt;
  songList[5] = &lt;Title: null, Artist: null, Album: null, Track Length: 0&gt;
  songList[6] = &lt;Title: null, Artist: null, Album: null, Track Length: 0&gt;
  
  
  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.
  </code></pre>
  
  <p>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</p>
  
  <pre><code> if(songs[indexToRemove].getName().equals(name)) {
  </code></pre>
  
  <p>Any help would be appreaciated! Thanks.</p>
  
  <p><strong>UPDATE</strong>
  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...</p>
  
  <pre><code> ----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.
  </code></pre>
  
  <p><strong>UPDATE 2</strong> <em>//track length</em></p>
  
  <p>CHANGED: Song.java</p>
  
  <pre><code>   public void setLength(int trackLength) {
        length = trackLength; 
     }
  </code></pre>
  
  <p>CHANGED: Playlist.java </p>
  
  <pre><code>   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;
     }
  </code></pre>
  
  <p>ADDED: Playlist.java</p>
  
  <pre><code>   public static long HOURS = 60 * 60 * 1000;
     public static long MINS = 60 * 1000;
     public static long SECS = 1000;
  </code></pre>
  
  <p><strong>UPDATE 3</strong> //Still not working</p>
  
  <pre><code>&gt;  ----jGRASP exec: java SongDriver
  
  NumSongs = 7 / PlayList song limit = 12
  songList[0] = &lt;Title: Hotline Bling, Artist: Drake, Album: Hotline Bing - Single, Track Length: 267000&gt;
  songList[1] = &lt;Title: What Do You Mean?, Artist: Justin Bieber, Album: What Do You Mean? - Single, Track Length: 207000&gt;
  songList[2] = &lt;Title: Watch Me, Artist: Silento, Album: Watch Me (Whip / Nae Nae) - Single, Track Length: 185000&gt;
  songList[3] = &lt;Title: 679, Artist: Fetty Wap ft. Remy Boyz, Album: Fetty Wap, Track Length: 185000&gt;
  songList[4] = &lt;Title: Can't Feel My Face, Artist: The Weeknd, Album: Beauty Behind the Madness, Track Length: 213000&gt;
  songList[5] = &lt;Title: Good for You, Artist: Selena Gomez ft. A$AP Rocky, Album: Good for You - Single, Track Length: 221000&gt;
  songList[6] = &lt;Title: If You, Artist: Big Bang, Album: MADE, Track Length: 264000&gt;
  
  
  NumSongs = 6 / PlayList song limit = 12
  songList[0] = &lt;Title: Hotline Bling, Artist: Drake, Album: Hotline Bing - Single, Track Length: 267000&gt;
  songList[1] = &lt;Title: What Do You Mean?, Artist: Justin Bieber, Album: What Do You Mean? - Single, Track Length: 207000&gt;
  songList[2] = &lt;Title: Watch Me, Artist: Silento, Album: Watch Me (Whip / Nae Nae) - Single, Track Length: 185000&gt;
  songList[3] = &lt;Title: Can't Feel My Face, Artist: The Weeknd, Album: Beauty Behind the Madness, Track Length: 213000&gt;
  songList[4] = &lt;Title: Good for You, Artist: Selena Gomez ft. A$AP Rocky, Album: Good for You - Single, Track Length: 221000&gt;
  songList[5] = &lt;Title: If You, Artist: Big Bang, Album: MADE, Track Length: 264000&gt;
  
  
  NumSongs = 5 / PlayList song limit = 12
  songList[0] = &lt;Title: Hotline Bling, Artist: Drake, Album: Hotline Bing - Single, Track Length: 267000&gt;
  songList[1] = &lt;Title: What Do You Mean?, Artist: Justin Bieber, Album: What Do You Mean? - Single, Track Length: 207000&gt;
  songList[2] = &lt;Title: Can't Feel My Face, Artist: The Weeknd, Album: Beauty Behind the Madness, Track Length: 213000&gt;
  songList[3] = &lt;Title: Good for You, Artist: Selena Gomez ft. A$AP Rocky, Album: Good for You - Single, Track Length: 221000&gt;
  songList[4] = &lt;Title: If You, Artist: Big Bang, Album: MADE, Track Length: 264000&gt;
  
  
  NumSongs = 0 / PlayList song limit = 12
  
  
  
   ----jGRASP: operation complete.
  </code></pre>
  
  <p>Playlist.java</p>
  
  <pre><code>    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 &gt; i) {
          return songs[i];
        }
        else {
           return null;
        }
     }     
     public Song remove(String name) {
        boolean found = false;
        int indexToRemove = 0;
        while(indexToRemove &lt; count &amp;&amp; !found) {
           if(songs[indexToRemove].getName().equals(name)) {
              found = true;
           }
           else {
              indexToRemove++;
           }        
        }
        if(indexToRemove &lt; count) {
           for(int from = indexToRemove + 1; from &lt; 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&lt;count; i++) {
              result += ("songList[" + i + "] = &lt;"
                          + songs[i] + "&gt;\n");
           }
        System.out.println(result.toString() + "\n");
     }   
     public int size() {
        return count;
     }
     public int totalTime() {
        int totalTime = 0;
        for (int i=0; i&lt;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&lt;songs.length; i++) {
           songs[i] = null;
           count = 0;
        }   
        return ;
     }
  }   
  </code></pre>
  
  <p>Song.java</p>
  
  <pre><code>    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();
     }         
  
  }               
  </code></pre>
  
  <p>Driver class:</p>
  
  <pre><code>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();
         }
      }   
  </code></pre>