Snow_Mac
Snow_Mac

Reputation: 5797

Homework: Java calculator dealing with dates -- Is my array correct?

I'm working on a Java assignment, where we take a date and do some calculations on it in various ways. Right now I'm working on a piece that will use my Array. Basically it's a list of the months by day. 0 for January 1st, 31 for Feburary 1, etc...

Does my array look correct based on these values?

Here's my array:

  private static int[] cumulDays = {0,31,59,90,120,151,181,212,243,273,304,334};

Java Assignment Doc for reference: http://www.cs.colostate.edu/~cs161/assignments/PA2/doc/MyUTC.html#cumulDays

cumulDays private static int[] cumulDays number of days from the beginning of the year to the beginning of a month (0 for Jan, 31 for Feb, ...). You will want to initialize this.

Upvotes: 0

Views: 372

Answers (2)

Yanick Rochon
Yanick Rochon

Reputation: 53531

Your array values are correct. However, as the assignment Javadoc state, "you will want to initialize this"; you need to take into account leap years. However, since you basically don't have that many array possibilities, you could simply create two static arrays

private static int[] CUMUL_DAYS = {0,31,59,90,120,151,181,212,243,273,304,334};
private static int[] CUMUL_DAYS_LEAP = {0,31,60,91,121,152,182,213,244,274,305,335};

and depending on if the year is leap or not, return one of the static array

Upvotes: 1

drewblaisdell
drewblaisdell

Reputation: 610

Yes, that is correct. His last value is 334 because the zero is given for January 1st (as zero days have passed at that point). You may consider entering a cumulative entry of value 365 at 12 for December 31st, but it depends on how you are manipulating your date-data.

Depending on the specifications of your homework, it may be more beneficial to use Java's built in Date class.

Upvotes: 1

Related Questions