(求助)The User class
(2) The User ClassImplement a class called User that represents a user of the Music Exchange Center
that logs in to download music. The class must have the following attributes:
• userName - a String indicating the user's name when on the system.
• online - a boolean indicating whether or not the user is on-line at the moment.
(An on-line user is able to download music and this user’s music is also made available to
other users who are logged on.)
• songList - an ArrayList<Song> containing all of the songs that this user has on his/her hard drive
to be made available to the Music Exchange Center community.
Create the following methods:
• A constructor that takes no parameters, and another constructor that takes the user name as a
parameter. When created, users are considered NOT to be online.
• A toString() method that shows the user's name, the number of files available in his/her song list
and whether or not he/she is currently on-line (e.g., BoppinBob: 14 songs (not online)).
• A method called addSong() which adds a given song to the user’s songList.
• A method called totalSongTime() that returns an integer indicating the total amount of time (in
seconds) that all of the user’s songs would require if played.
Test your code before continuing using this test program:
class UserTestProgram {
public static void main(String args[]) {
User discoStew, noNameFred, peterPunk;
// Create some users and give them some songs
discoStew = new User("Disco Stew");
discoStew.addSong(new Song("Hey Jude", "The Beatles", 4, 35));
discoStew.addSong(new Song("Barbie Girl", "Aqua", 3, 54));
discoStew.addSong(new Song("Only You Can Rock Me", "UFO", 4, 59));
System.out.println(discoStew); // should display Disco Stew: 3 songs (not online)
System.out.println(discoStew.totalSongTime()); // should display 808
noNameFred = new User();
System.out.println(noNameFred); // should display : 0 songs (not online)
System.out.println(noNameFred.totalSongTime()); // should display 0
peterPunk = new User("Peter Punk");
peterPunk.addSong(new Song("Bite My Arms Off", "Jaw", 4, 12));
peterPunk.addSong(new Song("Where's My Sweater", "The Knitters", 3, 41));
peterPunk.addSong(new Song("Is that My Toenail ?", "Clip", 4, 47));
peterPunk.addSong(new Song("Anvil Headache", "Clip", 4, 34));
peterPunk.addSong(new Song("My Hair is on Fire", "Jaw", 3, 55));
System.out.println(peterPunk); // should display Peter Punk: 5 songs (not online)
System.out.println(peterPunk.totalSongTime()); // should display 1269
peterPunk.online = true;
System.out.println(peterPunk); // should display Peter Punk: 5 songs (online)
}
}
----------------解决方案--------------------------------------------------------
简单看了一下,楼主是要大家帮忙编写一个与该类匹配的User class,主要功能为添加歌曲到歌曲列表和统计列表中歌
曲的时长,歌曲列表名由构造这个列表时候产生,我先给个思路,User 里有两个构造一个默认,一个实例化的时候会产生一首歌曲,默认都是不在线,在建一个Song类,构造方法可以记录歌曲基本参数。addSong()方法应该是一个list容器,用来存放歌曲,totalSongTime()方法会统计累加歌曲时间。有时间的话我会给你把代码写出来。你自己先想一下,写写看
----------------解决方案--------------------------------------------------------
简单的给你写了下,大概就这么些东西,不过你这个按道理说是要考虑总时间的进位的。不应该用string和int 去记录时间。还有,我没时间给你测,你自己拿了代码测测,有错自己改下,不懂再问。
import java.util.ArrayList;
public class User {
private String userName ="";
private ArrayList<Song> song;
public User(){}
public User(String userName){
this.userName=userName;
song = new ArrayList<Song>();
}
public void addSong(Song song){
this.song.add(song);
}
public String totalSongTime(){
int minute = 0;
int second = 0;
String sumTime = "";
for(int i =0;i<song.size();i++){
minute += Integer.parseInt((song.get(i).getDuration()).substring(0, 1));
second += Integer.parseInt((song.get(i).getDuration()).substring(2, 4));
}
minute += second/60;
second = second%60;
sumTime = ""+minute+","+second;
return sumTime;
}
}
public class Song {
private String title ="";
private String artist = "";
private String duration ="";
public Song(String title,String artist,String duration){
this.title = title;
this.artist = artist;
this.duration = duration;
}
public Song(){}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getArtist() {
return artist;
}
public void setArtist(String artist) {
this.artist = artist;
}
public String getDuration() {
return duration;
}
public void setDuration(String duration) {
this.duration = duration;
}
}
----------------解决方案--------------------------------------------------------
以下是引用流星雨在2009-11-2 17:50:47的发言:
简单的给你写了下,大概就这么些东西,不过你这个按道理说是要考虑总时间的进位的。不应该用string和int 去记录时间。还有,我没时间给你测,你自己拿了代码测测,有错自己改下,不懂再问。
import java.util.Array ...
非常感谢,我先测一下 简单的给你写了下,大概就这么些东西,不过你这个按道理说是要考虑总时间的进位的。不应该用string和int 去记录时间。还有,我没时间给你测,你自己拿了代码测测,有错自己改下,不懂再问。
import java.util.Array ...
----------------解决方案--------------------------------------------------------
记住,一定要多看,多想.我一般先给出思路,你可以按照我的思路去写,觉得有困难,可以再发出来讨论.我上班也比较忙,就是写程序也只是写出主要流程,细节恐怕也没时间去注意了.学编程最重要的是编程思想.不是死代码,切记!
----------------解决方案--------------------------------------------------------
最关键的那2个toString method我不会写啊,你帮我可以写一下吗
还有你的totalSongTime Method 我看不懂啊,也运行不了,我们是应该要return一个int数值才对
[ 本帖最后由 lyf3368 于 2009-11-7 05:20 编辑 ]
----------------解决方案--------------------------------------------------------
要int值转换一下就可以了,哎,,,有空再帮你写吧
----------------解决方案--------------------------------------------------------
toString 其实是JAVA里边有的方法,
你这里 method that shows the user's name, the number of files available in her song list
and whether or not she is currently on-line 是说,,无论他是不是在线.都可以显示他的用户名.如果仅仅只是这样可以写成
public string toSting(){
return this.userName;
}
如果你要把string类型改成int可以用 Integer.parseInt()方法来转换一下,还有其他很多的方法都可以转换数据类型.
我很多写代码就是在这个页面上直接手写的,你直接复制到你的编译器上有时候是不能编译的.
----------------解决方案--------------------------------------------------------