Help on program!

Time Converter program help!

Page 1 of 1

2 Replies - 901 Views - Last Post: 24 October 2008 - 02:54 AM Rate Topic: -----

#1 cookz74  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 24-October 08

Help on program!

Posted 24 October 2008 - 01:39 AM

Hi guys, this is my first Java program that i have to create and im completely new to Java so was wondering if somebody could point me in the right direction. Below is the task set out for me.

Write and test a Java application with just a main method class called TimeConverter to do the
following:
(i) Prompt for and read in an integer value representing a time in seconds.
(ii) Print out the equivalent time in hours, minutes and seconds, in the format hrs:mins:secs. If
either mins or secs is less than 10, a leading zero should be displayed.

This is my code so far

/*
 * 
 */

package TimeConverter;
import java.util.Scanner;

/**
 *
 * @author etc07bnu
 */
public class TimeConverter {

	/**
	 * @param args the command line arguments
	 */
	public static void main(String[] args) {
		
		
		Scanner scan = new Scanner(system.in);
		
	   
		System.out.println("Please enter a value in seconds");


I know that not much has been done so far but im all new to this and completely stuck on the next steps

Help would be much appreciated :)

Regards!

Is This A Good Question/Topic? 0
  • +

Replies To: Help on program!

#2 Gloin  Icon User is offline

  • Expert Schmexpert...
  • member icon

Reputation: 235
  • View blog
  • Posts: 4,489
  • Joined: 04-August 08

Re: Help on program!

Posted 24 October 2008 - 02:22 AM

You can certainly make use of the modulo operator as well as integer division.

ex: 12345 seconds

12345 % 60 = 45 //modulo 60 (60 seconds in 1 minute) means the remaining seconds will be 45.
12345 / 60 = 205 //integer division, the remainder is thrown away leaving you with 205 minutes.

Now let the program continue in the same way with the minutes.

205 % 60 = 25 //60 minutes in one hour.
205 / 60 = 3 // 3 * 60 = 180, remainder is 25 (Yep, the answer above)

Leaves you with 3 hours.

So how about putting leading zeros in, well I would have used an array of strings
["00", "01", "02", "03", "04", "05", "06", "07", "08", "09",...., "58", "59"]
Was This Post Helpful? 0
  • +
  • -

#3 g00se  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2639
  • View blog
  • Posts: 11,148
  • Joined: 20-September 08

Re: Help on program!

Posted 24 October 2008 - 02:54 AM

Hold your time info in an array and you can format it thus:

String t = String.format("%02d:%02d:%02d", time[0], time[1], time[2]);


Was This Post Helpful? 0
  • +
  • -

Page 1 of 1