Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

You are burning some music CDs for a party. You\'ve arranged a list of songs in

ID: 641825 • Letter: Y

Question

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 7 minutes and 42 seconds to play, the data entered in the data file for that songs would be 462.

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. For example:

Song Song Time Total Time
Number Minutes Seconds Minutes Seconds
------- ------- ------- ------- -------
1 5 10 5 10
2 7 42 12 52
3 4 19 17 11
4 4 33 21 44
5 10 27 32 11
6 8 55 41 6
7 5 0 46 6

There are 58 minutes and 54 seconds of space left on the 100 minute CD.

Note that the output converts the input from seconds to minutes and seconds. Use meaningful variable names, proper indentation, and appropriate comments. Thoroughly test the program using your own data sets.

Explanation / Answer

#include <iostream>
#include <fstream>
#include <iomanip>
using namespace std;
int main()
{
int songMinutes = 0;
int songSeconds = 0;
int timeSeconds = 0;
int totalMinutes = 0;
int totalSeconds = 0;
int minutesLeft = 0;
int secondsLeft = 0;
int totalTime = 0;
int usedTime = 0;
int current = 0;
int currentMinutes = 0;
int currentSeconds = 0;
int usedMinutes;
int usedSeconds;
int seconds = 0;
int newtotalTime = 0;
int songNumber;
bool ok = true;
ifstream inData;
inData.open("f:\songs.dat");
if ( !inData )
{
cout << "Can't open the input file. Cancelling." << endl;
cin.get();
return 1;
}
  
songNumber = 1; //First record
//Create columns and heading
  
cout << setw(8) << "Song" << setw(28)
<< "Song Time" << setw(28)
<< "Total Time" << setw(25)
<< "Number" << setw(20)
<< "Minutes" << setw(10)
<< "Seconds" << setw(18)
<< "Minutes" << setw(10)
<< "Seconds" << setw(22)
<< "-------" << setw(20) << "-------" << setw(10)
<< "-------" << setw(18) << "-------" << setw(10)
<< "-------" << endl;
/*
cout << fixed << showpoint << setprecision(2) << setw(7)
<< songNumber << setw(18)
<< songMinutes << setw(10)
<< timeSeconds << setw(18)
<< totalMinutes << setw(10)
<< totalSeconds << setw(15)
<< endl;
*/
inData >> totalTime;
  
// songNumber = 2;
songNumber = 1;
while(songNumber <= 14) //Loop until current complete
{
inData >>current;
// if (currentSeconds > 59)
if (current > 59)
{
currentMinutes = (current / 60);
currentSeconds = (current % 60);   
}
timeSeconds = currentSeconds;
songMinutes = currentMinutes;
seconds = (songMinutes * 60);
usedMinutes = 0;
usedSeconds = 0;
/*
if (currentSeconds > 59)
{
currentMinutes = (current / 60);
currentSeconds = (current % 60);   
}
*/
totalMinutes = (usedMinutes + currentMinutes);
totalSeconds = (usedSeconds + currentSeconds);
  
cout << fixed << showpoint << setprecision(2) << setw(7)
<< songNumber << setw(18)
<< songMinutes << setw(10)
<< timeSeconds << setw(18)
<< totalMinutes << setw(10)
<< totalSeconds << setw(15) << endl;
  
usedTime = (totalMinutes * 60 + totalSeconds);
minutesLeft = (totalTime - usedTime) / 60;
// secondsLeft = (totalTime - minutesLeft) % 60;
secondsLeft = (totalTime - usedTime) % 60;
  
songNumber++;
}   
//Calculates the time on the cd, time used,
//and the time left on the cd
totalTime = (80 * 60);
//Output the minutes and seconds left on the CD
cout << "There are " << minutesLeft << " minutes and "
<< secondsLeft
<< " seconds left of space on the 80 minute CD." << endl;
  
inData.close();
cin.get();
cin.get();
return 0;
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote