Vous êtes sur la page 1sur 23

QUESTION 24

Design a class to accept a date from the user and


subtract n no. of days from it with the following details:Class Name-Date1
Data Members- d, m, y, n of integer type

Write the main function.

ALGORITHM
Step 1: Start
Step 2: Declaring class Date1
Step 3: Declaring d, m, y, c, n of integer type as data members
c
0
Step 4: Creating main() in order to create the object of class Date1 in
order to call the member functions
Printing enter the day, month and year
Accepting day, month and year in variables d,m,y
Printing enter the number of days to be subtracted
Accepting the number of days in the variable n
Declaring an integer array in a[]
a[] {0,31,28,31,30,31,30,31,31,30,31,30,31}
if(y%4==0&&y%100!=0)||(y%400==0)
a[2]
29
while c<n
d d-1
if d==0
m
m-1
if m==0
y
y-1
m
12
d a[m]

c
c+1
Printing the date is: "+d+"."+m+"."+y
End if
End if
End while
End of main()
Step 5: End

PROGRAM
import java.io.*;
class Date1
{
private static BufferedReaderbr=new BufferedReader(new
InputStreamReader(System.in));
public static void main(String args[])throws Exception
{
int d,m,y,c=0,n;
//declaring variable as integers
System.out.println("enter the day,month and year");
d=Integer.parseInt(br.readLine());
m=Integer.parseInt(br.readLine());
y=Integer.parseInt(br.readLine());
System.out.println("enter the number of days to be
subtracted");
n=Integer.parseInt(br.readLine());
int a[]={0,31,28,31,30,31,30,31,31,30,31,30,31}; //creating
aninteger array
if((y%4==0&&y%100!=0)||(y%400==0))
a[2]=29;

while(c<n)
{
d--;
if(d==0)
{
m--;
if(m==0)
{
y--;
m=12;
}
d=a[m];
}
c++;
}
System.out.println("the date is:"+d+"."+m+"."+y);
}
}

OUTPUT
enter the day,month and year
10
5
2013
enter the number of days to be subtracted
20
the date is: 20.4.2013

VARIABLE CHART
Variable

Datatype

Description

a[]

int

D
M

int
int

Y
N

int
int

int

used to store an
integer array
used to store the date
used to store the
month no.
used to store the year
used to store n no. of
days
used as a counter for
loop

QUESTION 25
Design a class to print the no. of days elapsed between
any two dates with the following details:
Class Name: Date3
Data Members:d, m, y, d1, m1, y1, sum, sum1, sum2
Write the main function.

ALGORITHM
Step 1: Start
Step 2: Declaring class difference
Step 3:Declaring d,m,y,i,d1,m1,y1,sum, sum1, sum2 and f of integer
type as data members
sum
0
sum1 0
sum2 0
f
0
Step 4: Creating main() for creation of object of class difference
in order to call the member function
Printing enter the day,month and year
Accepting the day,month and year in d,m,y
Declaring an integer array in a[]
a[] 0,31,28,31,30,31,30,31,31,30,31,30,31
if(y%4==0&&y%100!=0)||(y%400==0)
a[2] 29
f
1
for i=1;i<m;i++
sum sum+a[i]
sum
sum+d
printing enter the second date
Accepting the day, month and year in d1,m1,y1

a[2] 29
else
a[2]
28
for i=1;i<m1;i++
sum1
sum1+a[i]
sum1
sum1+d1
if y==y1
printing difference between two dates is: + (sum1-sum)
else
if f==1
sum 366-sum
else
sum
365-sum
if y+1==y1
printing difference between two dates is: "+(sum1sum)
else if y+1<y1
for i=y+1;i<y1;i++
if(i%4==0&&i%100!=0)||(i%400==0)
sum2
sum2+366
else
sum2
sum2+365
printing difference between two dates is:"+(sum2+sum+sum1)
End for
End else if
End else
End main()

Step 5: end

PROGRAM
import java.io.*;
class difference
{
private static BufferedReaderbr=new BufferedReader(new
InputStreamReader(System.in));
public static void main(String args[])throws Exception
{
int d,m,y,i,d1,m1,y1,sum=0,sum1=0,sum2=0,f=0;//declaring
variable as integers
System.out.println("enter the day,month and year");
d=Integer.parseInt(br.readLine());
m=Integer.parseInt(br.readLine());
y=Integer.parseInt(br.readLine());
int a[]={0,31,28,31,30,31,30,31,31,30,31,30,31};
if((y%4==0&&y%100!=0)||(y%400==0))//checking for leap year
{
a[2]=29;
f=1;
}
for(i=1;i<m;i++)
{

sum=sum+a[i];
sum=sum+d;
System.out.println("enter the second date");
d1=Integer.parseInt(br.readLine());//accepting date from the
user
m1=Integer.parseInt(br.readLine());//accepting month from
the user
y1=Integer.parseInt(br.readLine());//accepting year from the
user
if((y1%4==0&&y1%100!=0)||(y1%400==0)) //Checking for
leap year
a[2]=29;
else
a[2]=28;
for(i=1;i<m1;i++)
{
sum1=sum1+a[i];
sum1=sum1+d1;
if(y==y1)
System.out.println("difference between two dates is: "+
(sum1-sum));
else
{
if(f==1)
sum=366-sum;
else

sum=365-sum;
if(y+1==y1)
System.out.println("difference between two dates is:
"+(sum1-sum));
else if(y+1<y1)
{
for(i=y+1;i<y1;i++)
{
if((i%4==0&&i%100!=0)||(i%400==0))
sum2=sum2+366;
else
sum2=sum2+365;
}
System.out.println("difference between two dates
is:"+(sum2+sum+sum1)+days);//printing the difference of two
date
}
}
}
}

OUTPUT
enter the day,month and year
10
9
1993
enter the second date
30
6
1996
difference between two dates is: 1024 days

VARIABLE CHART
Variable
a[]

Datatype
int

D
M

int
int

Y
I

int
int

d1

int

m1

int

y1

int

Sum

int

sum1

int

sum 2

int

int

Description
used to store days of
month
to store the first date
to store the first
month no.
to store the first year
used as a counter for
loop
to store the second
date
used to store the
second month no.
used to store the
second year
used to store sum of
the days
used to store the sum
of the months
used to store
difference of years
used as counter for
loop

QUESTION 26
Design a classto accept the day no. and year and print
date, month,year and day of the week when the starting
day of the year isThursday with the following details:
Class Name:Date
Data Members:d, m, y, c, dd, n of integer type
Write the main() function

ALGORITHM
Step 1: Start
Step 2: Declaring class date
Step 3: Declaring d, m, y, c, dd,n of integer type as a data
members
m
0
c
1
Step 4: Creating main() for creation of object of class
date
in order to call the member function
Printing enter the day no. and year
Accepting day no. and year in variables d,y
Declaring an integer array in day[]
day[] 0,31,28,31,30,31,30,31,31,30,31,30,31
Declaring an integer array in st[]
st[]
"","January","February","March","April","May",
"June","July","August","September","October","
November","December"

Declaring an integer array in str[]


str[]
"Thursday","Friday","Saturday","Sunday","Mon
day","Tuesday","Wednesday"
if(y%4==0&&y%100!=0)||(y%400==0)
a[2]
29
while m<d
m m+day[c]
if(m<d)
c
c+1
End while
dd m-d
if dd>0
dd
day[c]-dd
else if dd==0
dd
day[c]
d
d-1
n
d%7
Printing the date is: + dd +" "+ st[c] +","+ y
Printing the day is: " + str[n]
Printing the date is: "+d+"."+m+"."+y

End of main()
Step 5: End

PROGRAM
import java.io.*;
class date
{
//opening class
private static BufferedReaderbr=new BufferedReader(new
InputStreamReader(System.in));
public static void main(String args[])throws Exception
{
int d,m=0,y,c=1,dd,n;
//declaring data members as integers
System.out.println("enter the day number and year");
d=Integer.parseInt(br.readLine());
y=Integer.parseInt(br.readLine());
int day[]={0,31,28,31,30,31,30,31,31,30,31,30,31};//creating
an integer array
String
st[]={"","January","February","March","April","May","June","July","Aug
ust","September","October","November","December"};
Stringstr[]={"Thursday","Friday","Saturday","Sunday","Monday","Tues
day","Wednesday"};
if((y%4==0&&y%100!=0)||(y%400==0))
//checking leap year
day[2]=29;
while(m<d)
{
m=m+day[c];

if(m<d)
c++;
}
dd=m-d;
if(dd>0)
dd=day[c]-dd;
else if(dd==0)
dd=day[c];
d--;
n=d%7;
System.out.println("the date is: "+ dd +" "+ st[c] +","+ y);
System.out.println("the day is: " + str[n]);
}
}

//closing class

OUTPUT
OUTPUT:
enter the day number and year
233
2013
the date is: 21 August,2013
the day is: Friday

VARIABLE CHART
Variable
day[]

Datatype
int

int

int

int

int

dd

int

int

st[]

String

str[]

String

Description
used to store no.
of days in each
month
used to store the
date
used to store the
month no.
used to store the
year
used as a counter
variable
used to store the
new date
used to store the
number of days
used to store the
month array
used to store the
week array

Vous aimerez peut-être aussi