Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Java Create a Date class with three integer instance variables named day, month,

ID: 3854169 • Letter: J

Question

Java

Create a Date class with three integer instance variables named day, month, year. It has a constructor with three parameters for initializing the instance variables, and it has one method named daysSinceJan1().

Just as its identifier suggests, the daysSinceJan1() method computes and returns the number of days since January 1 of the same year, including January 1 and the day in the Date object itself.

Create a DateDemo class that tests the Date class. First prompt the user to enter an integer representing the day, a second prompt to enter an integer representing the month and a third to enter an integer representing the year. Do not worry about doing any validation of the values entered. Then, instantiate your Date object with these values. After that, call the method DaysSinceJan1() to display the answer.

Explanation / Answer

import java.util.Scanner;
class date
{
void daysSinceJan1(int d1,int m1,int y1) //function
{
int arr[ ]={0,31,28,31,30,31,30,31,31,30,31,30,31};
int d,m,y,s,s1,j,x;
s=0;
s1=0;
d=1; //predfeinde date of jan 1
m=1;
y=y1; //year same with given year
for(j=1;j<m;j++)
s=s+arr[j];
if(y%4==0) s=s+1;
s=s+d;
for(j=0;j<m1;j++)
s1=s1+arr[j];
if(y1%4==0)
s1=s1+1;
s1=s1+d1;
x=s1-s; //difference between date
System.out.print("Total number of days between : " + x); //printing the difference
}
public static void main(String arg[])
{
int day,year,month;
Scanner br =new Scanner(System.in);//accepting the user input
System.out.println("Enter Date day : ");
day=br.nextInt( );
System.out.println("Enter Date month : ");
year=br.nextInt( );
System.out.println("Enter Date year : ");
month=br.nextInt( );
date obj=new date(); //creating object of the class
obj.daysSinceJan1(day,year,month); //calling the fucntoon
}
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote