黑马程序员技术交流社区

标题: 这是一道笔试题(附有答案) [打印本页]

作者: gzp123    时间: 2015-4-28 23:40
标题: 这是一道笔试题(附有答案)
定义三个新类,分别名为Song、PlayList和MusicCollection ,  Song对象包含着关于特定歌曲的信息,比如歌曲名、艺术家,专辑、歌曲长度等,PlayList对象包含播放列表名称和一个歌曲的集合:MusicCollection对象包含播放列表集合。它包括一个名为Library的主播放列表,这个列表包含该集合中的所有歌曲。定义上述的三个类,并编写方法实现下列任务:
创建一个Song 对象,并设置其信息。
创建一个PlayList对象,并对播放列表添加和删除歌曲。如果一首新歌不在主列表中,那么将其添加进去。确保从主播放列表中删除一首歌时,也要从音乐集合中的其他播放列表中删除此歌曲。      
创建一个MusicCollection 对象,并对该集合添加和删除播放列表搜索并显示关于所有歌曲,播放列表或整个音乐集合的信息。   
确保所有你定义的类都不产生内存漏洞。
import java.util.Arrays;


public class TestDemo {
       
        public static void main(String[] args) {
                Song song = new Song();
                song.songName = "庐州月";
                song.author = "许嵩";
                song.album = "寻雾启示";
                song.time = 253;
                System.out.println("建立好了一首歌:"+song.toString());
               
                PlayList playList = new PlayList();
                // 定义播放列表的名称
                playList.playListName = "播放列表一";
                // 定义播放列表的长度
                playList.songs = new Song[0];
               
                // 创建对象
                MusicCollection mc = new MusicCollection();
                //创建主播放列表
                mc.Library = new PlayList();
                mc.Library.playListName = "Library";
                mc.Library.songs = new Song[0];
               
                // 其他播放列表中有一个播放列表,就是播放列表一
                mc.otherLibrary = new PlayList[1];
                mc.otherLibrary[0] = playList;
               
                PlayList.add(playList, song,mc.Library);
                PlayList.add(playList,new Song("清明雨上","许嵩","自定义",283),mc.Library);
                PlayList.add(playList,new Song("庐州月", "许嵩", "寻雾启示", 253),mc.Library);
                PlayList.delete(playList,"庐州月");
                PlayList.delete(playList,"舅舅家");
                PlayList.delete(playList,"清明雨上");
                PlayList.delete(playList,"庐州月");
               
                PlayList.showSongs(mc.Library.songs, "Library");
               
                PlayList.deletezhu(mc.Library, "庐州月", mc.otherLibrary);
               
                //PlayList.showSongs(mc.Library.songs, "Library");
                PlayList.showSongs(playList.songs, playList.playListName);
               
                PlayList.add(playList,new Song("清明雨上","许嵩","自定义",283),mc.Library);
                PlayList.showSongs(mc.Library.songs, "Library");
                PlayList.delete(playList,"清明雨上");
                PlayList.add(playList,new Song("庐州月", "许嵩", "寻雾启示", 253),mc.Library);
                PlayList.showSongs(playList.songs, playList.playListName);
                PlayList.showSongs(mc.Library.songs, "Library");
                PlayList.deletezhu(mc.Library, "庐州月", mc.otherLibrary);
                PlayList.deletezhu(mc.Library, "清明雨上", mc.otherLibrary);
                PlayList.showSongs(playList.songs, playList.playListName);
        }

}
//               
/**
定义一个歌曲类
*/
class Song {
        public String songName;// 歌曲名称
        public String author;// 艺术家
        public String album;// 专辑
        public long time;// 歌曲时长

        // 定义一个有参数构造方法
        public Song(String songName, String author, String album, long time) {
                this.songName = songName;
                this.author = author;
                this.album = album;
                this.time = time;
        }

        public String toString() {
                return songName + "-" + author + "-" + album + "-" + time;
        }

        // 一个无参数构造方法
        public Song() {
               
        }
}

/**
* 播放列表类
*                 * 播放列表名称
*                 * 当前播放列表中的歌曲集合
*/
class PlayList {
        //播放列表的名称
        public String  playListName;
       
        //当前播放列表的所有歌曲的集合
        public Song[] songs;
       
        public static void add(PlayList playList ,Song song ,PlayList Library) {
                int index = 0;
                for(int i = 0;i<playList.songs.length;i++){
                        if(song.songName==playList.songs[i].songName){
                                index = -1;
                        }
                }
                if(index==0){
                        Song[] newsongs = Arrays.copyOf(playList.songs,playList.songs.length + 1);
                        newsongs[newsongs.length - 1] = song;

                        playList.songs = newsongs;// 把新的数组赋值给播放列表一,想当于被播放列表一中添加歌曲
                        newsongs = null;// 回收垃圾

                        showSongs(playList.songs, playList.playListName);// 打印数组
                }
                else
                {
                        System.out.println(song.songName+"已在您的播放列表中,请查看。");
                }
                //同步到主列表
                add2library(Library, song);
        }
                               
        /**
         * 输出打印数组中的内容
         */
        public static void showSongs(Song[] songs, String b) {
                String name;
                if (b == "Library") {
                        name = "主播放列表";
                } else {
                        name = "播放列表一";
                }
                System.out.println(name + "中内容为:");
                System.out.println(Arrays.toString(songs));// 输出数组内容
        }
       
