ISC COMPUTER SCIENCE PRACTICAL– 2000 Days Elapsed, ISC JAVA PROGRAMS
Write a program to input two valid dates, each comprising of day (2 digits), Month (2
digits) and year (4 digits) and calculate the days elapsed between the two dates.
Test your program for the following data values:
i) FIRST DATE : DAY : 24
MONTH: 09
YEAR: 1960
SECONDDATE : DAY : 08
MONTH: 12
YEAR: 1852
ii) FIRST DATE : DAY : 10
MONTH: 01
YEAR: 1852
SECONDDATE : DAY : 16
MONTH: 10
YEAR: 1952
import java.util.*;
import java.io.*;
public class DaysElapsed {
public static void main(String[] args) throws IOException {
int day,month,year;
int day1,month1,year1;
boolean flag;
Scanner in = new Scanner(System.in);
DaysCalculation date = new DaysCalculation();
DaysCalculation date1 = new DaysCalculation();
do {
System.out.println("INPUT THE FIRST DATE:");
System.out.println("INPUT DAY:");
day = in.nextInt();
System.out.println("INPUT MONTH:");
month = in.nextInt();
System.out.println("INPUT YEAR:");
year = in.nextInt();
flag = date.checkDate(day,month,year);
if (flag == false)
System.out.println("INVALID,DATE,INPUT AGAIN");
} while(flag != true);
date.setDate(day,month,year);
do {
System.out.println("INPUT THE SECOND DATE:");
System.out.println("INPUT DAY:");
day1 = in.nextInt();
System.out.println("INPUT MONTH:");
month1 = in.nextInt();
System.out.println("INPUT YEAR:");
year1 = in.nextInt();
flag = date1.checkDate(day1,month1,year1);
if (flag == false)
System.out.println("INVALID,DATE,INPUT AGAIN");
} while(flag != true);
System.out.println("DAYS ELAPSED:"+date.countDays(day1,month1,year1));
}
}
class DaysCalculation {
public void setDate(int d, int m, int y ){
day = d;
month = m;
year = y;
}
public int countDays(int day1, int month1, int year1 ){
int daysCount = 0;
int y=0,y1=0;
boolean small = false;
if (year == year1)
if (month == month1)
if (day < day1)
small = true;
else
small = false;
else if ( month < month1)
small = true;
else
small = false;
else if( year < year1)
small = true;
else
small = false;
if (small == false) {
int td,tm,ty;
td = day;
day = day1;
day1 = td;
tm = month;
month = month1;
month1 = tm;
ty = year;
year = year1;
year1=ty;
}
if (checkLeap(year) == true) {
y += (ldays[month-1]-day);
for ( int i = month; i <=11;i++)
y +=ldays[i];
}
else {
y += (mdays[month-1]-day);
for ( int i = month; i <=11;i++)
y +=mdays[i];
}
if (checkLeap(year1) == true) {
y1 += day1-1;
for ( int i = 0; i <month1-1;i++)
y1 +=ldays[i];
}
else {
y1 += day1-1;
for ( int i = 0; i <month1-1;i++)
y1 +=mdays[i];
}
for ( int i = year+1; i <= year1-1; i++) {
if(checkLeap(i) == true)
daysCount +=366;
else
daysCount +=365;
}
if (year == year1)
if (checkLeap(year) == true)
daysCount = y1-(366-y);
else
daysCount = y1-(365-y);
else {
daysCount += (y1+y);
}
return daysCount;
}
public boolean checkDate(int day, int month, int year) {
if (checkLeap(year) == true) {
if( 1 <= month && month <=12) {
if(day>ldays[month-1] || day < 1 )
return false;
else
return true;
}
else
return false;
}
else {
if( 1 <= month && month <=12) {
if(day>mdays[month-1] || day < 1 )
return false;
else
return true;
}
else
return false;
}
}
private boolean checkLeap(int year) {
if(year%400==0)
leap=true;
else if (year%100==0)
leap=false;
else if (year%4==0)
leap=true;
else
leap=false;
return leap;
}
private boolean flag;
private static boolean leap;
private int day,month,year;
private int daysCount;
int[] mdays={31,28,31,30,31,30,31,31,30,31,30,31};
int[] ldays={31,29,31,30,31,30,31,31,30,31,30,31};
}
Write a program to input two valid dates, each comprising of day (2 digits), Month (2
digits) and year (4 digits) and calculate the days elapsed between the two dates.
Test your program for the following data values:
i) FIRST DATE : DAY : 24
MONTH: 09
YEAR: 1960
SECONDDATE : DAY : 08
MONTH: 12
YEAR: 1852
ii) FIRST DATE : DAY : 10
MONTH: 01
YEAR: 1852
SECONDDATE : DAY : 16
MONTH: 10
YEAR: 1952
import java.util.*;
import java.io.*;
public class DaysElapsed {
public static void main(String[] args) throws IOException {
int day,month,year;
int day1,month1,year1;
boolean flag;
Scanner in = new Scanner(System.in);
DaysCalculation date = new DaysCalculation();
DaysCalculation date1 = new DaysCalculation();
do {
System.out.println("INPUT THE FIRST DATE:");
System.out.println("INPUT DAY:");
day = in.nextInt();
System.out.println("INPUT MONTH:");
month = in.nextInt();
System.out.println("INPUT YEAR:");
year = in.nextInt();
flag = date.checkDate(day,month,year);
if (flag == false)
System.out.println("INVALID,DATE,INPUT AGAIN");
} while(flag != true);
date.setDate(day,month,year);
do {
System.out.println("INPUT THE SECOND DATE:");
System.out.println("INPUT DAY:");
day1 = in.nextInt();
System.out.println("INPUT MONTH:");
month1 = in.nextInt();
System.out.println("INPUT YEAR:");
year1 = in.nextInt();
flag = date1.checkDate(day1,month1,year1);
if (flag == false)
System.out.println("INVALID,DATE,INPUT AGAIN");
} while(flag != true);
System.out.println("DAYS ELAPSED:"+date.countDays(day1,month1,year1));
}
}
class DaysCalculation {
public void setDate(int d, int m, int y ){
day = d;
month = m;
year = y;
}
public int countDays(int day1, int month1, int year1 ){
int daysCount = 0;
int y=0,y1=0;
boolean small = false;
if (year == year1)
if (month == month1)
if (day < day1)
small = true;
else
small = false;
else if ( month < month1)
small = true;
else
small = false;
else if( year < year1)
small = true;
else
small = false;
if (small == false) {
int td,tm,ty;
td = day;
day = day1;
day1 = td;
tm = month;
month = month1;
month1 = tm;
ty = year;
year = year1;
year1=ty;
}
if (checkLeap(year) == true) {
y += (ldays[month-1]-day);
for ( int i = month; i <=11;i++)
y +=ldays[i];
}
else {
y += (mdays[month-1]-day);
for ( int i = month; i <=11;i++)
y +=mdays[i];
}
if (checkLeap(year1) == true) {
y1 += day1-1;
for ( int i = 0; i <month1-1;i++)
y1 +=ldays[i];
}
else {
y1 += day1-1;
for ( int i = 0; i <month1-1;i++)
y1 +=mdays[i];
}
for ( int i = year+1; i <= year1-1; i++) {
if(checkLeap(i) == true)
daysCount +=366;
else
daysCount +=365;
}
if (year == year1)
if (checkLeap(year) == true)
daysCount = y1-(366-y);
else
daysCount = y1-(365-y);
else {
daysCount += (y1+y);
}
return daysCount;
}
public boolean checkDate(int day, int month, int year) {
if (checkLeap(year) == true) {
if( 1 <= month && month <=12) {
if(day>ldays[month-1] || day < 1 )
return false;
else
return true;
}
else
return false;
}
else {
if( 1 <= month && month <=12) {
if(day>mdays[month-1] || day < 1 )
return false;
else
return true;
}
else
return false;
}
}
private boolean checkLeap(int year) {
if(year%400==0)
leap=true;
else if (year%100==0)
leap=false;
else if (year%4==0)
leap=true;
else
leap=false;
return leap;
}
private boolean flag;
private static boolean leap;
private int day,month,year;
private int daysCount;
int[] mdays={31,28,31,30,31,30,31,31,30,31,30,31};
int[] ldays={31,29,31,30,31,30,31,31,30,31,30,31};
}
No comments:
Post a Comment