Question

Radio station KJAVA wants a class to keep track of recordings it plays. Create a class...

Radio station KJAVA wants a class to keep track of recordings it plays. Create a class named Recording that contains fields to hold methods for setting and getting a Recording’s title, artist, and playing time in seconds. Save the file as Recording.java. b. Write an application that instantiates five Recording objects and prompts the user for values for the data fields. Then prompt the user to enter which field the Recordings should be sorted by—song title, artist, or playing time. Perform the requested sort procedure, and display the Recording objects. Save the file as RecordingSort.java.

Homework Answers

Answer #1

If you have any problem with the program feel free to comment.

Recording.java

public class Recording {
   //instance of the class
   private String title, artist;
   private int playtime;
  
   //getters and setters
   public String getTitle() {
       return title;
   }

   public void setTitle(String title) {
       this.title = title;
   }

   public String getArtist() {
       return artist;
   }

   public void setArtist(String artist) {
       this.artist = artist;
   }

   public int getPlaytime() {
       return playtime;
   }

   public void setPlaytime(int playtime) {
       this.playtime = playtime;
   }
  
}

RecordingSort.java

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Arrays;
import java.util.Comparator;

public class RecordingSort {

   public static void main(String[] args) throws IOException {
       // for taking console input
       BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

       Recording[] ar = new Recording[5];

       // taking user inputs
       for (int i = 0; i < 5; i++) {
           Recording obj = new Recording();

           System.out.print("Enter Title: ");
           obj.setTitle(br.readLine());
           System.out.print("Enter Artist: ");
           obj.setArtist(br.readLine());
           System.out.print("Enter Playtime: ");
           obj.setPlaytime(Integer.parseInt(br.readLine()));
           ar[i] = obj;// storing it into the array
           System.out.println();
       }
       // taking sortings choice from user
       System.out.println("1. Title");
       System.out.println("2. Artist");
       System.out.println("3. Playtime");
       System.out.print("Enter your sorting choice: ");
       int choice = Integer.parseInt(br.readLine());

       switch (choice) {
       //according to user choice perform sort using comparator
       case 1:
           Comparator<Recording> t = (i, j) -> {
               return i.getTitle().compareTo(j.getTitle());//sorting by title
           };
           Arrays.sort(ar, t);
           break;
       case 2:
           Comparator<Recording> a = (i, j) -> {
               return i.getArtist().compareTo(j.getArtist());//sorting by artist
           };
           Arrays.sort(ar, a);
           break;
       case 3:
           Comparator<Recording> p = (i, j) -> {
               return i.getPlaytime() < j.getPlaytime() ? -1 : 1;//sorting by playtime
           };
           Arrays.sort(ar, p);
           break;
       }
      
       //showing output
       System.out.println("List After Sort");
       for (Recording i : ar) {
           System.out.println(i.getTitle() + " " + i.getArtist() + " " + i.getPlaytime());
       }

   }

}

Output

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
Can someone please edit my code so that it satisfies the assignments' requirements? I pasted the...
Can someone please edit my code so that it satisfies the assignments' requirements? I pasted the codes below. Requirement: Goals for This Project:  Using class to model Abstract Data Type  OOP-Data Encapsulation You are asked to write an app to keep track of a relatively small music library. The app should load song information from a data file once the app is started. It should allow user to view, add, remove, and search for songs. The app should...
Using C# Create an Employee class with five fields: first name, last name, workID, yearStartedWked, and...
Using C# Create an Employee class with five fields: first name, last name, workID, yearStartedWked, and initSalary. It includes constructor(s) and properties to initialize values for all fields. Create an interface, SalaryCalculate, class that includes two functions: first,CalcYearWorked() function, it takes one parameter (currentyear) and calculates the number of year the worker has been working. The second function, CalcCurSalary() function that calculates the current year salary. Create a Worker classes that is derived from Employee and SalaryCalculate class. In Worker...
In the previous assessment, you used a static set of named variables to store the data...
In the previous assessment, you used a static set of named variables to store the data that was entered in a form to be output in a message. For this assessment, you will use the invitation.html file that you modified in the previous assessment to create a more effective process for entering information and creating invitations for volunteers. Rather than having to enter each volunteer and create an invitation one at a time, you will create a script that will...
Pandora is the Internet’s most successful subscription radio service. As of June 2013, it had over...
Pandora is the Internet’s most successful subscription radio service. As of June 2013, it had over 200 million registered users (140 million of which access the service via a mobile device) and over 70 million active listeners. Pandora now accounts for more than 70% of all Internet radio listening hours and a 7% share of total U.S. radio listening (both traditional and Internet). At Pandora, users select a genre of music based on a favorite musician, and a computer algorithm...