【新手问题】Class 之间的互相调用以及constructor的使用

javaleave 2012-09-19 09:41:09
现在要建一个MusicCollection class, 每个collection 需要至少三张专辑(3 albums), 我们需要显示出collection 并且允许用户去选择专辑特定设置。
要求:
1. 一个static Scanner object 用于整个class.
2. main method 需要以下功能:
a. 至少有三首歌(3 Songs), 每个版本的Song Constructors 至少用一次
b. 为每首歌创造出一个Albums(create an Album for each of the songs),每个版本的Album constructors 至少用一次
c. 有一个主循环需要做到以下步骤:
-打印显示出所有的专辑(Album)的数字排序(numbered ordering),需要用albums getter完成这个
-允许用户去选择那张专辑(Album)他想去使用,接着对那张专辑(Album) call albumOptions method
-这个循环必须终止如果用户输入0为专辑选择上(album selection)
-其他情况下继续这个循环
d.创建一个public static void albumOptions(Album variableName) {...}---->(题外话为什么一定是static?)
这个method 接收Album object, 可以随意定义它的名字,但必须有以下功能:
- 打印出专辑信息, 使用Album的 toString method
- 提示并且允许用户去挑选以下的选项:
>Get Favorite Track: 打印出歌曲(Song) 的信息(使用song's toString method)和favoriteTrack 在这个专辑中,包括track的数字
>Change genre:允许用户去改变Album 的genre. 这必须与歌(Song)的genre一起改变
>Return: 允许用户去返回到主循环
3.期间会使用到的shadowing variable, 有时需要用scanner.nextLine()

我写的Album.java 和Song.java 现在附上。对MusicCollection实在很头疼

Song.java

public class Song {
/*
* @parama title--songs' title
* @parama genre--songs' genre
* @parama artist--songs' artist
*/
private String title;
private String artist;
private String genre;

/*
* Constructs a song
* @param song is the song's title
* @param artist is the song's artist
* @param genre is the song's genre type
*/
public Song(String title, String artist, String genre){
this.title = title;
this.artist = artist;
this.genre = genre ;

}

public Song(String title, String artist){
String unknown= genre;
}

public String getTitle(){
return title;
}

public String getArtist(){
return artist;
}

public String getGenre(){
return genre;
}

public void setGenre(String genre){
this.genre=genre;
}

/*
* Return a String represent the song information
*
*/
public String toString(){
return title+"," + artist + ","+ genre;
}


}

Album.java

public class Album {
/*
* @param the title, artist, genre of album
* @param the list of favorite track number
*/

private String title;
private String artist;
private String genre;
private Song favoriteTrack;
private int trackNumber;
private static int numAlbums;
/*
* @param the album's number
*/
public static int numAlbum (int numAlbum){
return numAlbum;
}

/*Constructs an album
* @param title the title of album
* @param artis the artis of favorite track
* @param favoritetrack is a song's track
*/

public Album(String title, Song favoriteTrack, int trackNumber){
this.title= title;
this.favoriteTrack= favoriteTrack;
this.trackNumber= trackNumber;
artist = favoriteTrack.getArtist();
genre = favoriteTrack.getGenre();
int numAlbum = 0;
numAlbum ++;

}

public Album(String title, Song favoriteTrack){
this(title,favoriteTrack,1);
}

public String getTitle(){

return title;
}

public Song getfavoriteTrack(){
return favoriteTrack;
}

public int gettrackNumber(){
return trackNumber;
}

public void setGenre(String genre){

this.genre= genre;
}

/*
* Return a String represent the album
*
*/
public String toString(){
return "\""+title + "," + favoriteTrack.toString() +","+ trackNumber;
}

}

我写了一些MusicCollection.java,但大致的方向还是没找到。希望大家给予帮助!

import java.util.*;

public class MusicCollection {

private static String albumTitle;
private static int numAlbum;
public static Scanner{
Scanner scan = new Scanner(System.in);
}
public static void main(String[] args){
Album a = new Album("Beyond",new Song("I love Mars"), 4);
Album b = new Album("Flying", new Song("Who's care"), 2);
Album c = new Album("Complicated",new Song("Everybody"),5);

/*
* @param scan is an input parameter.
*/
Scanner scan = new Scanner(System.in);
albumTitle = Album.getTitle(scan.nextLine());
numAlbum = Album.numAlbum(scan.nextInt());
for (int =0; i < numAlbum; i ++){
Album =
}

}

public static void albumOpitions(Album variableName){
System.out.println(Album.toString());

}
}

谢谢!!请大家帮我看看这个MusicCollection余下该怎么写
...全文
323 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
K-2SO 2012-09-21
  • 打赏
  • 举报
回复

import java.util.Scanner;

public class Album {
private String title, artist, genre;
private Song favoriteTrack;
private int trackNumber;
static int numAlbums = 0;
public Album(String title, Song favoriteTrack, int trackNumber){
this.title = title;
artist = favoriteTrack.getArtist();
genre = favoriteTrack.getGenre();
this.favoriteTrack = favoriteTrack;
this.trackNumber = trackNumber;
numAlbums ++;
}
public Album(String title, Song favoriteTrack){
this.title = title;
artist = favoriteTrack.getArtist();
genre = favoriteTrack.getGenre();
this.favoriteTrack = favoriteTrack;
trackNumber = 1;
numAlbums ++;
}
public String getTitle(){
return title;
}
public String getFavoriteTrack(){
return favoriteTrack.toString();
}
public int getTrackNumber(){
return trackNumber;
}
public int getNumberAlbums(){
return numAlbums;
}
public void setGenre(String genre){
this.genre = genre;
favoriteTrack.setGenre(genre);
}
public void setTitle(String title){
this.title = title;
}
public void setArtist(String artist){
this.artist = artist;
favoriteTrack.setArtist(artist);
}
public String toString(){
return artist + " " + "-" + " " + title + "," + " " + "Genre:" + " " + genre;
}
}
K-2SO 2012-09-21
  • 打赏
  • 举报
回复

public class Song {
private String title, artist, genre;
public Song(String title, String artist, String genre){
this.title = title;
this.artist = artist;
this.genre = genre;
}
public Song(String title, String artist){
this.title = title;
this.artist = artist;
genre = "unknown";
}
public void setTitle(String title){
this.title = title;
}
public void setArtist(String artist){
this.artist = artist;
}
public void setGenre(String genre){
this.genre = genre;
}
public String getTitle(){
return title;
}
public String getArtist(){
return artist;
}
public String getGenre(){
return genre;
}
public String toString(){
return artist + " " + "-" + " " + title + "," + " " + "Genre:" + " " + genre;
}
}
K-2SO 2012-09-21
  • 打赏
  • 举报
回复
LOL
javaleave 2012-09-20
  • 打赏
  • 举报
回复
在线等啊。求帮助!!!
javaleave 2012-09-20
  • 打赏
  • 举报
回复
求助啊~大家帮我看看如何解这道题!谢谢了!

62,621

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