播放列表和歌曲及驱动程序 [英] Playlist and Song and Driver

查看:94
本文介绍了播放列表和歌曲及驱动程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这些是我的说明:




  • 写一个Song类,其中包含以下属性以及适当的get / set方法。请注意,这是一个非常简单的对象,并且没有达到编写实际音乐播放器程序所需的完整性水平(特别是因为该程序不知道如何读取和播放歌曲,并且不涉及MP3文件!)。 / p>


    • 名称(字符串)-歌曲的名称

    • 艺术家(字符串)-名称乐队或歌手

    • 专辑(字符串)-歌曲所在的专辑

    • 长度(整数)-歌曲的长度毫秒


  • 现在编写一个名为播放列表的类,它将管理您的歌曲对象。它应该具有以下属性。




    • 歌曲(数据类型为Song的数组)-大小被硬编码为12。

    • 计数(int)-跟踪存储在集合中的歌曲总数,以及歌曲数组中的下一个打开位置。 / li>
    • 名称-包含播放列表名称的字符串。这是为了防止您要制作多个播放列表。播放列表的某些名称可能是 Gym或 Podcast,但用户可以在此处输入他们喜欢的任何内容。您应该为此值写一个getter / setter。



      它至少应具有以下行为。


    • void add(Song a)-将歌曲添加到集合中。如果集合已满,则不执行任何操作。如果需要,这可能会在屏幕上显示错误。

    • Song get(int i)-返回给定索引处的Song,但不会将其删除。您需要检查i的值,以避免访问无效的数组索引。
      Song remove(String name)-删除集合中具有给定名称的第一首Song。如果找不到具有该名称的歌曲,则返回null。是的,这需要对Songs数组进行线性搜索。

    • void print()-打印格式正确的列表中所有Songs的列表。

    • int size()-返回集合中的歌曲总数。
      int totalTime()-以毫秒为单位返回播放列表中所有歌曲的累积时间。

    • String formattedTotalTime()返回播放列表中所有歌曲的累积时间,以毫秒为单位。格式:hh:mm:ss作为字符串。

    • void clear()-从集合中删除所有歌曲。


  • 最后,编写一个驱动程序类对此进行测试。创建一个播放列表对象并为其命名。然后,制作一些Song对象并将它们添加到您的Playlist对象中。打印收藏,删除一些歌曲,然后再次打印收藏。尽力演示该程序的全部功能。不需要是交互式驱动程序。可以对这部分作业进行硬编码。




附加说明



当您从列表中删除歌曲时,您不能在数组中留一个空白点。您将如何处理?请勿使用ArrayList来管理您的歌曲,而必须使用数组。不要为该分配扩展ArrayList。稍后我们会做;-)如果需要,您可以自由编写其他帮助程序方法。我列出的方法是分配的最低要求。如果有帮助,您随时可以添加更多。



要提交的内容,您的.java文件用于Song,Playlist和Driver类。



这就是我所拥有的:



Song.java

 公共类Song {

public字符串名称;
个公共String艺术家;
公开String专辑;
public int长度;

public Song(字符串songName,字符串artistName,字符串AlbumName,int trackLength){
this.name = name;
this.artist =艺术家;
this.album =相簿;
this.length =长度;
}
public void setName(String songName){
name = songName;
}
public String getName(){
返回名称;
}
public void setArtist(String artistName){
artist = artistName;
}
public String getArtist(){
return artist;
}
public void setAlbum(String albumName){
album = albumName;
}
public String getAlbum(){
返回相册;
}
public void setLength(int h,int m,int s){
length =(h * 3600 + m * 60 + s);
if(h == 0){
长度=(m * 60 + s);
}
}
public int getLength(){
返回长度;
}

public String toString(){
return Title: + getName()+,Artist: + getArtist()
+,专辑: + getAlbum()+,轨道长度: + getLength();
}

}

Playlist.java

 公共类播放列表{

私人Song []歌曲;
私人整数计数;
private String playlistName;

public Playlist(){
首歌曲=新首歌曲[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(错误:集合为歌曲未添加到播放列表中。);
}
首歌曲[count] = a;
count ++;
}

公共歌曲get(int i){
if(count> i){
返回歌曲[i];
}
else {
返回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 ++){
歌曲[from -1] =歌曲[来自];
}
首歌曲[count-1] = null;
个数-;
}
返回null;
}

