logo CodeStepByStep logo

Date-absoluteDay

Language/Type: Java

Write a method named absoluteDay that will be placed in the Date class. The method should return the "absolute day of the year" between 1 and 365 represented by the Date object. January 1 is absolute day #1, January 2 is day #2, ..., and December 31st is absolute day #365. For example, calling this method on a date representing February 13th should return 44, and calling it on a Date representing September 19th should return 263. The table below summarizes the absolute days for various dates throughout the year.

Date (month/day)1/11/21/3...1/301/312/12/2...2/283/13/2...12/3012/31
Absolute day123...30313233...596061...364365

The following table shows the results of calling your method on several Date objects.

Object Call Returns
Date jan1 = new Date(1, 1); jan1.absoluteDay() 1
Date jan4 = new Date(1, 4); jan4.absoluteDay() 4
Date feb1 = new Date(2, 1); feb1.absoluteDay() 32
Date mar10 = new Date(3, 10); mar10.absoluteDay() 69
Date sep19 = new Date(9, 19); sep19.absoluteDay() 262
Date dec31 = new Date(12, 31); dec31.absoluteDay() 365

You should not solve this problem by writing 12 if statements, one for each month; this is redundant and poor style.

Recall that your method is allowed to call other methods on Date objects or construct other objects if you like.

Partial class: Write code that will become part of an existing class as described. You do not need to write the complete class, just the portion described in the exercise.

You must log in before you can solve this problem.

Log In

Need help?

Stuck on an exercise? Contact your TA or instructor.

If something seems wrong with our site, please

Is there a problem? Contact us.