2 Replies - 364 Views - Last Post: 12 October 2012 - 11:27 PM Rate Topic: -----

#1 freeasabird  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 12-October 12

SIMPLE program help...

Posted 12 October 2012 - 08:18 PM

I need help to solve this... I have to do a program that converts milliseconds into hours, minutes, seconds, and milliseconds. Its supposed to be like this:
Starting milli-seconds: 10000123
Hours: 2
Minutes: 46
Seconds: 40
Milli Seconds: 123

And this is the code i have gotten so far, yet nothing works. Help?
public class Lab03A80
{
	public static void main(String args[])
	{
		int hours;
		int minutes;
		int seconds ;
		int startingmilliseconds = 10000123;
		int milliseconds;
		int remaindermilliseconds;
		int remainderseconds;
		
		hours = startingmilliseconds / 3600000;
		remaindermilliseconds = startingmilliseconds % 360000;
		minutes = remaindermilliseconds / 60000;
		seconds = minutes % 60; 
		milliseconds = seconds % 1000;
		
		System.out.println("starting milliseconds:  " + startingmilliseconds);
		System.out.println("hours: " + hours);
		System.out.println("minutes: " + minutes);
		System.out.println("seconds: " + seconds);
		System.out.println("milliseconds: " + milliseconds);
		}	
}




Is This A Good Question/Topic? 0
  • +

Replies To: SIMPLE program help...

#2 jon.kiparsky  Icon User is online

  • Pancakes!
  • member icon


Reputation: 7578
  • View blog
  • Posts: 12,742
  • Joined: 19-March 11

Re: SIMPLE program help...

Posted 12 October 2012 - 09:06 PM

So what is it that's not working? Spell it out, don't make me guess what problem I'm supposed to help you solve.
In fact, walk through it: it's a simple series of calculations.

At each step, you should have a pretty good idea of what you expect the values of each variable to be sat each step in the program. (otherwise you wouldn't know something was wrong)

Could you just re-post your code and insert those values as comments? Like so:


int a = 1;
int b = 3;
int c = a+b; // c = 4
int d = 4/b; // d = 1



I think this will be a useful exercise for you.
Was This Post Helpful? 0
  • +
  • -

#3 raghav.naganathan  Icon User is offline

  • Perfectly Squared ;)
  • member icon

Reputation: 408
  • View blog
  • Posts: 1,440
  • Joined: 14-September 12

Re: SIMPLE program help...

Posted 12 October 2012 - 11:27 PM

What I see that there is nothing wrong with the syntax of your code. It compiles well but the logic is not correct.

The mistake you are doing is using the value of remaindermilliseconds. The question is asking you to convert milliseconds to hours, minutes , seconds and milliseconds.

So, it would be really better to use the initialmilliseconds value every time.

Let me give you a hint. You need to use the modulus operator( several times sometimes) in the same formula.

To start you up, since your hours are coming as desired, I'm giving you this formula for minute.

minutes = (startingmilliseconds % (1000*60*60)) / (1000*60);



Now, you need to calculate for seconds and milliseconds.
If you understand the logic of my code, you will find out how to go about your problem.Good luck :)

regards,
Raghav
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1