# Ideas for sloving this problem.

• December 10th, 2012, 08:11 PM
Rain_Maker
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. :o
• December 10th, 2012, 08:14 PM
Norm
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.
• December 10th, 2012, 08:39 PM
Rain_Maker
Re: Ideas for sloving this problem.
Code Java:

```  //****************************************************************************** // 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?
• December 10th, 2012, 08:43 PM
Norm
Re: Ideas for sloving this problem.
Try doing solving the problem using pencil and paper.
Try 133 seconds.
Try 3733 seconds.
• December 10th, 2012, 10:52 PM
Rain_Maker
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.
• December 11th, 2012, 07:02 AM
Norm
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.
• December 11th, 2012, 07:45 AM
billyjthorton
Re: Ideas for sloving this problem.
Also, think about "int" math. When you have a problem, take this for example
Code :

`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:
Code :

`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.
• December 11th, 2012, 03:45 PM
Rain_Maker
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.
• December 11th, 2012, 03:55 PM
Norm
Re: Ideas for sloving this problem.
Quote:

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?
• December 11th, 2012, 04:18 PM
Rain_Maker
Re: Ideas for sloving this problem.
SPARRRRTAAAAAAAAAAAA LMFAO......SORRY......... but I did it, it works for every value I put in.

Code Java:

```  //****************************************************************************** // 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