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

This program focuses on classes and objects. Turn in two files named DateClient.

ID: 3626699 • Letter: T

Question



This program focuses on classes and objects. Turn in two files named
DateClient.java and Date.java.
None of the methods listed above should print any output to the console. You
may not use any of Java's date-related classes such as java.util.Date or
java.util.GregorianCalendar
Begin by prompting the user for today's date and for his/her birthday in month
day year format (see examples below).
Use this information to print the user's birthday in year/month/day format,
the day of the week the user was born, and how old the user is in days. If the
user was born in a leap year, you print an additional message letting them
know their birth year was a leap year.

DateClient.java is a testing program; it should call all of your Date methods to
test them in a very exhaustive way to try all cases and combinations.
Development Strategy and Hints:
? Write the constructor and getYear, getMonth, and getDay and methods first.
? Then implement toString, equals, and isLeapYear.
? Next, try to write nextDay. (You may wish to write a helping method that
returns the number of days in this Date's month, to help you implement
nextDay. If you decide to write this method, please remember that if it is a
leap year, February will have 29 days.)
? Next, write advanceTo. You should be able to use methods you have already
written to write this.
? Lastly, write getDayOfWeek. You might want to construct a Date object for
January 1, 1753 to help you. Again, you should use the other methods you
have already written to help you write this method. Remember that the Date
represented by the object should be the same as it was after a call to
getDayOfWeek (this is different from nextDay and advanceTo which modify
the state of the object).
DateClient.java is a testing program; it should call all of your Date methods to
test them in a very exhaustive way to try all cases and combinations.
Development Strategy and Hints:
? Write the constructor and getYear, getMonth, and getDay and methods first.
? Then implement toString, equals, and isLeapYear.
? Next, try to write nextDay. (You may wish to write a helping method that
returns the number of days in this Date's month, to help you implement
nextDay. If you decide to write this method, please remember that if it is a
leap year, February will have 29 days.)
? Next, write advanceTo. You should be able to use methods you have already
written to write this.
? Lastly, write getDayOfWeek. You might want to construct a Date object for
January 1, 1753 to help you. Again, you should use the other methods you
have already written to help you write this method. Remember that the Date
represented by the object should be the same as it was after a call to
getDayOfWeek (this is different from nextDay and advanceTo which modify
the state of the object).

Explanation / Answer

please rate - thanks


public class Date
{private int dMonth;    //variable to store the month
private int dDay;      //variable to store the day
private int dYear;     //variable to store the year
public Date()
    {
          }

public Date(int m, int d, int y)
    {dMonth=m;
    dDay=d;
     dYear=y;

          }
public int getMonth()
    {return dMonth;
    }
public int getDay()
    {return dDay;
    }
    public int getYear()
    {return dYear;
    }
public void setYear(int y)
    {dYear=y;
    }
   public String toString()
    {return (dYear + "/" + dMonth + "/" + dDay);
    }
public int daysAlive(Date a)
{int doy,doy2,days,j;
doy=todoy(dMonth,dDay,dYear);       //get day of year for 1st date
    doy2=todoy(a.dMonth,a.dDay,a.dYear);   // get day of year for second date
    days=0;
    switch(a.dYear-dYear)               // same year?
    {case 0: days=doy2-doy;          // if same year get days apart
              break;
    default: for(j=dYear+1;j<=a.dYear-1;j++)    //>1 year apart add 365 or 366 for each year
                days=days+365+leap(j);
    case 1: days=365+leap(dYear)-doy+doy2+days;    // if 1 year apart get difference
              break;
   
    }
return days;
}
public int todoy(int month,int day,int year)
{int daysinmonth;
int days=0,i;
             
for (i=1;i<month;i++)       //go up to that month add how many days in the month
    { switch(i)
         {
         case 9:
         case 4:
         case 6:
         case 11:daysinmonth=30;
                 break;
         case 2: daysinmonth=28 + leap(year);    //Feb has 28 or 29 days
                 break;
         default:daysinmonth=31;
         }
      days+=daysinmonth;               //and count how many days passed
     }
days+=day;                           //add to the day wanted
return days;   
}
public int leap(int year)
{
if((year%4==0&&year%100!=0)|| year%400==0)
             return 1;
          else
              return 0;
}
String getDayofWeek()
{int m,q,j,k,h;
String dow[]={"Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday"};
m=dMonth;
k=dYear;
q=dDay;
if(m<3)
    {m+=12;
    k--;
    }
j=k/100;
k%=100;
h=(q+26*(m+1)/10 +k +k/4+j/4+5*j)%7;
h--;
return dow[h];
}
}

----------------------------------------------------------------

import java.util.*;
public class DateClient
{public static void main(String[] args)
{
int m,d,y;
Scanner in=new Scanner(System.in);

System.out.print("What is today's date (month day year)? ");
m=in.nextInt();
d=in.nextInt();
y=in.nextInt();
Date today=new Date(m,d,y);
System.out.print("What is your birthday (month day year)? ");
m=in.nextInt();
d=in.nextInt();
y=in.nextInt();
Date bday=new Date(m,d,y);
System.out.println("You were born on "+bday.toString()+", which was a "+bday.getDayofWeek()+".");
if(bday.leap(bday.getYear())==1)
    System.out.println( bday.getYear()+" was a leap year.");
System.out.println("You are "+bday.daysAlive(today) +" days old.");




}
}

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