Design a class named Time. The class contains: The data fields hour, minute, and second that represent a time. A no-arg constructor that creates a Time object for the current time. (The values of the data fields will represent the current time.) A constructor that constructs a Time object with a specified elapsed time since midnight, January 1, 1970, in milliseconds. (The values of the data fields will represent this time.) A constructor that constructs a Time object with the specified hour, minute, and second. Three getter methods for the data fields hour, minute, and second, respectively. A method named set Time(long elapsed Time) that sets a new time for the object using the elapsed time. For example, if the elapsed time is 555550000 milliseconds, the hour is 10, the minute is 19, and the second is 10. Write a test program that creates two Time objects (using new Time() and new Time(555550000)) and displays their hour, minute, and second in the format hour:minute:second. Hint: The first two constructors will extract the hour, minute, and second from the elapsed time. For the no-arg constructor, the current time can be obtained using System.currentTimeMillis()

Respuesta :

Answer:

import java.util.Scanner;

class Time

{

  int hour,min,sec;

 

      Time()

      {

      long millisecs=System.currentTimeMillis();

      sec=(int)millisecs%60;

      min=(int)(millisecs/60)%60;  

      hour=(int)(millisecs/(60*60))%24;

      }

 

      Time(long millisecs)

      {

      sec=(int)millisecs%60;

      min=(int)(millisecs/60)%60;  

      hour=(int)(millisecs/(60*60))%24;

      }

     

      Time(int hour,int min,int sec)

      {

          this.hour=hour;

          this.min=min;

          this.sec=sec;

      }

      void readTime()

      {

          Scanner s=new Scanner(System.in);

          System.out.println("Type Hours : ");

          hour=s.nextInt();

          System.out.println("Type mins : ");

          min=s.nextInt();

          System.out.println("Type sec : ");

          sec=s.nextInt();

      }

     

      void setTime(long ellapseTime)

      {

      sec=(int)ellapseTime%60;

      min=(int)(ellapseTime/60)%60;  

      hour=(int)(ellapseTime/(60*60))%24;

      }

     

      void showTime()

      {

          System.out.println("Time is(hh:mm:ss) - "+hour+":"+min+":"+sec);

      }

  }

  class ShowCurrentTime

  {

      public static void main(String args[])

      {  

          Time t1=new Time();

          Time t2=new Time(555550000);

          Time t3=new Time(4,46,34);

          t1.showTime();

          t2.showTime();

          t3.showTime();

      }

  }

Explanation:

  • Initialize the seconds, miliseconds, minutes and hours in the constructor of the Time class.
  • Take the hour, minute and seconds as an input from user inside the readTime method.
  • Calculate the hour, minute and seconds inside the setTime method.
  • Inside the main method, create object of the Time class and call the showTime method to display the time.
ACCESS MORE