Question 1
Design a program to accept a day number (between 1 and 366), year (in 4 digits) from the user
to generate and display the corresponding date. Also, accept ‘N’ (1 <= N <= 100) from the
user to compute and display the future date corresponding to ‘N’ days after the generated date.
Display an error message if the value of the day number, year and N are not within the limit or
not according to the condition specified.
Test your program with the following data and some random data:
Example 1
INPUT: DAY NUMBER: 255
YEAR: 2018
DATE AFTER (N DAYS): 22
OUTPUT: DATE: 12 TH SEPTEMBER, 2018
DATE AFTER 22 DAYS: 4 TH OCTOBER, 2018
Example 2
INPUT: DAY NUMBER: 360
YEAR: 2018
DATE AFTER (N DAYS): 45
OUTPUT: DATE: 26 TH DECEMBER, 2018
DATE AFTER 45 DAYS: 9 TH FEBRUARY, 2019
Example 3
INPUT: DAY NUMBER: 500
YEAR: 2018
DATE AFTER (N DAYS): 33
OUTPUT: DAY NUMBER OUT OF RANGE.
Example 4
INPUT: DAY NUMBER: 150
YEAR: 2018
DATE AFTER (N DAYS): 330
OUTPUT: DATE AFTER (N DAYS) OUT OF RANGE.
-----------------------------------------------------CODE-------------------------------------------------------
import java.util.*;
public class qstn_1
{
static int[] daycheck(int day_number, int year)
{
int dd[] = {31,28,31,30,31,30,31,31,30,31,30,31};
boolean leap = false;
if(year % 4 == 0)
{
if( year % 100 == 0)
{
// year is divisible by 400, hence the year is a leap year
if ( year % 400 == 0)
leap = true;
else
leap = false;
}
else
leap = true;
}
else
leap = false;
if(leap!=false)
dd[1] = 29;
int res = day_number;
int flag = 0;
int result [] = {0,0};
while(res>31)
{
res = res-dd[flag++];
}
result[0] = res;//date
result[1] = flag++;//month
return result;
}
static int[] after_check(int day,int day_after,int year,int month)
{
int dd[] = {31,28,31,30,31,30,31,31,30,31,30,31};
boolean leap = false;
if(year % 4 == 0)
{
if( year % 100 == 0)
{
// year is divisible by 400, hence the year is a leap year
if ( year % 400 == 0)
leap = true;
else
leap = false;
}
else
leap = true;
}
else
leap = false;
if(leap!=false)
dd[1] = 29;
int res = day;
int flag = month--;//as as array index starts from 0
int left = dd[flag]-day;
if(left==day_after)
day+=dd[flag];
else if(left<day_after)
{
day_after= day_after -left;
day = dd[flag++];// Giving the maximum day of the month
}
else if(left>day_after)
{
day +=day_after;// adding the after day to current day
day_after = 0;
}
int yc = 0;
//for change in year
if(flag==12)
{
flag=0;
yc = 1;
}
while(day_after!=0)
{
// if there is a month change so year has to change
if(flag==11&&day_after!=0)
{
flag=0;
yc = 1;
}
// when finally day after is less than the days in that particular month
if(day_after<dd[flag])
{
day = day_after;
day_after = 0;
}
//when day afer is exactly as number of days in month
else if(day_after==dd[flag])
{
day = day_after;
day_after = 0;
}
//when day after is greater than the number of days in month we add the days
//we subtract the day after from number of days in the month and also
//add the days for calculation in the current period.
else if(day_after>dd[flag])
{
day = day_after-dd[flag];
day_after-=dd[flag++];
}
}
if(yc==1)
year +=1;
else
year = year;
int result[] = new int[3];
result[0] = day;
result[1] = flag;// month
result[2] = year;
return result;
}
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
System.out.println("Enter the day number");
int n = sc.nextInt();
System.out.println("Enter the year");
int y = sc.nextInt();
System.out.println("Enter the date after (N days) ");
int a = sc.nextInt();
if(n<1 || n>366)
{
System.out.println("DAY NUMBER OUT OF RANGE");
System.exit(0);
}
if(a<1 || a>100)
{
System.out.println("DATE AFTER (N DAYS) OUT OF RANGE");
System.exit(0);
}
String year = Integer.toString(y);
int length = year.length();
if(length!=4)
{
System.out.println("INVALID YEAR");
System.exit(0);
}
int day[] = daycheck(n,y);
//System.out.println(day[0]);
int after[] =after_check(day[0],a,y,day[1]);
String mon1 = "",mon2 ="";
String month[] = {"JANUARY","FEBRUARY","MARCH","APRIL","MAY","JUNE","JULY","AUGUST","SEPTEMBER","OCTOBER","NOVEMBER","DECEMBER"};
if(day[0]>3)
{
mon1 = "TH";
}
if( after[0]>3)
{
mon2="TH";
}
if(day[0]==1)
{
mon1 = "ST";
}
if(after[0]==1)
{
mon2 = "ST";
}
if(day[0]==2)
{
mon1 = "ND";
}
if(after[0]==2)
{
mon2 = "ND";
}
if(day[0]==3)
{
mon1 = "RD";
}
if (after[0]==3)
{
mon2 = "RD";
}
// else
// {
// mon1 = "TH";
// mon2 = "TH";
// }
System.out.println(day[0]+" "+mon1+" "+month[day[1]]+","+y );
System.out.println(after[0]+" "+mon2+" "+month[after[1]]+","+after[2]);
}
}
No comments:
Post a Comment