JAVA Programming Question WRITE IN JAVA You are burning some music CDs for a par
ID: 3663822 • 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
Program:
import java.util.ArrayList;
import java.util.Random;
public class MusicOrganizer
{
private ArrayList<Track> tracks;
private MusicPlayer player;
private TrackReader reader;
private Random randomtrack;
public MusicOrganizer()
{
tracks = new ArrayList<Track>();
player = new MusicPlayer();
reader = new TrackReader();
randomtrack = new Random();
readLibrary("audio");
System.out.println("Music library loaded. " + getNumberOfTracks() + " tracks.");
System.out.println();
}
public void addFile(String filename)
{
tracks.add(new Track(filename));
}
public void addTrack(Track track)
{
tracks.add(track);
}
public void playTrack(int index)
{
if(indexValid(index))
{
Track track = tracks.get(index);
player.startPlaying(track.getFilename());
System.out.println("Now playing: " + track.getArtist() + " - " + track.getTitle());
}
}
public void randomTrack(int index)
{
int trackNumber = getNumberOfTracks();
int rindex = randomtrack.nextInt(trackNumber);
if(indexValid(index))
{
Track track = tracks.get(rindex);
player.startPlaying(track.getFilename());
System.out.println("Now playing: " + track.getArtist() + " - " + track.getTitle());
}
}
public void randomAllTracks(int index)
{
int trackNumber = getNumberOfTracks();
int rindex = randomtrack.nextInt(trackNumber);
if(indexValid(index))
{
for(Track track : tracks)
{
player.startPlaying(track.getFilename());
System.out.println("Now playing: " + track.getArtist() + " - " + track.getTitle());
System.out.println();
}
}
}
public int getNumberOfTracks()
{
return tracks.size();
}
public void listTrack(int index)
{
System.out.print("Track " + index + ": ");
Track track = tracks.get(index);
System.out.println(track.getDetails());
}
public void listAllTracks()
{
System.out.println("Track listing: ");
for(Track track : tracks)
{
System.out.println(track.getDetails());
}
System.out.println();
}
public void listByArtist(String artist)
{
for(Track track : tracks)
{
if(track.getArtist().contains(artist))
{
System.out.println(track.getDetails());
}
}
}
public void removeTrack(int index)
{
if(indexValid(index))
{
tracks.remove(index);
}
}
public void playFirst()
{
if(tracks.size() > 0)
{
player.startPlaying(tracks.get(0).getFilename());
}
}
public void stopPlaying()
{
player.stop();
}
private boolean indexValid(int index)
{
boolean valid;
if(index < 0)
{
System.out.println("Index cannot be negative: " + index);
valid = false;
}
else if(index >= tracks.size())
{
System.out.println("Index is too large: " + index);
valid = false;
}
else
{
valid = true;
}
return valid;
}
private void readLibrary(String folderName)
{
ArrayList<Track> tempTracks = reader.readTracks(folderName, ".mp3");
for(Track track : tempTracks)
{
addTrack(track);
}
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.