JAVA PROGRAMMING Below is the assignment. Please write the code, and the client
ID: 3729752 • Letter: J
Question
JAVA PROGRAMMING
Below is the assignment. Please write the code, and the client class (class that will run). Must produce EXACTLY the output attached below!!! The only thing that should change in the output is the last line, if I enter different dates into the Scanner.
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
Explanation / Answer
Below is the code for the problem .
Note : please ensure that it require JAVA 8 to run this program which is the latest stable version of java.
import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.Calendar;
import java.util.GregorianCalendar;
import java.util.Scanner;
import static java.time.temporal.ChronoUnit.DAYS;
public class Date {
int day;
int month;
int year;
LocalDate date;
/* Constructor to construct a Date object*/
public Date(int day, int month, int year){
//Creates a new Date object
this.day = day;
this.month = month;
this.year = year;
// Contruct LocalDate object for this object
LocalDate date = LocalDate.of(year,month,day);
System.out.println(date);
this.date = date;
}
/* Return the day of the Date object*/
public int getDay(){
return this.day;
}
/* Return the month of the Date object*/
public int getMonth(){
return this.month;
}
/* Return the year of the Date object*/
public int getYear(){
return this.year;
}
/* Add the days to current Date object*/
public void addDays(int days){
this.date.plusDays(days);
}
/* Returns days between current Date object and other date*/
public int daysTo(LocalDate other){
long daysB = DAYS.between(this.date, other);
System.out.println("");
int daysBetween = (int)daysB;
return daysBetween;
}
/* Checks if the Date object is leap year or not*/
public boolean isLeapYear(){
LocalDate date = LocalDate.of(this.year, this.month, this.day);
return date.isLeapYear();
}
/* Convert Date object to String */
public String toString(){
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("dd/MM/yyyy");
String formattedString = this.date.format(formatter);
return formattedString ;
}
public static void main(String[] args) {
Scanner s = new Scanner(System.in);
// Take input data for dates from user
System.out.println("Enter day of first date:");
int day1 = s.nextInt();
System.out.println("Enter Month of first date:");
int month1 = s.nextInt();
System.out.println("Enter Year of first date:");
int year1 = s.nextInt();
System.out.println("Enter day of second date:");
int day2 = s.nextInt();
System.out.println("Enter Month of second date:");
int month2 = s.nextInt();
System.out.println("Enter year of second date:");
int year2 = s.nextInt();
// Create Date objects with input values
Date date1 = new Date(day1,month1,year1);
Date date2 = new Date(day2,month2,year2);
// Calculate Days between the two dates
int days = date1.daysTo(date2.date);
System.out.println("There are "+days+" days between "+date1.toString()+" and "+date1.toString());
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.