public void print(){
字符串结果= NumSongs = +计数
+ /播放列表的歌曲限制= + songs.length + n;

for(int i = 0; i< count; i ++){
结果+ =( songList [ + i +] =<
+歌曲[ i] +> \n);
}
System.out.println(result.toString()+ \n);
}
public int size(){
返回计数;
}
public int totalTime(){
int totalTime = 0;
for(int i = 0; i< count; i ++){
totalTime = songs [i] .getLength();
}
返回totalTime;
}
public String formattedTotalTime(){
long h,m,s;
字符串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);
返回lengthString;
}
public void clear(){
for(int i = 0; i< songs.length; i ++){{b $ b songs [i] = null;
count = 0;
}
回报;
}
}

驾驶员舱

 公共类SongDriver {
public static void main(String [] args){
播放列表一个=新播放列表();

Song song1 = new Song( Hotline Bling, Drake, Hotline Bing-Single,267000);
Song song2 = new Song(您的意思是什么?, Justin Bieber,您的意思是什么?-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( Ca n't Feel My Face, The Weeknd, Behind Be 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();
}
}

我的问题是我的输出没有输出我希望它出来的方式...

  ---- jGRASP exec:java SongDriver 

NumSongs = 7 /播放列表歌曲限制= 12
songList [0] =<标题:null,艺术家:null,专辑:null,曲目长度:0>;
songList [1] =<标题:null,艺术家:null,专辑:null,音轨长度:0>
songList [2] =<标题:null,艺术家:null,专辑:null,音轨长度:0>
songList [3] =<标题:null,艺术家:null,专辑:null,音轨长度:0>
songList [4] =<标题:null,艺术家:null,专辑:null,音轨长度:0>
songList [5] =<标题:null,艺术家:null,专辑:null,音轨长度:0>
songList [6] =<标题:null,艺术家:null,专辑:null,音轨长度:0>


线程 main中的异常java.lang.NullPointerException Playlist.remove(Playlist.java:38)的
SongDriver.main(SongDriver.java)的
:24)

---- jGRASP楔2:进程的退出代码为1。
---- jGRASP:操作完成。

列表未显示,所有内容均为空,我不知道为什么。轨道长度不是以hh:mm:ss为单位,而是全零。我将歌曲的每个长度都转换为毫秒,并将其作为毫秒数放入Driver类中。而且我不明白为什么它会不断抛出nullpointerException

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

任何帮助都会得到帮助!谢谢。



更新
我删除了静态文件并进行了更改,但是现在当我编译集合时,出现错误提示非静态方法不能从静态上下文中引用,这就是为什么我将静态放在首位的原因,所以我不明白...

  ---- jGRASP执行:javac -g @ Playlist_source_files_1673083996069575159jgr 

SongDriver.java:22:错误:非静态方法print()不能从静态上下文$ b引用$ b Playlist.print();
^
SongDriver.java:27:错误:无法从静态上下文中引用非静态方法print()
Playlist.print();
^
SongDriver.java:28:错误:无法从静态上下文中引用非静态方法clear()
Playlist.clear();
^
Playlist.java:78:错误:无法从静态上下文中引用非静态变量长度
s = Song.length;
^
4个错误

---- jGRASP楔子2:进程的退出代码为1。
---- jGRASP:操作完成。

更新2 //磁道长度



已更改:Song.java

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

已更改:Playlist.java

 公共字符串formattedTotalTime(){
long time = totalTime();
字符串lengthString;
两次溢出=时间;
long h =时间/小时;
长溢出=时间%小时;
long m =溢出/ MINS;
溢出=时间%MINS;
long s =溢出/ SECS;
lengthString = String.format(%02d,h)+: +
String.format(%02d,m)+: +
String.format( %02d,s);
返回lengthString;
}

添加:Playlist.java

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

UPDATE 3 //仍然不起作用

 > ---- jGRASP执行程序:java SongDriver 

NumSongs = 7 /播放列表歌曲限制= 12
songList [0] =<标题:热线Bling,艺术家:Drake,专辑:Hotline Bing -单曲径:267000>
songList [1] =<标题:您的意思是什么?,歌手:Justin Bieber,专辑:您的意思是什么? -单曲,轨道长度:207000>
songList [2] =<标题:Watch Me,艺术家:Silento,专辑:Watch Me(Whip / Nae Nae)-单曲,曲目长度:185000>
songList [3] =<标题:679,艺术家:Fetty Wap ft。Remy Boyz,专辑:Fetty Wap,曲目长度:185000>
songList [4] =<标题:Ca n't Feel My Face,艺术家:The Weeknd,专辑:Beauty Behind the Madness,曲目长度:213000>
songList [5] =<标题:对您有利,歌手:Selena Gomez ft。A $ AP Rocky,专辑:对您有利-单曲,曲目长度:221000>
songList [6] =<标题:如果您,歌手:Big Bang,专辑:MADE,曲目长度:264000>


NumSongs = 6 /播放列表歌曲限制= 12
songList [0] =< Title:热线Bling,艺术家:Drake,专辑:Hotline Bing-单曲,曲目长度:267000>
songList [1] =<标题:您的意思是什么?,歌手:Justin Bieber,专辑:您的意思是什么? -单曲,轨道长度:207000>
songList [2] =<标题:Watch Me,艺术家:Silento,专辑:Watch Me(Whip / Nae Nae)-单曲,曲目长度:185000>
songList [3] =<标题:Ca n't Feel My Face,艺术家:The Weeknd,专辑:Beauty Behind the Madness,曲目长度:213000>
songList [4] =<标题:对您有利,歌手:Selena Gomez ft。A $ AP Rocky,专辑:对您有利-单曲,曲目长度:221000>
songList [5] =<标题:如果您,歌手:Big Bang,专辑:MADE,曲目长度:264000>


NumSongs = 5 /播放列表歌曲限制= 12
songList [0] =<标题:热线Bling,歌手:Drake,专辑:热线Bing-单曲,曲目长度:267000>
songList [1] =<标题:您的意思是什么?,歌手:Justin Bieber,专辑:您的意思是什么? -单曲,轨道长度:207000>
songList [2] =<标题:Ca n't Feel My Face,艺术家:The Weeknd,专辑:Beauty Behind the Madness,曲目长度:213000>
songList [3] =<标题:对您有利,艺术家:Selena Gomez ft。A $ AP Rocky,专辑:对您有利-单曲,曲目长度:221000>
songList [4] =<标题:如果您,歌手:Big Bang,专辑:MADE,曲目长度:264000>


NumSongs = 0 /播放列表歌曲限制= 12



---- jGRASP:操作完成。

Playlist.java

 公共类播放列表{

public static long HOURS = 60 * 60 * 1000;
public static long MINS = 60 * 1000;
public static long SECS = 1000;
私人Song []歌曲;
私人整数计数;
private String playlistName;

public Playlist(){
首歌曲=新首歌曲[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(错误:集合为歌曲未添加到播放列表中。);
}
首歌曲[count] = a;
count ++;
}

公共歌曲get(int i){
if(count> i){
返回歌曲[i];
}
else {
返回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 ++){
歌曲[from -1] =歌曲[来自];
}
首歌曲[count-1] = null;
个数-;
}
返回null;
}

public void print(){
字符串结果= NumSongs = +计数
+ /播放列表的歌曲限制= + songs.length + n;

for(int i = 0; i< count; i ++){
结果+ =( songList [ + i +] =<
+歌曲[ i] +> \n);
}
System.out.println(result.toString()+ \n);
}
public int size(){
返回计数;
}
public int totalTime(){
int totalTime = 0;
for(int i = 0; i< count; i ++){
totalTime + = songs [i] .getLength();
}
返回totalTime;
}
public String formattedTotalTime(){
long time = totalTime();
字符串lengthString;
两次溢出=时间;
long h =时间/小时;
长溢出=时间%小时;
long m =溢出/ MINS;
溢出=时间%MINS;
long s =溢出/ SECS;
lengthString = String.format(%02d,h)+: +
String.format(%02d,m)+: +
String.format( %02d,s);
返回lengthString;
}
public void clear(){
for(int i = 0; i< songs.length; i ++){{b $ b songs [i] = null;
count = 0;
}
回报;
}
}

Song.java

 公共类Song {

public字符串名称;
个公共String艺术家;
公开String专辑;
public int长度;

public Song(字符串songName,字符串artistName,字符串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(){
返回名称;
}
public void setArtist(String artistName){
artist = artistName;
}
public String getArtist(){
return artist;
}
public void setAlbum(String albumName){
album = albumName;
}
public String getAlbum(){
返回相册;
}
public void setLength(int trackLength){
length = trackLength;
}
public int getLength(){
返回长度;
}

public String toString(){
return Title: + getName()+,Artist: + getArtist()
+,专辑: + getAlbum()+,轨道长度: + getLength();
}

}

驾驶员等级:

 公共类SongDriver {
public static void main(String [] args){
播放列表一个=新的播放列表() ;

Song song1 = new Song( Hotline Bling, Drake, Hotline Bing-Single,267000);
Song song2 = new Song(您的意思是什么?, Justin Bieber,您的意思是什么?-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( Ca n't Feel My Face, The Weeknd, Behind Be 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();
}
}


解决方案

第一个,摆脱对类字段的静态引用

 公共类Song {
public static String name;
public static String artist;
公共静态String相册;
public static int长度;

这基本上意味着 Song 的每个实例将具有彼此完全相同的值(最后应用的值)



下一步,在您的歌曲'中s的构造函数,实际上是将参数分配给您的字段...

  public Song(String songName,String artistName,String albumName,int trackLength){
this.name = name;
this.artist =艺术家;
this.album =相簿;
this.length =长度;
}

在这种情况下, this.name = name; 只是将值分配回自身。相反,您想做更多类似的事情...

 公共歌曲(String songName,String artistName,String albumName,int trackLength) {
this.name = songName;
this.artist = artistName;
this.album = albumName;
this.length = trackLength;
}

接下来,我将删除所有静态您方法的修饰符...

  public static String getArtist(){

应该

 公共字符串getArtist(){例如



我强烈建议您回顾其余代码,并确保没有犯同样的错误


只是最后一件事。轨道长度时间应该采用hh:mm:ss格式,但仍以毫秒为单位


需要这样做,但是在过去,我使用过类似的东西……

 公共静态长HOURS = 60 * 60 * 1000; 
public static long MINS = 60 * 1000;
public static long SECS = 1000;

public static void main(String [] args){
长时间=(1 *小时)+(30 * MINS);

两次溢出=时间;
long h =时间/小时;
长溢出=时间%小时;
long m =溢出/ MINS;
溢出=时间%MINS;
long s =溢出/ SECS;
System.out.printf(%02d:%02d。%02d%n,h,m,s);
}



所有测试代码...



 包javaapplication620; 

公共类SongDriver {

public static void main(String [] args){
播放列表一个=新的Playlist();

Song song1 = new Song( Hotline Bling, Drake, Hotline Bing-Single,267000);
Song song2 = new Song(您的意思是什么?, Justin Bieber,您的意思是什么?-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( Ca n't Feel My Face, The Weeknd, Behind Be 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();
}

公共静态长时数= 60 * 60 * 1000;
public static long MINS = 60 * 1000;
public static long SECS = 1000;

公共静态类播放列表{

私人Song []首歌曲;
私人整数计数;
private String playlistName;

public Playlist(){
首歌曲=新首歌曲[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(错误:集合为歌曲未添加到播放列表中。);
}
首歌曲[count] = a;
count ++;
}

公共歌曲get(int i){
if(count> i){
返回歌曲[i];
} else {
返回null;
}
}

public Song remove(String name){
boolean found = false;
int indexToRemove = 0;
而(indexToRemove< count&!found){
if(songs [indexToRemove] .getName()。equals(name)){
found = true;
} else {
indexToRemove ++;
}
}
if(indexToRemove< count){
for(int from = indexToRemove + 1; from< count; from ++){
首歌曲[来自-1] =歌曲[来自];
}
首歌曲[count-1] = null;
个数-;
}
返回null;
}

public void print(){
字符串结果= NumSongs = +计数
+ /播放列表的歌曲限制= + songs.length + n;

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

public int size(){
返回计数;
}

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

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

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

公共静态类Song {

public字符串名称;
个公共String艺术家;
公开String专辑;
public int长度;

public Song(字符串songName,字符串artistName,字符串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(){
返回名称;
}

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

public String getArtist(){
return artist;
}

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

public String getAlbum(){
返回相册;
}

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(){
返回长度;
}

public String toString(){
return Title: + getName()+,Artist: + getArtist()
+,专辑: + getAlbum()+,轨道长度: + formatTime(getLength());
}

}

public static String formatTime(long time){
long溢出=时间;
long h =时间/小时;
溢出=时间%HOURS;
long m =溢出/ MINS;
溢出=时间%分钟;
long s =溢出/ SECS;
return String.format(%02d:%02d。%02d,h,m,s);

}
}

此测试代码当前已打印

  NumSongs = 7 /播放列表歌曲限制= 12 
songList [0] =<标题:热线Bling,歌手: Drake,专辑:Hotline Bing-单曲,曲目长度:00:04.27>
songList [1] =<标题:您的意思是什么?,歌手:Justin Bieber,专辑:您的意思是什么? -单曲,曲目长度:00:03.27>
songList [2] =<标题:Watch Me,艺术家:Silento,专辑:Watch Me(Whip / Nae Nae)-单曲,曲目长度: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


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();
       }
    }   

解决方案

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

这篇关于播放列表和歌曲及驱动程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