..3aRadio station JAVA wants a class to keep track of recordings it plays. Creat
ID: 674415 • Letter: #
Question
..3aRadio 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.
Please explain each step. I am a beginner so please write program in beginner fashion. Thanks!
I know this question has been answered before but the answer does not prompt the user
Explanation / Answer
1. Recording.java
package com.rec;
public class Recording {
String recordTitle,recordArtist;
int recordTime;
/**
* @return the recordTitle
*/
public String getRecordTitle() {
return recordTitle;
}
/**
* @return the recordArtist
*/
public String getRecordArtist() {
return recordArtist;
}
/**
* @return the recordTime
*/
public int getRecordTime() {
return recordTime;
}
/**
* @param recordTitle the recordTitle to set
*/
public void setRecordTitle(String recordTitle) {
this.recordTitle = recordTitle;
}
/**
* @param recordArtist the recordArtist to set
*/
public void setRecordArtist(String recordArtist) {
this.recordArtist = recordArtist;
}
/**
* @param recordTime the recordTime to set
*/
public void setRecordTime(int recordTime) {
this.recordTime = recordTime;
}
/* (non-Javadoc)
* @see java.lang.Object#toString()
*/
@Override
public String toString() {
return "Recording [recordTitle=" + recordTitle + ", recordArtist="
+ recordArtist + ", recordTime=" + recordTime + "]";
}
}
2.ArtistComparator.java
package com.rec;
import java.util.Comparator;
public class ArtistComparator implements Comparator<Recording>{
public int compare(Recording rec1, Recording rec2) {
// TODO Auto-generated method stub
return rec1.getRecordArtist().compareTo(rec2.getRecordArtist());
}
}
3. TitleComparator.java
package com.rec;
import java.util.Comparator;
public class TitleComparator implements Comparator<Recording>{
public int compare(Recording rec1, Recording rec2) {
// TODO Auto-generated method stub
return rec1.getRecordTitle().compareTo(rec2.getRecordTitle());
}
}
4. TimeComparator.java
package com.rec;
import java.util.Comparator;
public class TimeComparator implements Comparator<Recording> {
@Override
public int compare(Recording rec1, Recording rec2) {
if(rec1.getRecordTime()==rec2.getRecordTime())
return 0;
else if(rec1.getRecordTime()>rec2.getRecordTime())
return 1;
else
return -1;
}
}
5. RecordingSort.java
package com.rec;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;
import java.util.Scanner;
public class RecordingSort {
public static void main(String args[]) {
List<Recording> recordings = new ArrayList<Recording>();
// reading five records
Scanner scanner = new Scanner(System.in);
for (int i = 1; i <= 5; i++) {
Recording recording = new Recording();
System.out.print("Enter the Recording’s Title :");
recording.setRecordTitle(scanner.nextLine());
System.out.print("Enter the Recording’s Artist :");
recording.setRecordArtist(scanner.nextLine());
System.out.print("Enter the playing time in seconds :");
recording.setRecordTime(Integer.parseInt(scanner.nextLine()));
recordings.add(recording);
}
System.out.print("Enter the Sorting Order :");
String sorting=scanner.nextLine();
if(sorting.equalsIgnoreCase("Title")){
Collections.sort(recordings, new TitleComparator());
}else if(sorting.equalsIgnoreCase("Artist")){
Collections.sort(recordings, new ArtistComparator());
}else {
Collections.sort(recordings, new TimeComparator());
}
Iterator<Recording> recIt=recordings.iterator();
System.out.println("Sorted by "+sorting);
System.out.println("Recording’s Title Recording’s Artist Recording’s Time");
while(recIt.hasNext()){
Recording recording=recIt.next();
System.out.println(recording.getRecordTitle()+" "+recording.getRecordArtist()+" "+recording.getRecordTime());
}
}
}
Output :
Enter the Recording’s Title :Rect1 tit
Enter the Recording’s Artist :Rect1 art
Enter the playing time in seconds :45
Enter the Recording’s Title :Rect2 tit
Enter the Recording’s Artist :Rect2 art
Enter the playing time in seconds :58
Enter the Recording’s Title :Rect3 tit
Enter the Recording’s Artist :Rect3 art
Enter the playing time in seconds :72
Enter the Recording’s Title :Rect4 tit
Enter the Recording’s Artist :Rect4 art
Enter the playing time in seconds :25
Enter the Recording’s Title :Rect5 tit
Enter the Recording’s Artist :Rect5 art
Enter the playing time in seconds :95
Enter the Sorting Order :time
Sorted by time
Recording’s Title Recording’s Artist Recording’s Time
Rect4 tit Rect4 art 25
Rect1 tit Rect1 art 45
Rect2 tit Rect2 art 58
Rect3 tit Rect3 art 72
Rect5 tit Rect5 art 95
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.