Professor C. is making some music CDs to use at a party. Each CD will contain no
ID: 3604188 • Letter: P
Question
Professor C. is making some music CDs to use at a party. Each CD will contain no more than 20 minutes worth of songs. He has made a list of songs that he wants to use and the order in which he wants to play them. Write a Java program that will tell him which songs will be on each CD and the total playing time of the CD. The first line of input from a file will contain a single integer n that indicates the number of songs she has on her list. Each of the following n lines will contain the name of a song followed by two integers, m and s, that indicate the length of the song in minutes m and seconds s. Each item will be separated by a space. For each CD, you will output CD #x, where x is the number of the CD, followed by the first and last song on the CD and the total number of minutes and seconds of songs on the CD in the format mm:ss for times 10 minutes or greater and m:ss for times less than 10 minutes. All information shall be printed on separate lines with a blank line after the information for each CD. Let the user enter the file name from the keyboard. Refer to the sample output below.
Sample File
21
Can't You Hear Me Knocking 3 45
Sunshine of Your Love 4 12
While My Guitar Gently Weeps 4 23
Riders on the Storm 4 12
Back in Black 5 12
Smoke on the Water 3 56
Dance the Night Away 3 23
The Best Is Yet to Come 3 46
The Way You Look Tonight 3 32
Luck Be a Lady 4 21 Bewitched 3 42
The Good Life 3 56
The Girl from Ipanema 3 43
Fly Me to the Moon 2 34
Summer Wind 3 32
Strangers in the Night 3 52
Call Me Irresponsible 4 3
Somethin' Stupid 5 2
My Kind of Town 5 42
It Was a Very Good Year 5 21
That's Life 13 21
Sample Run:
Enter the file name: songs.txt
CD #1
Can't You Hear Me Knocking
Riders on the Storm
16:32
CD #2
Back in Black
The Way You Look Tonight
19:49
CD #3
Luck Be a Lady
Fly Me to the Moon
18:16
CD #4
Summer Wind
Somethin' Stupid
16:29
CD #5
My Kind of Town
It Was a Very Good Year
11:03
CD #6
That's Life
That's Life
13:21
Name the program: CDMakerXX.java, where XX are your initials.
Explanation / Answer
Here is your program : -------------------->>>>>>>>>>>>>>>>>>>>>>>>>
import java.util.Scanner;
import java.io.BufferedReader;
//import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
class SongDisplay{
static String fname;
static String lname;
static int minutes;
static int seconds;
static int totalLen;
static int totalSec;
static int totalSong,cdNo=1;
public static void main(String[] args) {
try {
Scanner sc = new Scanner(System.in);
String fileName,line;
System.out.println("Enter The File Name : ");
fileName = sc.next();
FileReader fr=new FileReader(fileName);
BufferedReader br=new BufferedReader(fr);
line = br.readLine();
totalSong = Integer.parseInt(line);
minutes = 0;
seconds = 0;
totalLen = 0;
totalSec = 0;
while((line = br.readLine()) != null) {
String data[];
data = line.split(" ");
int len = data.length;
totalSec = seconds;
seconds = seconds+Integer.parseInt(data[len-1]);
if(totalLen == 0){
for(int i=0;i<len-2;i++){
fname = fname+data[i];
}
}
if(seconds >= 60){
seconds = seconds - 60;
minutes = minutes +1;
}
minutes = minutes+Integer.parseInt(data[len-2]);
if(totalLen+minutes <= 20){
totalLen = totalLen+minutes;
for(int i=0;i<len -2;i++){
lname = lname+data[i];
}
}
else{
System.out.println("CD #"+cdNo+" ");
System.out.println(fname);
System.out.println(lname);
System.out.println(totalLen+":"+totalSec);
totalSec = 0;
totalLen = 0;
}
}
}catch(Exception e){e.printStackTrace();}
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.