tryclass PlayList
{
int numberOfSong=0;
int capacity;
String songArtistTitle;
PlayList[] list;
public PlayList(int capacityIn)
{
capacity=capacityIn;
list=new PlayList[capacity];
}
public PlayList(String songArtistTitleIn)
{
if (songArtistTitleIn.length() < 1)
{
System.err.println("bad song artist in constructor") ;
System.exit(1) ;
}
songArtistTitle=songArtistTitleIn;
}
public boolean addSong(String songArtistTitleIn)
{
songArtistTitle=songArtistTitleIn;
if(numberOfSong>=0 && numberOfSong<=(capacity-1))
{
list[numberOfSong]=new PlayList(songArtistTitle);
numberOfSong++;
return true;
}
else
return false;
}
public String pickSong()
{
int ran;
String str;
if(numberOfSong==0)
return null;
else
{
ran=(int)(Math.random()*numberOfSong);
str=list[ran].toString();
for(int j=ran;j<(numberOfSong-1);j++)
{
list[j]=list[j+1];
}
numberOfSong--;
return str;
}
}
public String toString()
{
return songArtistTitle;
}
public boolean isFull()
{
if(numberOfSong==capacity)
return true;
else
return false;
}
public boolean isEmpty()
{
if(numberOfSong==0)
return true;
else
return false;
}
}
public class PlayListTester
{
public static void main( String[] args )
{
PlayList hitList = new PlayList(10); if( hitList.isEmpty() )
System.out.println( "The play list is empty" ); hitList.addSong( "The Rolling Stones: Jumpin' Jack Flash" );
hitList.addSong( "Manic Street Preachers: Motorcycle Emptiness" );
hitList.addSong( "Manic Street Preachers: If You Tolerate This" );
hitList.addSong( "The Kinks: Waterloo Sunset" );
hitList.addSong( "The Pretenders: Don't Get Me Wrong" );
hitList.addSong( "Radiohead: Airbag" );
hitList.addSong( "Johny Cash: San Quentin" );
hitList.addSong( "Blur: Girls and Boys" );
hitList.addSong( "Pulp: Do You Remember The First Time" );
hitList.addSong( "The Verve: This is Music" ); System.out.println( "The play list contains..." );
System.out.println( hitList.toString() ); if( hitList.isFull() )
System.out.println( "The play list is full" ); if( ! hitList.addSong( "The Smiths: This Charming Man" ) )
System.out.println( "Couldn't add The Smiths track - full play list" ); while( ! hitList.isEmpty() )
System.out.println( "Next song to play is: " + hitList.pickSong() );
if( hitList.isEmpty() )
System.out.println( "The play list is empty" );
}
}
{
int numberOfSong=0;
int capacity;
String songArtistTitle;
PlayList[] list;
public PlayList(int capacityIn)
{
capacity=capacityIn;
list=new PlayList[capacity];
}
public PlayList(String songArtistTitleIn)
{
if (songArtistTitleIn.length() < 1)
{
System.err.println("bad song artist in constructor") ;
System.exit(1) ;
}
songArtistTitle=songArtistTitleIn;
}
public boolean addSong(String songArtistTitleIn)
{
songArtistTitle=songArtistTitleIn;
if(numberOfSong>=0 && numberOfSong<=(capacity-1))
{
list[numberOfSong]=new PlayList(songArtistTitle);
numberOfSong++;
return true;
}
else
return false;
}
public String pickSong()
{
int ran;
String str;
if(numberOfSong==0)
return null;
else
{
ran=(int)(Math.random()*numberOfSong);
str=list[ran].toString();
for(int j=ran;j<(numberOfSong-1);j++)
{
list[j]=list[j+1];
}
numberOfSong--;
return str;
}
}
public String toString()
{
return songArtistTitle;
}
public boolean isFull()
{
if(numberOfSong==capacity)
return true;
else
return false;
}
public boolean isEmpty()
{
if(numberOfSong==0)
return true;
else
return false;
}
}
public class PlayListTester
{
public static void main( String[] args )
{
PlayList hitList = new PlayList(10); if( hitList.isEmpty() )
System.out.println( "The play list is empty" ); hitList.addSong( "The Rolling Stones: Jumpin' Jack Flash" );
hitList.addSong( "Manic Street Preachers: Motorcycle Emptiness" );
hitList.addSong( "Manic Street Preachers: If You Tolerate This" );
hitList.addSong( "The Kinks: Waterloo Sunset" );
hitList.addSong( "The Pretenders: Don't Get Me Wrong" );
hitList.addSong( "Radiohead: Airbag" );
hitList.addSong( "Johny Cash: San Quentin" );
hitList.addSong( "Blur: Girls and Boys" );
hitList.addSong( "Pulp: Do You Remember The First Time" );
hitList.addSong( "The Verve: This is Music" ); System.out.println( "The play list contains..." );
System.out.println( hitList.toString() ); if( hitList.isFull() )
System.out.println( "The play list is full" ); if( ! hitList.addSong( "The Smiths: This Charming Man" ) )
System.out.println( "Couldn't add The Smiths track - full play list" ); while( ! hitList.isEmpty() )
System.out.println( "Next song to play is: " + hitList.pickSong() );
if( hitList.isEmpty() )
System.out.println( "The play list is empty" );
}
}
The play list is empty
The paly list contains...
The Verve:This is Music
Couldn't add The Smiths Track-full play list
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException
at PlayList.pickSong(PlsyList.java:54)
at PlayListTester.main(PlayListTester.java:36)
谢谢
PlayList[] list;
public PlayList(int capacityIn)
{
capacity=capacityIn;
list=new PlayList[capacity];
}
2.
for(int j=ran;j<(numberOfSong-1);j++)
{
list[j]=list[j+1];
}
numberOfSong--;
3. public boolean isFull()
{
if(numberOfSong==capacity)
return true;
else
return false;
}