Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Java. Modify the Date class. It should have the following methods: public Date(int day, int month, int year) -- Constructs a new Date object to

Java. Modify the Date class. It should have the following methods:

public Date(int day, int month, int year) -- Constructs a new Date object to represent the given date

public int getDay() -- returns the day value of this date

public int getMonth() -- returns the month value of this date

public int getYear() -- returns the year value of this date

public void addDays(int days) -- Moves the Date object forward in time by the given number of days

public int daysTo(Date other) -- Returns the number of days between this Date and the other Date

public boolean isLeapYear() -- Returns true if the year of this Date is a leap year. A leap year occurs when the value of the year is divisible by 4, except when it is divisible by 100 and not 400 (so 1700, 1800, and 1900 are not leap years)

public String toString() -- returns a String representation of this date in the form day/month/year

=====

Date class

import java.io.*;

public class Date {

// Declaring instance variables

private int day;

private int month;

private int year;

// Parameterized constructor

public Date(int day, int month, int year) {

this.day = day;

this.month = month;

this.year = year;

}

// getters and setters

public int getDay() {

return day;

}

public void setDay(int day) {

this.day = day;

}

public int getMonth() {

return month;

}

public void setMonth(int month) {

this.month = month;

}

public int getYear() {

return year;

}

public void setYear(int year) {

this.year = year;

}

// This method will calculate the difference between two dates

public int daysTo(Date d) {

int months[] = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };

int m1=0,m2=0;

int nod1 = getYear(); //copy year into nod1

int y1=nod1*365; //get all days of all years

int nod2 =getMonth();

for(int i=0;i

m1=m1+months[i]; // get all days of all months

int nod3=getDay();

int total1=nod3+m1+y1; //calculate total days in that date

int nod4 =d.getYear();

int y2=nod4*365; //get all days of all years

int nod5 = d.getMonth();

for(int i=0;i

m2=m2+months[i]; // get all days of all months

int nod6=d.getDay();

int total2=y2+m2+nod6; //calculate total days in that date

int diff = 0;

int leapy=0;

if (nod1 > nod4) //check which date is maximum to find difference

{

diff =total1-total2; //calculate difference between to dates

leapy=leapYears(nod1,nod4); // calling leapYears() method to get number of leap days between two dates

//without leap adding leap year days

}

else if (nod1 < nod4) //calculate difference between to dates

{

diff = total2-total1; // calling leapYears() method to get number of leap days between two dates

//without leap adding leap year days

leapy=leapYears(nod1,nod4);

}

else{

if(nod2>nod5){

diff =total1-total2; //calculate difference between to dates

//leapy=leapYears(nod1,nod4); // calling leapYears() method to get number of leap days between two dates

//without leap adding leap year days

//return diff;

}

else{

diff =total2-total1; //calculate difference between to dates

//leapy=leapYears(nod4,nod1); // calling leapYears() method to get number of leap days between two dates

//without leap adding leap year days

//return diff;

}

}

return leapy+diff; // returns total diff days + total leap year days (which is 29th-Feb)

}

// This method will check how many leap years

public int leapYears(int n1,int n2) // this method return number of leap years between two dates

// year values from two dates passed as parameter

{

int a=n2;

int b=n1;

int n=0;

if(a>=b)

{

while(a>=b) // checking one by one leap year

// if first year value is greater than other then condition checked as.....

{

if ((a % 100 == 0 && a % 400 == 0) || (a % 100 != 0 && a % 4 == 0))

n++; // counting leap years

a--;

}

}else

{

while(b>a) // if first year value is greater than other then condition checked as.....

if ((b % 100 == 0 && b % 400 == 0)|| (b % 100 != 0 && b % 4 == 0))

n++;

b--;

}

return n;

}

// toString() method is used to display the contents of an object

@Override

public String toString() {

return day + "/" + month + "/" + year;

}

}

Test class

import java.util.*; public class Test {

public static void main(String[] args) {

//Declaring variables

int month1,day1,year1,month2,day2,year2;

/*

* Creating an Scanner class object which is used to get the inputs

* entered by the user

*/

Scanner sc = new Scanner(System.in);

//Getting the input entered by the user

System.out.print("Enter day of first date:");

day1=sc.nextInt();

System.out.print("Enter month of first date:");

month1=sc.nextInt();

System.out.print("Enter year of first date:");

year1=sc.nextInt();

//Creating an Instance of Date class

Date d1=new Date(day1, month1, year1);

//Getting the input entered by the user

System.out.print("Enter day of second date:");

day2=sc.nextInt();

System.out.print("Enter month of second date:");

month2=sc.nextInt();

System.out.print("Enter year of second date:");

year2=sc.nextInt();

//Creating an Instance of Date class

Date d2=new Date(day2, month2, year2);

//Displaying the difference between two dates

System.out.println("There are "+d1.daysTo(d2)+" days between "+d1.toString()+" and "+d2.toString());

}

}

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image_2

Step: 3

blur-text-image_3

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Essentials of Database Management

Authors: Jeffrey A. Hoffer, Heikki Topi, Ramesh Venkataraman

1st edition

133405680, 9780133547702 , 978-0133405682

More Books

Students also viewed these Databases questions