Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class durationOfMusic {
- public static void main(String[] args) {
- // TODO Auto-generated method stub
- String song[] = {"Mamma Mia", "Tiger", "YMCA", "Pour Que Tu M'aimes Encore", "My Heart Will Go On", "I'm Gonna Be (500 Miles)", "I'm Alive", "As Good As New", "Hasta Mi Final", "Roar"}; //10 songs in the iPod so far
- String artist[] = {"Abba", "Abba", "Village People", "Il Divo", "Celine Dion", "The Proclaimers", "Celine Dion", "Abba", "Il Divo", "Katy Perry"}; //best artists so far
- int durationSec[] = {211, 175, 287, 235, 281, 219, 211, 206, 216, 224}; //the number of seconds each song takes
- int total = 0; //to hold the total number of seconds
- //the total duration of the whole iPod
- for(int i = 0; i<durationSec.length; i++)
- {
- total = total + durationSec[i];
- System.out.println("Your iPod contains music lasting for " + total + " sec. in total");
- }
- //now to see which song takes the longest
- //the songs are sorted in descending order
- for(int i = 0; i<durationSec.length; i++)
- {
- for(int j = 1; j<(durationSec.length-1); j++)
- {
- if(durationSec[j]>durationSec[j-1])
- {
- int temp1 = durationSec[j];
- durationSec[j] = durationSec[j-1];
- durationSec[j-1] = temp1;
- String temp2 = song[j];
- song[j] = song[j-1];
- song[j-1] = temp2;
- String temp3 = artist[j];
- artist[j] = artist[j-1];
- artist[j-1] = temp3;
- }
- }
- }
- for(int i = 0; i<durationSec.length; i++)
- System.out.println(song[i] + " by " + artist[i] + ", " + durationSec[i] + " sec.");
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement