logo Practice-It logo


Language/Type: Java classes instance methods
Author: Marty Stepp

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. If you solve the problem that way, you will receive at most half credit. Your method also should not modify the state of the fields of the Date object on which it was called. A solution that does so will receive at most half credit.

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

Type your solution here:

This is a partial class problem. Submit code that will become part of an existing Java class as described. You do not need to write the complete class, just the portion described in the problem.

You must log in before you can solve this problem.

Log In

If you do not understand how to solve a problem or why your solution doesn't work, please contact your TA or instructor.
If something seems wrong with the site (errors, slow performance, incorrect problems/tests, etc.), please

Is there a problem? Contact a site administrator.