This is the version as of March 5th..
public class CDPlayerTest {
public static void main (String [] args) {
String beatles1 = new String(
"It's been a hard day's night, and I been working like a dog\n" +
"It's been a hard day's night, I should be sleeping like a log\n" +
"But when I get home to you I'll find the things that you do\n" +
"Will make me feel alright");
String beatles2 = new String(
"Ah, look at all the lonely people\n" +
"Ah, look at all the lonely people\n\n" +
"Eleanor Rigby picks up the rice in the church " +
"where a wedding has been\n" +
"Lives in a dream\n" +
"Waits at the window, wearing the face that she keeps " +
"in a jar by the door\n" +
"Who is it for?");
String beatles3 = new String(
"In the town where I was born\n" +
"Lived a man who sailed to sea\n" +
"And he told us of his life\n" +
"In the land of submarines");
String thedoors1 = new String(
"People are strange when you're a stranger\n" +
"Faces look ugly when you're alone\n" +
"Women seem wicked when you're unwanted\n" +
"Streets are uneven when you're down\n");
String thedoors2 = new String(
"Carry me Caravan take me away\n" +
"Take me to Portugal, take me to Spain\n" +
"Andalusia with fields full of grain\n" +
"I have to see you again and again\n" +
"Take me, Spanish Caravan\n" +
"Yes, I know you can");
String [] songs = new String[3];
songs[0] = beatles1;
songs[1] = beatles2;
songs[2] = beatles3;
String name = new String("The Beatles");
System.out.println(songs[0]);
CD beatles = new CD(name,songs);
System.out.println(songs[0]);
System.out.println(beatles);
songs = new String[2];
songs[0] = thedoors1;
songs[1] = thedoors2;
CD thedoors = new CD("The Doors",songs);
System.out.println(thedoors);
System.out.println(thedoors.getCDName());
System.out.println("The number of songs is " + thedoors.getNumSongs());
//System.out.println(thedoors.getSong(1));
CDPlayer myplayer = new CDPlayer();
myplayer.putCD(beatles);
System.out.println(myplayer);
myplayer.play(2);
System.out.println(myplayer);
myplayer.play(2);
System.out.println(myplayer);
myplayer.eject();
myplayer.putCD(thedoors);
myplayer.play(2);
}
}
class CD {
private String CDname;
private String [] CDsongs;
public CD(String newName, String [] newSongs) {
CDname = newName;
int size = newSongs.length;
CDsongs = new String[size];
for (int i = 0; i < size; i++) {
CDsongs[i] = newSongs[i];
}
}
public String toString() {
return CDname + ", " + CDsongs.length + " songs.";
}
// getCDName
public String getCDName() {
return CDname;
}
// getNumSong
public int getNumSongs() {
return CDsongs.length;
}
// getSong(int)
public String getSong(int i) {
// returns the song given a song number
// if i is larger than the last valid song
// index, the string "End of CD" gets returned
if (i < CDsongs.length)
return CDsongs [i];
else
return "End of CD";
}
}
class CDPlayer {
private CD cd; // null indicates that there is no cd
private int song_num; // the current song number
public CDPlayer() {
// do nothing -- default values are OK
}
public void play(int i) {
// plays the specified number of songs or until the end of
// the cd if there is not enough songs on the cd
// handle the shorter case first:
if (cd == null){
System.out.println("Please put in a cd");
return; // quit the method
}
// don't need 'else' since the 'if' case ends
// with 'return'
int x = i + song_num;
for (int j = song_num; (j < x) && (j < cd.getNumSongs()); j++){
System.out.println(cd.getSong(j));
++song_num;
}
if (song_num == cd.getNumSongs())
System.out.println("End of CD");
}
public String toString() {
// prints the cd name and the current song number.
// if there is no CD, prints the message
// "There is no CD in the player."
if (cd != null){
String s = new String(cd.getCDName() + " " + song_num);
return s;
}
else
return "There is no CD in the player.";
}
public void putCD(CD c) {
if (cd != null)
System.out.println("Please eject current CD first");
else
cd = c;
}
// the method returns the cd currently in the player and
// resets the player to the empty state.
// If no cd is in the player, the method returns null
public CD eject() {
// store the current cd value before setting cd to null
CD c = cd;
cd = null;
song_num = 0;
return c;
}
}
This is an example from CSci 1211 course.