a. Radio station JAVA wants a class to keep track of recordings it plays. Create
ID: 3844064 • Letter: A
Question
a. Radio station JAVA 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.Explanation / Answer
Recording,java
public class Recording {
//Declaring instance variables
private String title;
private String artist;
private int time;
//Zero argumented constructor
public Recording() {
super();
}
//Parameterized constructor
public Recording(String title, String artist, int time) {
super();
this.title = title;
this.artist = artist;
this.time = time;
}
//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 getTime() {
return time;
}
public void setTime(int time) {
this.time = time;
}
}
_________________
RecordingSort.java
import java.util.ArrayList;
import java.util.Collections;
import java.util.Scanner;
public class RecordingSort {
public static void main(String[] args) {
//Declaring variables
String title, artist;
int time;
// Scanner object is used to get the inputs entered by the user
Scanner sc = new Scanner(System.in);
//Creating An ArrayList which can hold Recording class Objects
ArrayList<Recording> arl = new ArrayList<Recording>();
/* Getting inputs entered by the user and create an Recording class Object
* and populate user entered values into each object and store in ArrayList
*/
for (int i = 0; i < 5; i++) {
//getting the info entered by the user
System.out.println("Recorsing#" + (i + 1) + ":");
System.out.print("Enter the Title :");
title = sc.nextLine();
System.out.print("Enter the name of the Artist :");
artist = sc.nextLine();
System.out.print("Enter the no of seconds :");
time = sc.nextInt();
arl.add(new Recording(title, artist, time));
sc.nextLine();
}
int choice;
/* This while loop continues to execute
* until the user enters a choice 4
*/
while (true) {
System.out.println("1.Sort by song Title.");
System.out.println("2.Sort by Artist.");
System.out.println("3.Sort by Playing Time.");
System.out.println("4.Exit.");
System.out.print("Enter Choice :");
choice = sc.nextInt();
switch (choice) {
case 1: {
Collections.sort(arl, new SortByTitle());
System.out
.println(" ___________Sorting based on Title __________");
for (Recording re : arl) {
System.out.println(re.getTitle() + ", " + re.getArtist()
+ ", " + re.getTime());
}
continue;
}
case 2: {
Collections.sort(arl, new SortByArtist());
System.out
.println(" ___________Sorting based on Artist __________");
for (Recording re : arl) {
System.out.println(re.getTitle() + ", " + re.getArtist()
+ ", " + re.getTime());
}
}
continue;
case 3: {
Collections.sort(arl, new SortByTime());
System.out
.println(" ___________Sorting based on Time __________");
for (Recording re : arl) {
System.out.println(re.getTitle() + ", " + re.getArtist()
+ ", " + re.getTime());
}
}
continue;
case 4: {
break;
}
default: {
System.out.println("** Invalid.Enter valid Input **");
continue;
}
}
break;
}
}
}
____________________
SortByArtist.java
import java.util.Comparator;
public class SortByArtist implements Comparator<Recording> {
@Override
public int compare(Recording re1, Recording re2) {
return re1.getArtist().compareTo(re2.getArtist());
}
}
_________________
SortByTime.java
import java.util.Comparator;
public class SortByTime implements Comparator<Recording> {
@Override
public int compare(Recording re1, Recording re2) {
int val= 0;
if(re1.getTime() < re2.getTime()){
val = -1;
}else if(re1.getTime() > re2.getTime()){
val = 1;
}else if(re2.getTime() == re2.getTime()){
val = 0;
}
return val;
}
}
_____________________
SortByTitle.java
import java.util.Comparator;
public class SortByTitle implements Comparator<Recording> {
@Override
public int compare(Recording re1, Recording re2) {
return re1.getTitle().compareTo(re2.getTitle());
}
}
____________________
Recorsing#1:
Enter the Title :Hard Rock
Enter the name of the Artist :Williams
Enter the no of seconds :678
Recorsing#2:
Enter the Title :Crazy Girls
Enter the name of the Artist :Lucia
Enter the no of seconds :456
Recorsing#3:
Enter the Title :Dancing Dolls
Enter the name of the Artist :Mary
Enter the no of seconds :668
Recorsing#4:
Enter the Title :Street Boys
Enter the name of the Artist :Kevin
Enter the no of seconds :453
Recorsing#5:
Enter the Title :Might Maniacs
Enter the name of the Artist :Robert
Enter the no of seconds :723
1.Sort by song Title.
2.Sort by Artist.
3.Sort by Playing Time.
4.Exit.
Enter Choice :1
___________Sorting based on Title __________
Crazy Girls, Lucia, 456
Dancing Dolls, Mary, 668
Hard Rock, Williams, 678
Might Maniacs, Robert, 723
Street Boys, Kevin, 453
1.Sort by song Title.
2.Sort by Artist.
3.Sort by Playing Time.
4.Exit.
Enter Choice :2
___________Sorting based on Artist __________
Street Boys, Kevin, 453
Crazy Girls, Lucia, 456
Dancing Dolls, Mary, 668
Might Maniacs, Robert, 723
Hard Rock, Williams, 678
1.Sort by song Title.
2.Sort by Artist.
3.Sort by Playing Time.
4.Exit.
Enter Choice :3
___________Sorting based on Time __________
Street Boys, Kevin, 453
Crazy Girls, Lucia, 456
Dancing Dolls, Mary, 668
Hard Rock, Williams, 678
Might Maniacs, Robert, 723
1.Sort by song Title.
2.Sort by Artist.
3.Sort by Playing Time.
4.Exit.
Enter Choice :4
_____________Could you rate me well.Plz .Thank You
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.