Wrox Programmer Forums
Java Basics General beginning Java language questions that don't fit in one of the more specific forums. Please specify what version.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Java Basics section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
Old September 22nd, 2011, 04:11 AM
Authorized User
Join Date: Sep 2011
Posts: 12
Thanks: 1
Thanked 0 Times in 0 Posts
Default Timer Exercise

Hi Everyone

I have this piece of a work:

hours = allSeconds / 3600;
int remainder = allSeconds % 3600;
minutes = remainder / 60;
seconds = remainder % 60;
Can any body please explain it to me, the %60 part especially.

Last edited by stahorse; September 22nd, 2011 at 08:19 AM..
Old October 18th, 2011, 01:28 PM
jminatel's Avatar
Wrox Staff
Points: 18,059, Level: 58
Points: 18,059, Level: 58 Points: 18,059, Level: 58 Points: 18,059, Level: 58
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
Join Date: May 2003
Posts: 1,906
Thanks: 62
Thanked 139 Times in 101 Posts

Sure. But first I'll tell you, integer division and remainders in Java arithmetic are covered well in the Wrox book Beginning Java. In Beginning Java Java 7 Edition integer division and remainders are covered in chapter 2 "Programs, Data, Variables, and Calculation."

In Java, the standard division operator / returns just the whole integer part of the result. The modulus operator % returns the remainder. Using your example code and it helps me visualize this to pick a sample value for allSeconds of say 5000 to work through this

hours = allSeconds / 3600;
This gives you the whole number of hours in allSeconds. If the value of allSeconds is 5000: 5000 / 3600 yields 1

int remainder = allSeconds % 3600;
Now you're decalring remainder and getting the remainder of AllSeconds divided by 3600. With allSeconds at 5000, the % operator would leave 1400

minutes = remainder / 60;
Now the number of minutes again is the whole result of remainder divided by 60. Back to the sample, 1400 / 60 yields 23

seconds = remainder % 60;
And finally, the seconds are the remainder of your remainder divided by 60. So that 1400 / 60 had a leftover remainder of 20. That's the seconds.

Even though this is probably too late for your homework, hope it helps you or someone else.
Jim Minatel
Associate Publisher, WROX - A Wiley Brand
Did someone here help you? Click on their post!

Similar Threads
Thread Thread Starter Forum Replies Last Post
Countdown timer DennyLoi Javascript How-To 2 November 27th, 2007 08:01 AM
Timer Dhanapal ADO.NET 2 April 16th, 2007 04:58 AM
Timer prasanta2expert Access VBA 1 November 8th, 2006 09:28 AM
timer amerei ASP.NET 1.0 and 1.1 Professional 0 October 15th, 2005 10:49 PM
Timer Control sankar_massoft General .NET 0 December 21st, 2004 11:53 PM

Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.