Question

In: Computer Science

Learning Outcomes Using Java, maintain a collection of objects using an array. Construct a class that...

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:

  1. Prompt the user for an album title.

  2. Prompt the user for an album artist.

  3. Prompt the user for the number of tracks on the album until the user enters a value greater than zero.

  4. Call the getTracklist method to prompt the user for the tracks on the album and get an array of Song objects.

  5. 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:

  1. Prompt the user for the title and length in minutes and seconds for each track.

  2. Create a Song object for each track with the information provided and store it in an array of Song objects.

  3. Return the array of Song objects.

  • A private static method called getAlbumFromCollection that takes an AlbumCollection as a parameter and does the following:

  1. Prompt for the title and artist to search for.

  2. Call the findAlbum method to find the album in the collection.

  3. If the album is not found, display a message indicating that the album is not in the collection, and go back to step 1.

  4. Otherwise return the album.

  • A private static method called getSongFromAlbum that takes an Album as a parameter and does the following:

  1. Display the tracklist for the album.

  2. Prompt the user to choose a track number.

  3. Call the getTrack method to get the song with the given track number.

  4. If the track number is not valid, go back to step 2.

  5. Otherwise, return the song.

  • A main function that does the following:

  1. Create an empty AlbumCollection and an empty Playlist

  2. Display a welcome message.

  3. Call displayMenu to display the menu.

  4. Call getChoice to get the user’s choice.

  5. If the choice is 1, call createAlbum to create a new album and add the album to the collection.

  6. If the choice is 2, display the albums in the collection.

  7. If the choice is 3, display the songs in the collection.

  8. If the choice is 4, sort the albums in the collection.

  9. If the choice is 5, call getAlbumFromCollection to select an album, call getSongFromAlbum to select a song, and add the song to the playlist.

  10. If the choice is 6, display the playlist.

  11. If the choice is 7, clear the playlist.

  12. If the choice is 8, display a thank you message.

  13. If the choice is any other value, display an error message.

  14. Go back to step 3 unless the choice was 8.

Solutions

Expert Solution

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");
        }
    }
}

Related Solutions

Give an example of how to build an array of objects of a super class with...
Give an example of how to build an array of objects of a super class with its subclass objects. As well as, use an enhanced for-loop to traverse through the array while calling a static method(superclass x). Finally, create a static method for the class that has the parent class reference variable.
Using Java Summary Create a Loan class, instantiate and write several Loan objects to a file,...
Using Java Summary Create a Loan class, instantiate and write several Loan objects to a file, read them back in, and format a report. Project Description You’ll read and write files containing objects of the Loan class. Here are the details of that class: Instance Variables: customer name (String) annual interest percentage (double) number of years (int) loan amount (double) loan date (String) monthly payment (double) total payments (double) Methods: getters for all instance variables setters for all instance variables...
java Objective: Create a class. Create objects. Use methods of a class. Create a class BankAccount...
java Objective: Create a class. Create objects. Use methods of a class. Create a class BankAccount to represent a bank account according to the following requirements: A bank account has three attributes: accountnumber, balance and customer name. Add a constructor without parameters. In the initialization of the attributes, set the number and the balance to zero and the customer name to an empty string. Add a constructor with three parameters to initialize all the attributes by specific values. Add a...
JAVA: Provide two different implementations, an array and a linked list, to maintain a list of...
JAVA: Provide two different implementations, an array and a linked list, to maintain a list of names (two separate programs).The following operations are available: insert rear, insert front, remove a particular element, and print the whole list. Do not implement an ADT(Do not use a class with data and operations) Just set up a fixed size array or a linked list of nodes in main and provide code in main or functions/static methods to perform insert, remove, and print. You...
Java Proect Project Outcomes Develop a Java program that: • creates a user designed class •...
Java Proect Project Outcomes Develop a Java program that: • creates a user designed class • uses proper design techniques including reading UML Class Diagrams • reads input from the keyboard using a Scanner Object and its methods • uses selection (if and if else) statements • uses iteration (while, do while or for) statements • uses String comparison methods. • follows standard acceptable programming practices. • handles integer overflow errors Prep Readings: Zybooks chapter 1 - 6 1. General...
Java the goal is to create a list class that uses an array to implement the...
Java the goal is to create a list class that uses an array to implement the interface below. I'm having trouble figuring out the remove(T element) and set(int index, T element). I haven't added any custom methods other than a simple expand method that doubles the size by 2. I would prefer it if you did not use any other custom methods. Please use Java Generics, Thank you. import java.util.*; /** * Interface for an Iterable, Indexed, Unsorted List ADT....
Implement a Binary tree using an array using class.
Implement a Binary tree using an array using class.
2-Dimensional Array Operations. Use a Java class with a main method. In this class (after the...
2-Dimensional Array Operations. Use a Java class with a main method. In this class (after the main method), define and implement the following methods: public static void printArray. This method accepts a two-dimensional double array as its argument and prints all the values of the array, separated by a comma, each row in a separate line. public static double getAverage. This method accepts a two-dimensional double array as its argument and returns the average of all the values in the...
Learning Outcomes Implement a dynamically resizable array and analyze its performance. Instructions Our last program was...
Learning Outcomes Implement a dynamically resizable array and analyze its performance. Instructions Our last program was to maintain a list of Planets in the STL’s vector class. In this assignment, you will be writing your own vector class. Implement class vector consisting of an array of a generic type. This class will require the following fields: mData: An array of items of a generic type. mCapacity: An integer representing the capacity of the vector, which is the number of items...
please use C++ Create a class named Byte that encapsulates an array of 8 Bit objects....
please use C++ Create a class named Byte that encapsulates an array of 8 Bit objects. The Byte class will provide the following member functions: Byte - word: Bit[8] static Bit array defaults to all Bits false + BITS_PER_BYTE: Integer16 Size of a byte constant in Bits; 8 + Byte() default constructor + Byte(Byte) copy constructor + set(Integer): void sets Bit to true + clear(): void sets to 0 + load(Byte): void sets Byte to the passed Byte + read():...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT