JAVA-Create a class called Time1224-Put this class and Date class created in a package called timePack and use it in another class named trainSchedule

Q. Create a class called Time1224 with the following: 
  a. Instance variables – hour, minute, second 
  b. A three-argument constructor 
  c. Methods for add and subtract two times
  d. A method to display the 12 hour time [e.g. 09:45:27 PM] 
Put this class and Date class created in Assignment 2.3 in a package called timePack and use it in another class named trainSchedule with the following:
  a. Instance variables – trainNo, deptTime, arrTime, stationCode 
  b. Constructors – default and parameterized 
  c. A method to display the train schedule with Date and Time 
Write a JAVA program to implement all the above functionalities.



time1224.java :
package timepack;
public class time1224
{
public int hour,minute,second;
public time1224(int h,int m,int s)
{
hour=h;
minute=m;
second=s;
}
public void add(time1224 t1,time1224 t2)
{
hour=t1.hour+t2.hour;
minute=t1.minute+t2.minute;
second=t1.second+t2.second;
//System.out.println("Addition = "+hour+"/"+minute+"/"+second);
}
public void sub(time1224 t1,time1224 t2)
{
hour=t1.hour-t2.hour;
minute=t1.minute-t2.minute;
second=t1.second-t2.second;
//System.out.println("substract = "+hour+"/"+minute+"/"+second);
}
public String twe()
{
return(hour+":"+minute+":"+second);
}
}

date.java :
package timepack;
import java.util.*;
public class date
{
public int date,month,year;
public date()
{
date=1;
month=1;
year=1970;
}
public date(int dat,int mon)
{
date=dat;
month=mon;
year=2013;
}
public  date(int x,int y, int z)
{
date=x;
month=y;
year=z;
}
public void setdate()
{
Scanner sc=new Scanner(System.in);
System.out.println("enter the date: ");
date=sc.nextInt();
System.out.println("enter the month: ");
month=sc.nextInt();
System.out.println("enter the year: ");
year=sc.nextInt();
}
public String getdate()
{
return (date+"/"+month+"/"+year);
}
public void display()
{
System.out.println("dd/mm/yyyy");
System.out.println(getdate());


}

train.java :
import timepack.date;
import timepack.time1224;
class trainschedule
{
int trainno; time1224 depttime, arrtime;
int stationcode;
trainschedule()
{
trainno=1334;
time1224 ob1=new time1224(12,33,55);
time1224 ob2=new time1224(2,4,5);
depttime=ob1;
arrtime=ob2;
stationcode=23;
}
trainschedule(int t,time1224 dt,time1224 at,int s)
{
trainno=t;
depttime=dt;
arrtime=at;
stationcode=s;
}
void display()
{
date d=new date();
d.setdate();
System.out.println("train number = " +trainno+  "\ndept time = "+depttime.twe()+" \narrival time ="+arrtime.twe()+"\nstationcode= "+stationcode+"\ndate: "+d.getdate());
}
}
class train
{
public static void main(String args[])
{
time1224 ob1=new time1224(3,6,55);
time1224 ob2=new time1224(4,5,1997);
trainschedule ob=new trainschedule(45,ob1,ob2,52);
ob.display();
}
}

Output:
enter the date: 08
enter the month: 06
enter the year: 2018
train number= 1334
dept time- 4:45:34
arrival time= 5:20:00
station code= 52
date: 08/06/2018

//if you have any problem, comment below

Comments