这些是我的指示:
编写一个包含以下属性的Song类以及相应的get / set方法。请注意,这是一个非常简单的对象,并不能达到编写实际音乐播放器程序所需的完整程度(特别是因为该程序不知道如何阅读和播放歌曲而且不涉及MP3文件!)。< / p>
现在编写一个名为Playlist的类来管理您的歌曲对象。它应该具有以下属性。
名称 - 包含播放列表名称的字符串。这是因为你想要制作多个播放列表。播放列表的某些名称可能是“健身房”或“播客”,但用户可以在此处输入他们喜欢的任何内容。你应该为这个值写一个getter / setter。
至少应该有以下行为。
最后,写一个驱动程序类来测试它。创建播放列表对象并为其命名。然后,制作一些Song对象并将它们添加到播放列表对象中。打印集合,删除一些歌曲,然后再次打印该集合。尽力展示该计划的全部功能。这不需要是交互式驱动程序。对这部作业进行硬编码很好。
附加说明
从列表中删除歌曲时,无法在阵列中留下空白点。你将如何处理这个问题?不要使用ArrayList来管理您的歌曲,您必须使用数组。不要为此分配扩展ArrayList。我们稍后会这样做;-)如果你想这样做,你可以自由地编写额外的“助手”方法。我列出的方法是分配的最低要求。如果有帮助,您可以随意添加更多内容。
提交内容您的歌曲,播放列表和驱动程序类的.java文件。
这就是我所拥有的:
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 ;
}
}
驱动程序类
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();
}
}
我的问题是我的输出并不是我希望它出来的方式......
----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.
列表没有显示,一切都只是一个空,我不明白为什么。轨道长度不是hh:mm:ss,它只是零。我将歌曲的每个长度转换为毫秒,并在Driver类中将其设置为毫秒。我不明白为什么它一直在
上抛出nullpointerException if(songs[indexToRemove].getName().equals(name)) {
任何帮助都会被贬低!感谢。
更新 我删除了静态并提供了更改,但现在当我编译集合时,我得到的错误说非静态方法不能从静态上下文引用这就是为什么我把静态放在第一位,所以我不明白...
----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.
更新2 //跟踪长度
已更改:Song.java
public void setLength(int trackLength) {
length = trackLength;
}
已更改: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;
更新3 //仍然无效
> ----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();
}
}
驱动程序类:
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();
}
}
答案 0 :(得分:0)
当应该引用实例字段时,所有字段和getter都是静态的:
public static String getName() {
return name;
}
应该是:
public String getName(){
return name;
}
并且
public static String name;
public static String artist;
public static String album;
public static int length;
应该是:
private String name;
private String artist;
private String album;
private int length;
否则,就像我说的那样,当你的逻辑需要实例变量时,你指的是类变量。
答案 1 :(得分:0)
首先,摆脱对{class 1}}字段
的static
个引用
public class Song {
public static String name;
public static String artist;
public static String album;
public static int length;
这基本上意味着Song
的每个实例都具有彼此完全相同的值(最后应用的值)
接下来,在您的Song
构造函数中,实际将参数分配给您的字段...
public Song(String songName, String artistName, String albumName, int trackLength) {
this.name = name;
this.artist = artist;
this.album = album;
this.length = length;
}
在这种情况下,this.name = name;
只是将值分配回自身。相反,你想做更像......的事情。
public Song(String songName, String artistName, String albumName, int trackLength) {
this.name = songName;
this.artist = artistName;
this.album = albumName;
this.length = trackLength;
}
接下来,我将删除所有static
修饰符到您的方法...
public static String getArtist() {
应该是
public String getArtist() {
例如。
我强烈建议您重新使用其余代码并确保您没有犯同样的错误
但现在只有最后一件事了。轨道长度时间应该是hh:mm:ss格式,但它仍然以毫秒为单位
自从我需要这么做以后已经很长时间了,但在过去,我曾经使用过像......
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);
}
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);
}
}
此测试代码目前打印出来
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