JAVA Programming Question WRITE IN JAVA You are burning some music CDs for a par
ID: 3663819 • Letter: J
Question
JAVA Programming Question
WRITE IN JAVA
You are burning some music CDs for a party. You've arranged a list of songs in the order in which you want to play them. However, you would like to maximize your use of space on the CD, which holds 80 minutes of music. So, you want to figure out the total time for a group of songs and see how well they fit. Write and design a java program to help you do this. The data are on the file songs.dat. The time is entered as seconds. For example, if a song takes 6 minutes and 30 seconds to play, the data entered in the data file for that songs would be 390. After all the data has been read, the application should print a message indicating the time remaining on the CD. The output should be in the form of a table with columns and headings written on a file.
Song Number Song Time Total Time
Minutes Seconds Minutes Seconds
__________________________________________
1 5 10 5 10
2 7 42 12 52
5 4 19 17 11
3 4 33 21 44
4 10 27 32 11
5 8 55 41 6
There are 38 minutes and 54 seconds of space left on the 80 minute CD
Explanation / Answer
package assignment;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Scanner;
public class CDMusicTime {
public static void main(String[] theArgs) {
String inFileName = "D://ravi/Cheg/songs.txt";
String outFileName = "D://ravi/Cheg/songsReport.txt";
String outputStr = "Song Number Song Time Total Time ";
outputStr += " Minutes Seconds Minutes Seconds ";
outputStr += "------------------------------------------------------- ";
try {
Scanner input = new Scanner(new File(inFileName));
int songno = 0;
int timeInsec = 0;
int totalMin = 0;
int totalSec = 0;
int min = 0;
int sec = 0;
while(input != null && input.hasNext()) {
songno = input.nextInt();
timeInsec = input.nextInt();
//input.nextLine();
//convert time to min:sec
min = timeInsec / 60;
sec = timeInsec % 60;
totalMin += min;
totalSec += sec;
outputStr += songno+" "+min+" "+sec+" "+totalMin+" "+totalSec+" ";
}
int remTime = 80 * 60 - (totalMin* 60+totalSec);
min = remTime / 60;
sec = remTime % 60;
outputStr +=("There are "+min+" minutes and "+sec+" seconds of space left on the 80 minute CD");
System.out.println(outputStr);
//Write to File
FileWriter writer = new FileWriter(new File(outFileName));
// Writes the content to the file
writer.write(outputStr);
writer.flush();
writer.close();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
---output---
Song Number Song Time Total Time
Minutes Seconds Minutes Seconds
-------------------------------------------------------
1 5 10 5 10
2 7 42 12 52
3 4 33 16 85
4 10 27 26 112
5 8 55 34 167
There are 43 minutes and 13 seconds of space left on the 80 minute CD
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.