Solved

Simple Java Playlist

Posted on 2013-01-12
16
596 Views
Last Modified: 2013-01-17
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
Comment
Question by:Kevin_Coors
  • 6
  • 5
  • 3
  • +2
16 Comments
 
LVL 37

Expert Comment

by:momi_sabag
Comment Utility
I find it hard to believe users will enter that information
Why don't you get it from the file itself?
0
 

Author Comment

by:Kevin_Coors
Comment Utility
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
 
LVL 16

Assisted Solution

by:krakatoa
krakatoa earned 200 total points
Comment Utility
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
 
LVL 6

Expert Comment

by:yats
Comment Utility
Are you trying to print duration of each song as well??
0
 
LVL 37

Accepted Solution

by:
zzynx earned 300 total points
Comment Utility
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
 
LVL 16

Expert Comment

by:krakatoa
Comment Utility
Ha - I only read the last sysout delivering the minutes. Teach me to read the whole thing. ;) Well spotted zzynx.
0
 

Author Comment

by:Kevin_Coors
Comment Utility
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
 
LVL 37

Expert Comment

by:zzynx
Comment Utility
>> Well spotted zzynx.
:-)

>> I hope this is ok?
Seems reasonable to me.
0
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
LVL 16

Expert Comment

by:krakatoa
Comment Utility
And just to think . . . screaming . . .  *I could have had it all, I could have had it all*!

Yeah's fine.
;)
0
 

Author Comment

by:Kevin_Coors
Comment Utility
Thanks :)
0
 
LVL 37

Expert Comment

by:zzynx
Comment Utility
Thanx 4 axxepting
0
 
LVL 16

Expert Comment

by:krakatoa
Comment Utility
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
 
LVL 37

Expert Comment

by:zzynx
Comment Utility
Hi krakatoa,
Sure. Post your code in a new question (and post the link here) and I'll see what I can do.
0
 
LVL 16

Expert Comment

by:krakatoa
Comment Utility
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
 
LVL 37

Expert Comment

by:zzynx
Comment Utility
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
 
LVL 16

Expert Comment

by:krakatoa
Comment Utility
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

Featured Post

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
triangle challenge 4 75
countX 22 68
Problem to start Neon 20 49
recursion example 16 66
For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.

772 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now