Solved

Simple Java Playlist

Posted on 2013-01-12
16
689 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 6
  • 5
  • 3
  • +2
16 Comments
 
LVL 37

Expert Comment

by:momi_sabag
ID: 38770801
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
ID: 38770809
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
ID: 38771788
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
PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

 
LVL 6

Expert Comment

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

Accepted Solution

by:
zzynx earned 300 total points
ID: 38773912
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
ID: 38774019
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
ID: 38774036
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
ID: 38774095
>> Well spotted zzynx.
:-)

>> I hope this is ok?
Seems reasonable to me.
0
 
LVL 16

Expert Comment

by:krakatoa
ID: 38774222
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
ID: 38774318
Thanks :)
0
 
LVL 37

Expert Comment

by:zzynx
ID: 38774711
Thanx 4 axxepting
0
 
LVL 16

Expert Comment

by:krakatoa
ID: 38786816
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
ID: 38786862
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
ID: 38787402
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
ID: 38787512
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
ID: 38787602
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

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
In this post we will learn how to make Android Gesture Tutorial and give different functionality whenever a user Touch or Scroll android screen.
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 tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
Suggested Courses

626 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