// The Date class contains a month, day, and year, // and methods to set and dis
ID: 3567182 • Letter: #
Question
// The Date class contains a month, day, and year,
// and methods to set and display the values.
// The month cannot be set to less than 1 or more than 12,
// and the day of the month cannot be set to less than 1
// or more than the number of days in that month.
// The demonstration program instantiates four Date objects and
// purposely assigns invalid values to some of the arguments;
// the class methods will correct the invalid values.
class Date
Declarations
private num month
private num day
private num year
public void setDate(num month, num da, num yr)
num HIGH_MONTH = 12
num HIGHEST_DAYS[HIGH_MONTH] =
31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31
if month > HIGH_MONTH then
month = HIGH_MONTH
else
if month < 1 then
month = 1
else
month = mo
endif
endif
if da > HIGHEST_DAYS[month - 1] then
day = HIGHEST_DAYS[month - 1]
else
if da < 1 then
day = 1
else
day = da
endif
year = yr
return
public void showDate()
output "Date: ", month,
Explanation / Answer
The only bug in the code is return statements at then end of functions setDate and getDate
As they both return void, they shouldn't have it.
Also while checking month validity, month=mo, but mo was not declared (but i guess its just a typo)
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.