28.0k views
4 votes
Write a method that converts a time given in seconds to hours, minutes and seconds using the following header:

public static String convertTime(int totalSeconds)

The method returns a string that represents the time as hours:minutes:seconds.

Write a test program that asks the user for the time in seconds and then display the time in the above format.

1 Answer

1 vote

Answer:

hope this helps.

Step-by-step explanation:

import java.util.*;

class Timeconvert{ //class nane

public static String convertTime(int totalSeconds) /* fn to convert seconds to hh:mm:ss format */

{

int sec = totalSeconds; // a copy of totalSeconds is stored in sec

int m=sec/60; //to find the total min which obtained by doing sec/60

int psec=sec%60; //psec store remaining seconds

int hrs=m/60; //stores hr value obtained by doing m/60

int min=m%60; //stores min value obtained by m%60

return (hrs + ":" + min+ ":" +psec);// returning that string

}

}

public class Main

{

public static void main(String[] args) {

Timeconvert t = new Timeconvert();

Scanner in = new Scanner(System.in); //Scanner class

System.out.println("Enter the number of seconds:");

int sec = in.nextInt(); //to input number of seconds

System.out.println("hours:minutes:seconds is " + t.convertTime(sec)); //result

}

}

User Ginna
by
3.0k points