Looking for reliable answers? Westonci.ca is the ultimate Q&A platform where experts share their knowledge on various topics. Explore thousands of questions and answers from a knowledgeable community of experts on our user-friendly platform. Get immediate and reliable solutions to your questions from a community of experienced professionals on our platform.

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.

Sagot :

Answer:

hope this helps.

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

 

  }

}