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 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 for the following data and some
random data.
Example:
INPUT : DAY NUMBER : 233 YEAR : 2008 DATE AFTER(N) : 17
OUTPUT: 20TH AUGUST 2008 DATE AFTER 17 DAYS : 6TH SEPTEMBER 2008
INPUT : DAY NUMBER : 360 YEAR : 2008 DATE AFTER(N) : 45
OUTPUT: 25TH DECEMBER 2008 DATE AFTER 45 DAYS : 8TH FEBRUARY 2009
ISC Computer Science Practical 2009 Java Code
import java.util.*;
public class DayNumber {
public static void main(String[] args) {
int dayNumber,year;
int dayAfter;
boolean flag;
Scanner in = new Scanner(System.in);
DaysCalculation date = new DaysCalculation();
do {
System.out.println("INPUT THE DAY NUMBER:");
dayNumber = in.nextInt();
System.out.println("INPUT YEAR:");
year = in.nextInt();
System.out.println("INPUT THE VALUE OF N:");
dayAfter = in.nextInt();
flag = date.checkDate(dayNumber,year,dayAfter);
if (flag == false)
System.out.println("INVALID INPUTS:");
} while(flag != true);
date.setDate(dayNumber,year,dayAfter);
date.dateSpecifier();
in.close();
}
}
class DaysCalculation {
public void setDate(int dayNumber, int year, int dayAfter) {
this.dayNumber = dayNumber;
this.year = year;
this.dayAfter = dayAfter;
}
public boolean checkDate(int dayNumber, int year, int dayAfter) {
if ( dayNumber < 1 || dayNumber > 365 )
return false;
else if ( dayAfter < 1 || dayAfter > 100)
return false;
else if (String.valueOf(year).length() != 4)
return false;
else
return true;
}
public void dateSpecifier() {
int m = 0,k=1;
for(int i = 1; i <= dayNumber; i++) {
if( checkLeap(year) == true ? k > ldays[m] : k > mdays[m] ) {
k = 1;
m++;
}
k++;
}
String prefix;
prefix = (k-1)%10 == 1 ? "st" : (k-1)%10 == 2 ? "nd" : (k-1)%10 == 3 ? "rd" : "th";
System.out.println(k-1+prefix+" "+months[m]);
for (int i = 1; i <= dayAfter; i++) {
if( checkLeap(year) == true ? k > ldays[m] : k > mdays[m] ) {
k = 1;
m++;
if(m > 11) {
year++;
m = 0;
}
}
k++;
}
prefix = (k-1)%10 == 1 ? "st" : (k-1)%10 == 2 ? "nd" : (k-1)%10 == 3 ? "rd" : "th";
System.out.println("DATE AFTER "+dayAfter+" DAYS:"+(k-1)+""+prefix+" "+months[m]);
}
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 dayNumber,year;
private int dayAfter;
String[] months = {"January","Feburary","March","April","May","June","July","August","Sepetember","October","November","December"};
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};
}
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 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 for the following data and some
random data.
Example:
INPUT : DAY NUMBER : 233 YEAR : 2008 DATE AFTER(N) : 17
OUTPUT: 20TH AUGUST 2008 DATE AFTER 17 DAYS : 6TH SEPTEMBER 2008
INPUT : DAY NUMBER : 360 YEAR : 2008 DATE AFTER(N) : 45
OUTPUT: 25TH DECEMBER 2008 DATE AFTER 45 DAYS : 8TH FEBRUARY 2009
ISC Computer Science Practical 2009 Java Code
import java.util.*;
public class DayNumber {
public static void main(String[] args) {
int dayNumber,year;
int dayAfter;
boolean flag;
Scanner in = new Scanner(System.in);
DaysCalculation date = new DaysCalculation();
do {
System.out.println("INPUT THE DAY NUMBER:");
dayNumber = in.nextInt();
System.out.println("INPUT YEAR:");
year = in.nextInt();
System.out.println("INPUT THE VALUE OF N:");
dayAfter = in.nextInt();
flag = date.checkDate(dayNumber,year,dayAfter);
if (flag == false)
System.out.println("INVALID INPUTS:");
} while(flag != true);
date.setDate(dayNumber,year,dayAfter);
date.dateSpecifier();
in.close();
}
}
class DaysCalculation {
public void setDate(int dayNumber, int year, int dayAfter) {
this.dayNumber = dayNumber;
this.year = year;
this.dayAfter = dayAfter;
}
public boolean checkDate(int dayNumber, int year, int dayAfter) {
if ( dayNumber < 1 || dayNumber > 365 )
return false;
else if ( dayAfter < 1 || dayAfter > 100)
return false;
else if (String.valueOf(year).length() != 4)
return false;
else
return true;
}
public void dateSpecifier() {
int m = 0,k=1;
for(int i = 1; i <= dayNumber; i++) {
if( checkLeap(year) == true ? k > ldays[m] : k > mdays[m] ) {
k = 1;
m++;
}
k++;
}
String prefix;
prefix = (k-1)%10 == 1 ? "st" : (k-1)%10 == 2 ? "nd" : (k-1)%10 == 3 ? "rd" : "th";
System.out.println(k-1+prefix+" "+months[m]);
for (int i = 1; i <= dayAfter; i++) {
if( checkLeap(year) == true ? k > ldays[m] : k > mdays[m] ) {
k = 1;
m++;
if(m > 11) {
year++;
m = 0;
}
}
k++;
}
prefix = (k-1)%10 == 1 ? "st" : (k-1)%10 == 2 ? "nd" : (k-1)%10 == 3 ? "rd" : "th";
System.out.println("DATE AFTER "+dayAfter+" DAYS:"+(k-1)+""+prefix+" "+months[m]);
}
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 dayNumber,year;
private int dayAfter;
String[] months = {"January","Feburary","March","April","May","June","July","August","Sepetember","October","November","December"};
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};
}
bhaiya aapne mujhe bacha liya thank you;
ReplyDeleteChalo phir page share kar dena.
ReplyDelete