Improve company productivity with a Business Account.Sign Up

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 795
  • Last Modified:

Simple Java Playlist

Hello

I am trying vain to answer this question. Can someone please help me.

How do I get the user to enter the details of each song in the playlist.

Write a program that asks the user to enter the name of a playlist, and the
number of songs in the playlist. The program will then prompt the user to
enter the duration of each song in minutes and seconds. On completion, the
program should display a report that shows the name of the playlist, the
total number of tracks on the playlist, and the total duration of the playlist
in hours, minutes and seconds.

import java.util.Scanner; //import scanner class

public class Playlist
{
	public static void main (String [] args)
	{
		Scanner kb = new Scanner(System.in);
		
		String name;
		int numSongs = 0;
		int minutes = 0;
		int seconds = 0;
		int totalHrs = 0;
		int totalMins = 0;
		int totalSecs = 0;
		
		//enter name of playlist
		System.out.println("Please enter the name of your playlist");
		name = kb.nextLine();
		System.out.println("The name of your playlist is " +name);
		
		System.out.println();// new line
		
		//enter number of songs on the playlist
		System.out.println("Please enter the number of songs in your playlist?");
		numSongs = kb.nextInt();
		System.out.println("Number of songs you have chosen for your playlist is " +numSongs);
		
		System.out.println();// new line
	
		for(int i = 1; i <=numSongs; i++)
		{
		  System.out.println (i);
		
		
		System.out.println();// new line
		
		//enter the duration of each song in mins and seconds
		System.out.println("Please duration of song " + i + " in minutes");
		minutes = kb.nextInt();
		System.out.println("Please duration of song " + i + " in seconds?");
		seconds = kb.nextInt();
		System.out.println("Minutes: " +minutes);
		System.out.println("Seconds:" +seconds);
		}
	
		System.out.println();//new line
		
	  /*display a report that shows
	   1) The name of the Playlist
		2) The total number of tracks on the playlist
		3) The total duration of the playlist in hrs, mins and secs*/
	
		System.out.println ("Playlist : " + name);
		System.out.println ("No of tracks : " + numSongs);
		System.out.println ("Total Playing time : " + totalMins);

		
	}

}

Open in new window

0
Kevin_Coors
Asked:
Kevin_Coors
  • 6
  • 5
  • 3
  • +2
2 Solutions
 
momi_sabagCommented:
I find it hard to believe users will enter that information
Why don't you get it from the file itself?
0
 
Kevin_CoorsAuthor Commented:
Hello

I have only starting learning Java. It's a question I got from a Java book.

I am only making the playlist at random.

Thanks
0
 
krakatoaCommented:
Similar handling of the minutes and seconds as once before :

import java.util.Scanner; //import scanner class

public class Playlist
{
	public static void main (String [] args)
	{
		Scanner kb = new Scanner(System.in);
		
		String name;
		int numSongs = 0;
		int minutes = 0;
		int seconds = 0;
		int totalHrs = 0;
		int totalMins = 0;
		int totalSecs = 0;
		int numSecs = 0;
		
		//enter name of playlist
		System.out.println("Please enter the name of your playlist");
		name = kb.nextLine();
		System.out.println("The name of your playlist is " +name);
		
		System.out.println();// new line
		
		//enter number of songs on the playlist
		System.out.println("Please enter the number of songs in your playlist?");
		numSongs = kb.nextInt();
		System.out.println("Number of songs you have chosen for your playlist is " +numSongs);
		
		System.out.println();// new line
	
		for(int i = 1; i <=numSongs; i++)
		{
		  System.out.println (i);
		
		
		System.out.println();// new line
		
		//enter the duration of each song in mins and seconds
		System.out.println("Please duration of song " + i + " in minutes");
		minutes = kb.nextInt();

		System.out.println("Please duration of song " + i + " in seconds?");
		seconds = kb.nextInt();
		System.out.println("Minutes: " +minutes);
		System.out.println("Seconds:" +seconds);
		totalMins =totalMins+minutes;
		totalSecs +=seconds;
		
		}
	
		System.out.println();//new line
		
	  /*display a report that shows
	   1) The name of the Playlist
		2) The total number of tracks on the playlist
		3) The total duration of the playlist in hrs, mins and secs*/

		numSecs = totalSecs % 60;
		totalSecs = totalSecs - numSecs;
		totalMins = totalMins + (totalSecs/60);
		

	
		System.out.println ("Playlist : " + name);
		System.out.println ("No of tracks : " + numSongs);
		System.out.println ("Total Playing time : " + totalMins+" mins "+numSecs+" secs.");

		
	}

}

Open in new window

0
Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
yatsCommented:
Are you trying to print duration of each song as well??
0
 
zzynxSoftware engineerCommented:
You should indeed first total the minutes and seconds for each song like krakatoa showed.
But then, since you need the duration of the playlist in hours, minutes and seconds you need this calculations:

                
        totalSecs = totalSecs + totalMins*60;
        totalHrs = totalSecs / 3600;
        totalMins = (totalSecs % 3600) / 60;
        totalSecs = (totalSecs % 3600) % 60;


        System.out.println ("Playlist : " + name);
        System.out.println ("No of tracks : " + numSongs);
        System.out.println ("Total Playing time : " + totalHrs + " hr(s), " + totalMins+" min(s) and "+totalSecs+" second(s).");

Open in new window


