In: Computer Science
Learning Outcomes
Using Java, maintain a collection of objects using an array.
Construct a class that contains an array as a private instance variable.
Construct methods with arrays as parameters and return values.
Use partially filled arrays to implement a class where objects can be dynamically added.
Implement searching and sorting algorithms.
Instructions
For this assignment you will be implementing an application that manages a music collection. The application will allow the user to add albums to the collection and create a playlist of songs from that collection. The collection, albums, and playlist should all be implemented using arrays.
The Song Class
The Song class has the following members:
Private instance variables for the title, artist, and length of the song in seconds.
A constructor that takes a title, artist, number of minutes, and number of seconds and creates a Song object. The length of the song is calculated by multiplying the number of minutes by 60 and adding the number of seconds.
Accessor methods for the title, artist, and length.
A method called display that displays a song in the following format:
Title - Artist (Minutes:Seconds)
To display the seconds as a two-digit number you can use the format code %02d. This will add a leading zero if the number of seconds is less than 10.
The Album Class
The Album class has the following members:
Private instance variables for the title, artist, and tracklist of the album. The tracklist should be an array of Song objects.
A constructor that takes a title, artist, and an array of Song objects and initializes the private instance variables. The tracklist should be initialized by creating a new array and copying all of the Song objects from the array passed as a parameter.
Accessor methods for the title and artist.
A method called getNumTracks that returns the number of tracks on the album.
A method called getTrack that takes a track number between 1 and the number of tracks, and returns the corresponding song. If the number provided is out of range, it returns null.
A method called comesBefore that takes another album as a parameter and returns true if the album the method was called on comes before the album passed as a parameter. The order is determined by the artist first, then the title. Use compareToIgnoreCase and equalsIgnoreCase to do the artist and title comparisons.
A method called displayAlbum that displays the title, artist, and number of tracks in the following format:
Title - Artist (NumTracks tracks)
If the number of tracks is one, it should display 1 track in the parentheses.
A method called displayTracklist that displays each track with the corresponding track number before it. Use the format code %2d to align the track numbers to the right.
The AlbumCollection Class
The AlbumCollection class has the following members:
A public named constant for the maximum number of albums allowed in a collection.
Private instance variables for the number of albums in the collection, and an array of Album objects.
A default constructor that initializes the number of albums to zero and the array to a new array that can store the maximum number of albums allowed.
An accessor function for the number of albums.
A boolean method called addAlbum that takes an Album as a parameter. If the collection is full it returns false. Otherwise, it stores the album in the next open spot in the array, and increments the number of albums.
A method called findAlbum that takes a title and artist as parameters and uses a sequential search to find and return the first album it finds with the given title and artist. If no album is found, it returns null.
A void method called sortAlbums that sorts all of the albums in the collection using selection sort, with the comesBefore method from the Album class determining the sorting order.
A void method called displayAlbums that displays all of the albums in the collection.
A void method called displaySongs that displays all of the albums in the collection along with their track lists.
The Playlist Class
The Playlist class has the following members:
A public named constant for the maximum number of songs allowed in a playlist.
Private instance variables for the number of songs in the playlist and an array of Song objects.
A default constructor that initializes the number of songs to zero and the array to a new array that can store the maximum number of songs allowed.
An accessor function for the number of songs.
A method called getLength that returns the total length in seconds of the songs on the playlist.
A boolean method called addSong that takes an Song as a parameter. If the playlist is full, it returns false. Otherwise, it stores the song in the next open spot in the array and increments the number of songs.
A void method called display that displays all of the songs in the playlist, followed by the total length of the playlist in minutes and seconds.
A void method called clear that empties out the playlist by setting the number of songs to 0. It does not have to go through the array and remove any songs.
The Main Class
The main class should be called YourlastnameProgram3 and has the following members:
A private static void method called displayMenu that has no parameters and displays the following menu:
Choose one of the following:
1. Add an album to the collection
2. Display the albums in the collection
3. Display the songs in the collection
4. Sort the albums in the collection
5. Add a song to the playlist
6. Display the playlist
7. Clear playlist
8. Exit the program
A private static method called getChoice that takes no arguments, reads an integer from the user, and returns it.
A private static method called createAlbum that does the following:
Prompt the user for an album title.
Prompt the user for an album artist.
Prompt the user for the number of tracks on the album until the user enters a value greater than zero.
Call the getTracklist method to prompt the user for the tracks on the album and get an array of Song objects.
Create a new Album object with the title, artist, and track list provided and return it.
A private static method called getTracklist that takes the number of tracks and the album artist as parameters and does the following:
Prompt the user for the title and length in minutes and seconds for each track.
Create a Song object for each track with the information provided and store it in an array of Song objects.
Return the array of Song objects.
A private static method called getAlbumFromCollection that takes an AlbumCollection as a parameter and does the following:
Prompt for the title and artist to search for.
Call the findAlbum method to find the album in the collection.
If the album is not found, display a message indicating that the album is not in the collection, and go back to step 1.
Otherwise return the album.
A private static method called getSongFromAlbum that takes an Album as a parameter and does the following:
Display the tracklist for the album.
Prompt the user to choose a track number.
Call the getTrack method to get the song with the given track number.
If the track number is not valid, go back to step 2.
Otherwise, return the song.
A main function that does the following:
Create an empty AlbumCollection and an empty Playlist
Display a welcome message.
Call displayMenu to display the menu.
Call getChoice to get the user’s choice.
If the choice is 1, call createAlbum to create a new album and add the album to the collection.
If the choice is 2, display the albums in the collection.
If the choice is 3, display the songs in the collection.
If the choice is 4, sort the albums in the collection.
If the choice is 5, call getAlbumFromCollection to select an album, call getSongFromAlbum to select a song, and add the song to the playlist.
If the choice is 6, display the playlist.
If the choice is 7, clear the playlist.
If the choice is 8, display a thank you message.
If the choice is any other value, display an error message.
Go back to step 3 unless the choice was 8.
import java.util.Scanner;
class Song{
private String title;
private String artist;
private int songLengthInSecs;
private int songMinutes;
private int songSeconds;
public Song(String title,String artist,int songMinutes,int songSeconds){
this.title=title;
this.artist=artist;
this.songMinutes=songMinutes;
this.songSeconds=songSeconds;
songLengthInSecs=songMinutes*60+songSeconds;
}
public String getTitle(){
return title;
}
public String getArtist(){
return artist;
}
public int getSongLengthInSecs(){
return songLengthInSecs;
}
public void diplaySong(){
System.out.println(title+"-"+artist+"("+songMinutes+":"+String.format("%02d",songSeconds));
}
}
class Album{
private String title;
private String artist;
private Song[] tracklist;
public Album(String title,String artist,Song[] tracklist){
this.title=title;
this.artist=artist;
this.tracklist=tracklist;
}
public String getArtist(){
return artist;
}
public String getTitle(){
return title;
}
public int getNumTracks(){
return tracklist.length;
}
public String getTrack(int trackNum){
if(trackNum>0&&trackNum<=tracklist.length){
String s=tracklist[trackNum].getTitle()+"-"+tracklist[trackNum].getArtist();
return s;
}
else{
return null;
}
}
// public comesBefore(){
// }
public void displayAlbum(){
System.out.println(title+"-"+artist+"("+getNumTracks()+" "+tracklist);
}
public void displayTrackList(){
for (int i=0;i<tracklist.length;i++){
System.out.println((i+1)+" "+tracklist[i]);
}
}
class AlbumCollection{
public int maxNoOfAlbums=15;
private int noOfAlbums;
private Album[] albumObj;
public AlbumCollection(){
noOfAlbums=0;
}
Album[] newarrayObj=new Album[noOfAlbums];
public int getNoOfAlbums(){
return noOfAlbums;
}
public boolean addAlbum(Album album){
if(noOfAlbums<maxNoOfAlbums){
newarrayObj[noOfAlbums]=album;
noOfAlbums++;
return true;
}
else{
return false;
}
}
public Album findAlbum(String title,String artist){
boolean b=true;
Album obj[]=new Album[1];
for (int i=0;i<newarrayObj.length;i++){
if(newarrayObj[i].getTitle().equals(title)&&newarrayObj[i].getArtist().equals(artist)){
b=false;
obj[0]=newarrayObj[i];
}
}
if(b){
return null;
}
else{
return obj[0];
}
}
public void sortAlbums(){
}
public void displayAlbums(){
for(int i=0;i<newarrayObj.length;i++){
System.out.println(newarrayObj[i]);
}
}
public void displaySongs(){
for(int i=0;i<albumObj.length;i++){
System.out.println(newarrayObj[i].getTitle()+"-"+newarrayObj[i].getArtist());
}
}
}
class Playlist{
public int maxSongs;
private int noOfSongs;
private Song[] songobj;
public Playlist(){
noOfSongs=0;
}
Song[] songsArray=new Song[maxSongs];
public int getNoOfSongs(){
return noOfSongs;
}
public int getSongLength(){
int len=0;
for(int i=0;i<songsArray.length;i++){
len=songsArray[i].getSongLengthInSecs();
}
return len;
}
public boolean addsong(Song song){
if(noOfSongs<maxSongs){
songsArray[noOfSongs]=song;
noOfSongs++;
return true;
}
else{
return false;
}
}
public void displayPlaylist(){
for(Object o:songsArray){
System.out.println(o);
}
}
public void clear(){
noOfSongs=0;
}
}
}
public class PrintMessage{
public static void main (String[] args) {
Scanner sc=new Scanner(System.in);
PrintMessage p=new PrintMessage();
int i=0;
while(i==0){
p.displayMenu();
int ch=p.getChoice();
if(ch==8){
break;
}
else if(ch==1){
p.createAlbum();
System.out.println("Enter the song length");
System.out.println("Minutes: ");
// int m=sc.ne
Song sObj=new Song();
}
}
}
private static void displayMenu(){
System.out.println("Choose one of the following");
System.out.println("1.Add an album to the collection");
System.out.println("2.Display the albums in the collection");
System.out.println("3.Display the songs in the collection");
System.out.println("4.Sort the albums in the collection");
System.out.println("5.Add a song to the playlist");
System.out.println("6.Display the playlist");
System.out.println("7.Clear playlist");
System.out.println("8.Exit the program");
}
public static int getChoice(){
Scanner sc=new Scanner(System.in);
int choice=sc.nextInt();
return choice;
}
private static void createAlbum(){
Scanner sc=new Scanner(System.in);
System.out.print("Enter the album title:");
String album=sc.nextLine();
System.out.print("Enter the album artist: ");
String artist=sc.nextLine();
int i=0;
while(i==0){
System.out.print("Enter the number of tracks: ");
int noOfTracks=sc.nextInt();
if(noOfTracks>0)
break;
else
System.out.println("Number of tracks should be greater than 0");
}
}
}