I already wrote all the methods except I'm having a problem with public int advanceTo(Date endDay) which modifies the state of this Date object by advancing it to the given end Date and returns the number of days it took to advance this Date object to the given end Date. I'm guessing public boolean equals(Date d) is also incorrect as I think it should compare a complete date not just the day.
Anyways, my question comes from the Dataclient.java file (the 2nd pice of code here). I'm trying to create a new object from the input provided by the user but once it runs the "new Date" line, it runs an object with 0/0/0. Can someone explain to me what I did wrong or what I'm missing?
Thank you in advance!
Date.java
//This program tells you what day of the week you were born, and how old you are in days by
//collecting today's date and your birthday. It will also let you know if you were born on a leap year.
public class Date {
private int year;
private int month;
private int day;
public Date (int year, int month, int day) {
this.year = getYear();
this.month = getMonth();
this.day = getDay();
}
public int getYear() {
return year;
}
public int getMonth() {
return month;
}
public int getDay() {
return day;
}
public String toString() {
return year+ "/" + month + "/" + day;
}
public boolean equals(Date d) {
return d.equals(year + " " + month + " " + day);
}
public boolean isLeapYear() {
if(year%4 == 0) {
if(year%100 == 0 && !(year%400 == 0)) {
return true;
}else if(year%100 == 0) {
return false;
}
}
return true;
}
public int getDaysInMonth() {
if(month==1||month==3||month==5||month==7||month==8||month==10||month==12) {
return 31;
}else if(month==4||month==6||month==9||month==11) {
return 30;
}else if(month==2 && isLeapYear()) {
return 29;
}else {
return 28;
}
}
public void nextDay() {
if(!equals(getDaysInMonth())) {
if(month==12) {
year += 1;
month += 1;
day += 1;
}else {
month += 1;
day += 1;
}
}else {
day += 1;
}
}
public int advanceTo(Date endDay) {
if (getDay() > endDay) {
//fix this method to use today's date instead of day...
}
}
public String getDayOfWeek() {
int count = 0;
for (int i=1753; i<=year; i++) {
if(i%4==0 && i%100==0 && i%400==0) {
count++;
}
}
int dayChange = ((year - 1753) * 365 + count + month - 1 + day - 1)%7;
if(dayChange==1) {
return "Tuesday";
}else if(dayChange==2) {
return "Wednesday";
}else if(dayChange==3) {
return "Thursday";
}else if(dayChange==4) {
return "Friday";
}else if(dayChange==5) {
return "Saturday";
}else if(dayChange==6) {
return "Sunday";
}else {
return "Monday";
}
}
}
DateClient.java
import java.util.*;
public class DateClient {
public static void main(String[] args) {
Scanner console = new Scanner(System.in);
giveIntro();
System.out.print("What is today's date (month day year)? ");
int month = console.nextInt();
System.out.print(month + " ");
int day = console.nextInt();
System.out.print(day + " ");
int year = console.nextInt();
System.out.print(year + " ");
Date today = new Date(year, month, day); // object isn't built, prints 0/0/0
System.out.println(today);
System.out.print("What is your birthday (month day year)? ");
month = console.nextInt();
day = console.nextInt();
year = console.nextInt();
Date birthday = new Date(year, month, day);
System.out.print(today);
//System.out.println(", which was a " + getDayOfWeek());
}
public static void giveIntro() {
System.out.println("This program tells you what day of the week you were born on,");
System.out.println("how old you are in days, and if you were born on a leap year.");
System.out.println();
}

New Topic/Question
Reply



MultiQuote






|