Win a copy of Spring Boot in Practice this week in the Spring forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Liutauras Vilda
  • Henry Wong
  • Devaka Cooray
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Al Hobbs
  • Carey Brown
Bartenders:
  • Piet Souris
  • Mikalai Zaikin
  • Himai Minh

Using simple GUI to convert seconds to days, minutes, and seconds.

 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello, I am a newbie in Java and practicing some simple programs. I was using a simple GUI to convert seconds to days, minutes, and seconds.  The code is not functioning properly. Here is the code, could you please correct the calculation ?

 
Master Rancher
Posts: 4839
38
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

The code is not functioning properly.


Please explain.  
Copy the output and paste it here to show what the code is currently doing.
Add a print statement to print the output that can be copied.
Add some comments where you think the output is wrong.

Please wrap all posted code in code tags:  Select the code and press the Code button.
 
Marshal
Posts: 27288
87
Eclipse IDE Firefox Browser MySQL Database
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Mahadi Hasan wrote:The code is not functioning properly. Here is the code, could you please correct the calculation ?



It's not possible to solve a problem until you know what it is. It's tempting to think that "not functioning properly" is a description of the problem. And it is, but it isn't a useful description. A useful description would tell us at least what the input was, what the output was, and what you expected instead.
 
Marshal
Posts: 76110
362
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Mahadi Hasan wrote:. . . . I was using a simple GUI to convert seconds to days, minutes, and seconds. . . .

I am afraid you are mistaken there. You are using a simple GUI to get input and to display the results of your calculation. You should always separate display from the actual program.
Please go through youir code and see what each line calculates. You are correct that you can get mintes by dividing seconds by 60, and integer divison will round towards zero, which gives you the correct number of minutes. But if youi want the minutes to be in additon to hours, you will have to do some different arithmetic.
By thee way: please use the code buttonif I go back to your post and use the code button, you can see how much better it sill look

And, welcome to the Ranch
 
Mahadi Hasan
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Marshal Ritchie, Thank you. your comment really got me into the correct solution. Found the right formula in the end.
 
Campbell Ritchie
Marshal
Posts: 76110
362
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Well done; please show us the current code. We can probably suggest lots of improvements.
 
Mahadi Hasan
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello Ritchie, Here is the solution.

 
Campbell Ritchie
Marshal
Posts: 76110
362
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
That's good, but I can think of all sorts of ways to improve it. This is how it started:-Let's improve the formatting by getting rid of excess whitespace:-Now, get rid of the long lines:-Let's put some whitespace around your binary operators:-Now, stop using _ characters:-Now get rid of the magic numbers, replacing them with constants:-Lines 1 2 3 can be converted to (static) fields.
Now, some people will say this ia overkill, but let' convert that to an object:-You can use that code like this:-
 
Mahadi Hasan
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Wow ! that's really mind blowing !  I really appreciate the way you just taught me the formatting and variable declaring method. I will follow this method. Thank you so much.
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic