Coding Lab - TechOnTechnology

Java Program to do Following : Define a class called Time. Implement the following methods Add T1 and T2 ,Display the Time.




import java.io.*;
import java.lang.*;
class timedemo
{
    int hour,min,sec;
    timedemo(int x,int y,int z)
    {
        hour=x;
        min=y;
        sec=z;
    }
}
class time
{
public static timedemo add(timedemo t1,timedemo t2)
{
    int hour=t1.hour+t2.hour;
    int min=t1.min+t2.min;
    int sec=t1.sec+t2.sec;
    if ( sec>= 60)
    {
        sec=sec-60;
        min=min+1;
    }
    if(min>=60)
    {
        min=min-60;
        hour=hour+1;
    }
    if(hour>=12)
    {
        hour=hour-12;
    }
timedemo t3=new timedemo(hour,min,sec);
return (t3);
}
public static void display(timedemo t)
{
    System.out.println("TIME : "+t.hour+"hours :"+t.min+"minutes :"+t.sec+"seconds");
}
public static void main(String args[]) throws IOException
{
    int h,m,s;
    DataInputStream in=new DataInputStream(System.in);
   
        System.out.println("\nENTER TIME : ");
        System.out.println("\nENTER Ist [ T1 ] TIME [ HH:MM:SS ] : ");
        System.out.print("Hours : ");
        h = Integer.parseInt(in.readLine());
        System.out.print("Minutes : ");
        m = Integer.parseInt(in.readLine());
        System.out.print("Seconds : ");
        s = Integer.parseInt(in.readLine());
       
        timedemo t1=new timedemo(h,m,s);
       
        System.out.println("\nENTER IInd [ T2 ] TIME [ HH:MM:SS ] : ");
        System.out.print("Hours : ");
        h = Integer.parseInt(in.readLine());
        System.out.print("Minutes : ");
        m = Integer.parseInt(in.readLine());
        System.out.print("Seconds : ");
        s = Integer.parseInt(in.readLine());

        timedemo t2=new timedemo(h,m,s);
   
        timedemo t3;
        t3=add(t1,t2);    // calling add method to add two time
        System.out.println("\nIst ");display(t1);      // calling display method
        System.out.println("\nIInd ");display(t2);
        System.out.println("\nI+II ");display(t3);
}
}

Name

Email *

Message *