Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

// The Date class contains a month, day, and year, and methods to set // and dis

ID: 3634551 • 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 Dates and purposely uses invalid values as some
// of the arguments; the class methods will correct the invalid values.
class Date
Fields
private num month
private num day
private num year
Methods
public void setDate(num mo, num da, num yr)
num HIGH_MONTH = 12
num HIGHEST_DAYS[HIGH_MONTH) =
31, 29, 31, 20, 31, 30, 31, 31, 30, 31, 30, 31
if month > HIGH_MONTH then
month = HIGH_MONTH
else
if mo < 1 then
month = 1
else
mo = month
endif
endif
if da > HIGHEST_DAYS[month] then
day = HIGHEST_DAYS[month]
else
if da < 1 then
da = 1
else
day = da
endif
yr = year
return

public void showDate()
output "Date: ", month, “/”, da, “/”, year
return

start
Declarations
Date birthday
Date anniversary
date graduation
Datte party
birthday.setTheDate(6, 24, 1982)
anniversary.setDate(10, 15, 2009)
graduation.setDate(14, 19, 2011)
party.setDate(7, 35, 2012)
output "Birthday "
birthday.showDate()
output "Anniversary "
anniversary.showDate()
output "Graduation "
graduation.showDate()
output "Party "
party.showDate()
stop

Explanation / Answer

Dear...

Given code shows that, it is trying to validate the input month and correct bad values of month dates and more over, HIGH_MONTH is assigned to Month. Here 1 is assigned to Month and assign month (with a little "m") to mo. It is not suggested that assign month (with a little "m") to mo.

Correct code:

class Date
privatenum month
privatenum day
privatenum year
public void setDate(nummo, num da, numyr)
num HIGH_MONTH = 12
num HIGHEST_DAYS[HIGH_MONTH] =
31, 29, 31, 20, 31, 30, 31, 31, 30, 31, 30, 31
if month >HIGH_MONTH then
month = HIGH_MONTH
else
ifmo< 1 then
month = 1
else
mo = month
endif
endif

if month > HIGH_MONTH then
month = HIGH_MONTH
else
if mo < 1 then
month = 1
else
mo = month
endif
endif
public void showDate()
print “Date: “, month, “/”, da, “/”, year
return