So, the complete Playlist class looks like this:
import java.util.Scanner; //import scanner class

public class PlayList
{
    public static void main (String [] args)
    {
        Scanner kb = new Scanner(System.in);

        String name;
        int numSongs = 0;
        int minutes = 0;
        int seconds = 0;
        int totalHrs = 0;
        int totalMins = 0;
        int totalSecs = 0;
        int numSecs = 0;

        //enter name of playlist
        System.out.println("Please enter the name of your playlist");
        name = kb.nextLine();
        System.out.println("The name of your playlist is " +name);

        System.out.println();// new line

        //enter number of songs on the playlist
        System.out.println("Please enter the number of songs in your playlist?");
        numSongs = kb.nextInt();
        System.out.println("Number of songs you have chosen for your playlist is " +numSongs);

        System.out.println();// new line

        for(int i = 1; i <=numSongs; i++)
        {
            System.out.println (i);


            System.out.println();// new line

            //enter the duration of each song in mins and seconds
            System.out.println("Please duration of song " + i + " in minutes");
            minutes = kb.nextInt();

            System.out.println("Please duration of song " + i + " in seconds?");
            seconds = kb.nextInt();
            System.out.println("Minutes: " +minutes);
            System.out.println("Seconds:" +seconds);
            totalMins =totalMins+minutes;
            totalSecs +=seconds;

        }

        System.out.println();//new line

        /*display a report that shows
         1) The name of the Playlist
          2) The total number of tracks on the playlist
          3) The total duration of the playlist in hrs, mins and secs*/

        totalSecs = totalSecs + totalMins*60;
        totalHrs = totalSecs / 3600;
        totalMins = (totalSecs % 3600) / 60;
        totalSecs = (totalSecs % 3600) % 60;

        System.out.println ("Playlist : " + name);
        System.out.println ("No of tracks : " + numSongs);
        System.out.println ("Total Playing time : " + totalHrs + " hr(s), " + totalMins+" min(s) and "+totalSecs+" second(s).");
    }
}

Open in new window

0
 
krakatoaCommented:
Ha - I only read the last sysout delivering the minutes. Teach me to read the whole thing. ;) Well spotted zzynx.
0
 
Kevin_CoorsAuthor Commented:
Hi Guys

Thanks a lot for your help.

I am going to award the points as follows

zzynx 300 points

Krakatoa 200 points

I hope this is ok?
0
 
zzynxSoftware engineerCommented:
>> Well spotted zzynx.
:-)

>> I hope this is ok?
Seems reasonable to me.
0
 
krakatoaCommented:
And just to think . . . screaming . . .  *I could have had it all, I could have had it all*!

Yeah's fine.
;)
0
 
Kevin_CoorsAuthor Commented:
Thanks :)
0
 
zzynxSoftware engineerCommented:
Thanx 4 axxepting
0
 
krakatoaCommented:
zzynx : I wondered if you might be up for answering a question from me? I'm posting this here in an old question, ahead of posting a new one, to see what you think first. I have a pile of code which is causing some distress - can't figure out what is probably a logical bug on my part. If you think you can lend an ear, pls advise. ;) k. (PS couple of other EE dudes have chipped in previously, but it's still not right. There are only a handful of others on the forum who have a chance of understanding the issue !)
0
 
zzynxSoftware engineerCommented:
Hi krakatoa,
Sure. Post your code in a new question (and post the link here) and I'll see what I can do.
0
 
krakatoaCommented:
zzynx, thanks.

The code (4 classes) is already in a Q here on EE, and so rather than bring too much attention to it in a new Q, I will post the link to that code here, and simply open another Q entitled "UDP management", in which I will ask a general question. The underlying real question is this : why, with 2 peers in a voice conversation, is the quality fine and the conversation understandable, but then as soon as a third peer joins in, the signals become broken and unintelligible. When the 3rd peer leaves again, the quality is restored.

I can't believe this is a scaling problem, but it might be a logical one that I have not coded well. There are not many people coding for sound as far as I can see, and even fewer coding for voice, so help is rare. I don't know how familiar you are with javax.sound.sampled, but at this point, in my opinion, the strange nature of that API should not put you off looking for a mechanical logic error, because I believe that is where I might have gone wrong. The key is that in the client, a "ServerMic" takes in the signal and puts it into Datagrams. These are put on a queue, and then a "ClientMic" (just a thread) sends the packet to the remote peer that it is responsible for. At the receiving end, a thread picks up the packet, asks where it came from, and makes a Line out to the speakers ( a SourceDataline) to play it to. So there is a SDL per sending peer (and the sending peer obviously does the same thing itself, to listen to this peer). This all works fine with just 2, but what is going on when the 3rd one joins in is the question! Hope some of that makes sense.

The link
 http://www.experts-exchange.com/Programming/Languages/Java/Q_27936978.html
0
 
zzynxSoftware engineerCommented:
Hi krakatoa,
>> I don't know how familiar you are with javax.sound.sampled
I'm sorry, but I'm not familiar with it at all. Never did any sound stuff in Java.
So, I'm afraid I'll be of no help here.
Sorry, again.
0
 
krakatoaCommented:
That's fine. Not many people seem to have gone into sound with Java. I thought you might be able to see look at the logicality of a couple of the methods, not particularly anything to do with sound as such. But I take your point, and thanks for looking anyway.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

  • 6
  • 5
  • 3
  • +2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now