Question: Java. You need to fix the code, so it provides you the right calculation of the days between two dates. For example, there are 73075
Java. You need to fix the code, so it provides you the right calculation of the days between two dates. For example, there are 73075 days between 19/1/2197 and 16/2/2397
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 nod1 = getYear() * 365 + 1;
for (int i = 0; i
nod1 += months[i];
}
Date d1 = new Date(getDay(), getMonth(), getYear());
nod1 += noOfLeapYears(d1)-getDay();
int nod2 = d.getYear() * 365 + 1;
for (int i = 0; i
nod2 += months[i];
}
nod2 += noOfLeapYears(d)-d.getDay();
int diffDays = 0;
if (nod1 > nod2)
diffDays = nod1 - nod2;
else if (nod1
diffDays = nod2 - nod1;
return diffDays;
}
private int noOfLeapYears(Date d) {
int noOfYears = d.getYear();
if (d.getMonth()
noOfYears = noOfYears - 1;
}
return (noOfYears / 4) - (noOfYears / 100) + (noOfYears / 400);
}
// This method will check whether the year is leap year or not
public boolean isLeapYear() {
if ((year % 100 == 0 && year % 400 == 0)
|| (year % 100 != 0 && year % 4 == 0))
return true;
else
return false;
}
// toString() method is used to display the contents of an object
@Override
public String toString() {
return day + "/" + month + "/" + year;
}
}
___________________
Test.java
import java.util.Scanner;
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());
}
}
Write a class called Date that represents a date consisting of a day, month, and year. A date object 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 Write a client program in which to test your class. This program should prompt the user to enter the days, months, and years of two separate dates, and then print out the number of days between these two dates. The following is one run of your program and their expected output (user input is bold and underlined): Enter day of first date:19 Enter month of first date:1 Enter year of first date:2197 Enter day of second date:16 Enter month of second date:2 Enter year of second date:2397 There are 73075 days between 19/1/2197 and 16/2/2397
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
