Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 10 of 10

Thread: Ideas for sloving this problem.

  1. #1
    Member
    Join Date
    Dec 2012
    Location
    Detroit Mi
    Posts
    122
    My Mood
    Amazed
    Thanks
    14
    Thanked 0 Times in 0 Posts

    Default Ideas for sloving this problem.

    Okay I been practicing with some java code. I'm trying to solve a problem on the back of this chapter. I kind of I want to solve this problem with out code being given to me. But I do want to be given hints and direction on what I can do to run this application. Here is the problem.

    ***Problem***
    Create a version of the previous project that reverses the computation. That is, read a value representing a number of seconds, then print the equivalent amount of time as a combination of hours, minutes, and seconds. (For example, 9999 seconds is equivalent to 2 hours, 46 minutes, and 39 seconds.)

    [code = Java]

    //************************************************** ****************************
    // PP 2.9.java Author: Rain_Maker
    //
    // This application will take the input value of
    // minutes and secs. It will then display the over all users times in secs.
    //************************************************** ***************************



    package pp2.pkg9;

    import java.util.*;

    public class Pp29

    {


    public static void main(String[] args)

    {


    double hours,minutes,seconds;


    Scanner scan = new Scanner(System.in);


    System.out.print(" How many secs did you travel? ");
    seconds = scan.nextInt();

    hours = (int) seconds/3600;
    minutes = hours/60;
    seconds = minutes%60;

    System.out.println( " The amount hours : " + hours);
    // System.out.println( " The amount minutes: " + minutes);
    // System.out.println( " The amount secs: " + seconds);





    }
    }

    [/code]


    I have the answer for hrs, but minutes and secs are a little difficult to solve. Can some one give me a clue on what I can do to solve this problem. I have another similar problem on the next problem I want to solve. So I really want some type of self accomplishment if I can solve both problems.


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,165
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Ideas for sloving this problem.

    Can you post the program's current output so we can see what the problem is?
    Add some comments explaining what is wrong with the output and say what it should be.


    Execute the program with different data to see what happens then.

    Try working out the correct formulas with a pencil and paper before writing any more code.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Member
    Join Date
    Dec 2012
    Location
    Detroit Mi
    Posts
    122
    My Mood
    Amazed
    Thanks
    14
    Thanked 0 Times in 0 Posts

    Default Re: Ideas for sloving this problem.

     
    //******************************************************************************
    //  PP 2.9.java                         Author: Rain_Maker
    //
    //  This application will take the input value of secs.  And display the results
    //  in hrs, minutes and secs. 
    //*****************************************************************************
     
     
     
    package pp2.pkg9;
     
    import java.util.*;
     
    public class Pp29 
     
    {
     
     
        public static void main(String[] args) 
     
        {
     
     
            double hours,minutes,seconds;
     
     
            Scanner scan = new Scanner(System.in);
     
     
           System.out.print(" How many secs did you travel? ");
           seconds = scan.nextInt();
     
           hours =  (int) seconds/3600; //This is supposed to show hrs
           minutes =  hours/60;  //This is supposed to show minutes
           seconds = minutes%60; //This is supposed to show secs 
     
     
           //this displays hrs
           System.out.println( " The amount hours : " + hours + " hrs"); //
     
           //this display minutes
           System.out.println( " The amount mintues: " + minutes + " mins" );
     
           //this display seconds
           System.out.println( " The amount secs: " + seconds + " secs" );
     
     
        }
    }


    Output

    run:
    How many secs did you travel? 9999
    The amount hours : 2.0 hrs //this is right
    The amount mintues: 0.03333333333333333 mins // this is wrong, this is supposed to be 46mins
    The amount secs: 0.03333333333333333 secs // this is wrong, this is supposed to be 39 secs
    BUILD SUCCESSFUL (total time: 2 seconds)

    The application is supposed to take input of secs and convert them into hrs, mins and secs. The example used 9999 secs but it supposed to work on all secs enter in this text application. Do you have any ideas to find mins and secs?

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,165
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Ideas for sloving this problem.

    Try doing solving the problem using pencil and paper.
    Try 133 seconds.
    Try 3733 seconds.
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Member
    Join Date
    Dec 2012
    Location
    Detroit Mi
    Posts
    122
    My Mood
    Amazed
    Thanks
    14
    Thanked 0 Times in 0 Posts

    Default Re: Ideas for sloving this problem.

    Well my problem is developing the logic to even solve problems like these. I mean understanding the code statements is easy, but creating the logic is another story. As a programer with experience how will you go about solving this problem? I have a feeling I'm going to use the mod operation at any point of my code statements.

    --- Update ---

    Norm I'm going to go back over my code and call a night. I will try to respond tomorrow if I have any problems. I'm going to rewrite my forumula again and try again. Thank you for your advice. I find myself getting fraustrated to Easily.

    --- Update ---

    Okay lol,

    I couldn't sleep so I started doing the problem again. I understand what my formula needs to be. My question is now once I find out the answer how do I retrieve the reminder. For example 9999/3600 = 2.7775, I now have my hrs in which I will display the output as an integer. But I wanted to retrieve the reminder to that number. I'm confuse on what I should do after this part.

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,165
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Ideas for sloving this problem.

    Try solving the problem manually for 133 seconds. Write down the equations and arithmetic operations you would use to get the hours, minutes and seconds for 133 seconds. Post the steps here.

    Then do the same thing for 3733 seconds.

    Forget about writing code until you can manually solve the problem.
    If you don't understand my answer, don't ignore it, ask a question.

  7. #7
    Junior Member
    Join Date
    Sep 2012
    Posts
    13
    Thanks
    1
    Thanked 1 Time in 1 Post

    Default Re: Ideas for sloving this problem.

    Also, think about "int" math. When you have a problem, take this for example
    int a = 4/3
    what value do you think will be returned? If you thought "1", then your right.
    In the same respect what do you think the output would be of this:
    int a=4%3
    Yup. you got it, "1". This is because integer logic essentially does not compute decimals, which might be causing your problem. Try using this logic to help you when dividing the seconds to get hours, minutes and seconds.

  8. The Following User Says Thank You to billyjthorton For This Useful Post:

    Rain_Maker (December 11th, 2012)

  9. #8
    Member
    Join Date
    Dec 2012
    Location
    Detroit Mi
    Posts
    122
    My Mood
    Amazed
    Thanks
    14
    Thanked 0 Times in 0 Posts

    Default Re: Ideas for sloving this problem.

    Okay here is my arithmetic. My math skills has gone down these last few years but here is what I have.

    3733 * 1/3600(1hr) = 1.036... = 1hr

    3733 – 3600(1hr) = 133 secs


    133 * 1/60(1min) = 2 mins

    133 – 120 (2min) = 13 secs

    Final answer 1hrs, 2 mins & 13 secs



    Thanks for the replays everyone. So I done everything in code. I'm going to write my code in similar fashion but I will wait until you responses NORM.

  10. #9
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,165
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Ideas for sloving this problem.

    3733 * 1/3600(1hr) = 1.036... = 1hr
    If you use int variables you won't have to truncate decimal places


    Have you revised the code to follow the steps you used in post#8?
    If you don't understand my answer, don't ignore it, ask a question.

  11. The Following User Says Thank You to Norm For This Useful Post:

    Rain_Maker (December 11th, 2012)

  12. #10
    Member
    Join Date
    Dec 2012
    Location
    Detroit Mi
    Posts
    122
    My Mood
    Amazed
    Thanks
    14
    Thanked 0 Times in 0 Posts

    Default Re: Ideas for sloving this problem.

    SPARRRRTAAAAAAAAAAAA LMFAO......SORRY......... but I did it, it works for every value I put in.

     
    //******************************************************************************
    //  PP 2.9.java                         Author: Rain_Maker
    //
    //  This application will take the input value of secs. And display the results
    //  in hrs, minutes and secs. 
    //*****************************************************************************
     
     
     
    package pp2.pkg9;
     
    import java.util.*;
     
    public class Pp29 
     
    {
     
     
        public static void main(String[] args) 
     
        {
     
     
            double hours,minutes,seconds,a,b;
     
            Scanner scan = new Scanner(System.in);
     
     
           System.out.print(" How many secs did you travel? ");
           seconds = scan.nextDouble();
     
           hours =  seconds/3600; //This is supposed to show hrs
               a =  seconds%3600;
     
             minutes = a/60;    
             b = seconds % 60;
     
     
     
     
     
           //this displays hrs
          System.out.println( " The amount hours : " + (int) hours + " hrs"); 
     
           //this display minutes
          System.out.println( " The amount mintues: " + (int) minutes + " mins" );
     
           //this display seconds
          System.out.println( " The amount secs: " +   (int) b + " secs" );
     
     
        }
    }

    Thanks again Norm and billyjthorton! Now is time to do another project, this is addictive. lol

Similar Threads

  1. Help i cant get it to work any ideas thanks
    By Freshtilldeath0 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: October 30th, 2012, 08:26 AM
  2. Project ideas
    By seal308 in forum Collections and Generics
    Replies: 2
    Last Post: August 3rd, 2012, 02:27 PM
  3. NEED HELP WITH IDEAS
    By ash12 in forum What's Wrong With My Code?
    Replies: 2
    Last Post: July 21st, 2012, 04:52 PM
  4. Java Speech API and web.. problem getting ideas
    By yugeshshrestha in forum Java Theory & Questions
    Replies: 0
    Last Post: July 7th, 2012, 02:27 AM
  5. Any ideas?
    By ahender1 in forum What's Wrong With My Code?
    Replies: 3
    Last Post: February 2nd, 2012, 03:58 PM