        // 添加歌曲到主列表的数组中
        public static void add2library(PlayList Library , Song song) {
                int index = 0;
                for(int i = 0;i<Library.songs.length;i++){
                        if(song.songName==Library.songs[i].songName){
                                index = -1;
                        }
                }
                if(index==0){
                        Song[] newsongs = Arrays.copyOf(Library.songs,Library.songs.length + 1);
                        newsongs[newsongs.length - 1] = song;

                        Library.songs = newsongs;// 把新的数组赋值给播放列表一,想当于被播放列表一中添加歌曲
                        newsongs = null;// 回收垃圾
                }
        }
       
        public static void delete(PlayList playList , String name) {
                if(playList.songs.length==0){
                        System.out.println("该播放列表已经没有歌曲了");
                        return;
                }
                if (name == null || "".equals(name)) {
                        System.out.println("你传递的歌曲名称为空,请检查");
                        return;
                }
                int index = -1;
                System.out.println(playList.songs.length);
                for (int i = 0; i < playList.songs.length; i++) {
                        // 如果集合中有和要删除的名称相同的歌曲
                        if (name.equals(playList.songs[i].songName)) {
                                index = i;
                                break;
                        }
                }
                // 如果标记不是-1,就有要删除的了
                if (index != -1) {
                        Song[] songs = new Song[playList.songs.length - 1];
                        for (int i = 0; i < songs.length; i++) {
                                if (i == index) {
                                        songs[i] = playList.songs[++i];
                                        break;
                                }
                                songs[i] = playList.songs[i];
                        }
                        // 新的数组赋值给旧的,相当于从旧的中删除了
                        playList.songs = songs;
                        songs = null;// 回收垃圾
                        System.out.println("删除:" + name + "成功");
                        showSongs(playList.songs, playList.playListName);
                } else {
                        System.out.println("您要删除的歌曲:" + name + "不存在");
                }
        }
       


        public static void deletezhu(PlayList playList , String name,PlayList arr []) {
                if(playList.songs.length==0){
                        System.out.println("该播放列表已经没有歌曲了");
                        return;
                }
                if (name == null || "".equals(name)) {
                        System.out.println("你传递的歌曲名称为空,请检查");
                        return;
                }
                int index = -1;
                System.out.println(playList.songs.length);
                for (int i = 0; i < playList.songs.length; i++) {
                        // 如果集合中有和要删除的名称相同的歌曲
                        if (name.equals(playList.songs[i].songName)) {
                                index = i;
                                break;
                        }
                }
                // 如果标记不是-1,就有要删除的了
                if (index != -1) {
                        Song[] songs = new Song[playList.songs.length - 1];
                        for (int i = 0; i < songs.length; i++) {
                                if (i == index) {
                                        songs[i] = playList.songs[++i];
                                        break;
                                }
                                songs[i] = playList.songs[i];
                        }
                        // 新的数组赋值给旧的,相当于从旧的中删除了
                        playList.songs = songs;
                        songs = null;// 回收垃圾
                        System.out.println("删除:" + name + "成功");
                        showSongs(playList.songs, playList.playListName);
                } else {
                        System.out.println("您要删除的歌曲:" + name + "不存在");
                }
                for(int i = 0;i<arr.length;i++){
                        PlayList.delete(arr[i], name);
                }
        }
       

}
/**
* 播放列表集合类,存放播放列表
*        
*
*/
class MusicCollection {
        //主播放列表
        public PlayList Library;
       
        //其他播放列表
        public PlayList [] otherLibrary;
       
}





作者: 王国库    时间: 2015-4-29 00:05
哪里的笔试题啊?
作者: 米江波    时间: 2015-4-29 00:06
好厉害  我还是初学者 明天才面向对象呢
作者: 只吃饭不洗碗    时间: 2015-4-29 13:54
太长啦   
作者: 冯林01    时间: 2015-4-29 15:57
面试题这么难吗?
作者: major2015    时间: 2015-4-29 16:02
add方法为什么要是静态的?
作者: xgd6612    时间: 2015-4-29 16:52
我也感觉这个面试题好疯狂
作者: hui1130    时间: 2015-4-29 19:50
视频面试题吗?
作者: hnnz    时间: 2015-4-29 21:15
真的是视频面试的题目吗?这么长,那得弄多久啊!!!
作者: 崔小可    时间: 2015-4-29 21:17
学习了  嘿嘿  
作者: 落魄逗比    时间: 2015-4-29 22:07
楼主去哪里找的?
作者: Zack    时间: 2015-4-29 22:11
这面试题略复杂啊
作者: thoris    时间: 2015-4-29 22:50
先mark  集合类还没有学
作者: hejinze    时间: 2015-4-29 22:56
路过。。顶
作者: 精湛学术    时间: 2015-4-29 23:28
哪里的面试题
作者: ljd19930325    时间: 2015-4-30 07:12
这是考就业班的笔试题,为啥我有一大半看不懂,别吓我
作者: LPStone    时间: 2015-4-30 07:53
mark一下 初学java 有空研究研究
作者: 宥美    时间: 2015-4-30 08:52
mark  学习学习  
作者: ye646682485    时间: 2015-4-30 09:23
谢谢分享
作者: 上善若水_Wx    时间: 2015-4-30 09:39
好难啊~~~~~~~~~~~~~~~




欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/) 黑马程序员IT技术论坛 X3.2