(* pgm1-Date.cl - Kishen Maloor CIS, University of Delaware. This is an object oriented program which calculates the day for an entered date that is after January 1st, 1980, and displays it as the output to the user. The "Date" class contains the procedure that carries out this function. The use of the individual attributes and other secondary variables in this class have been defined. The main idea behind this implementation is to first calculate the total number of days between the entered date and the 1st of January, 1980. This number is divided by 7 to determine the number of weeks that the period corresponds to. The remainder after division would give us the # of extra days to be counted after the day that corresponds to the 1st of January, 1980 to reach the exact day for the entered date. This result is displayed for the user. *) -- the Date class class Date inherits IO { d : Int; -- entered date m : Int; -- entered month y : Int; -- entered year yd : Int; -- number of yrs in between y and 1980 c : Int; -- counter nduly : Int; -- number of days in between entered date and 111980 nw : Int; -- number of weeks between 111980 and dmy lw : Int; -- number of extra days in the last week fstd : Int; -- day that corresponds to 111980 leap : Int; -- leap year count m1 : Int <- 1; -- months 1 to 12 m2 : Int <- 2; m3 : Int <- 3; m4 : Int <- 4; m5 : Int <- 5; m6 : Int <- 6; m7 : Int <- 7; m8 : Int <- 8; m9 : Int <- 9; m10 : Int <- 10; m11 : Int <- 11; m12 : Int <- 12; -- for initializing the Date class init(dd : Int,mm : Int,yy : Int) : Date { { d <- dd; m <- mm; y <- yy; self; } }; -- for calculating the number of days in between dmy and 111980 noduly() : Int { { c <- 1980; yd <- y - c; nduly <- 0; leap <- 4; while c <= (y-1) loop { if leap = 4 then { nduly <- nduly + 1; } else if leap = 0 then { nduly <- nduly + 1; leap <- 4; } else { nduly <- nduly + 0; } fi fi; leap <- leap - 1; nduly <- nduly + 365; c <- c+1; } pool; if leap = 4 then { if 2 < m then { nduly <- nduly + 1;} else { nduly <- nduly+0;} fi; } else if leap = 0 then { if 2 < m then { nduly <- nduly + 1;} else { nduly <- nduly+0;} fi; } else { nduly <- nduly + 0; } fi fi; c <- 1; -- counter while c <= (m-1) loop { if c = m1 then { nduly <- nduly + 31; } else if c = m2 then { nduly <- nduly + 28; } else if c = m3 then { nduly <- nduly + 31; } else if c = m4 then { nduly <- nduly + 30; } else if c = m5 then { nduly <- nduly + 31; } else if c = m6 then { nduly <- nduly + 30; } else if c = m7 then { nduly <- nduly + 31; } else if c = m8 then { nduly <- nduly + 31; } else if c = m9 then { nduly <- nduly + 30; } else if c = m10 then { nduly <- nduly + 31; } else if c = m11 then { nduly <- nduly + 30; } else { nduly <- nduly + 31; } fi fi fi fi fi fi fi fi fi fi fi; c <- c+1; } pool; nduly <- nduly+d; nduly; } }; -- to determine and display the actual day actdat() : SELF_TYPE { { fstd <- 2; nw <- nduly / 7; nw <- nw*7; lw <- nduly - nw; fstd <- fstd + lw; if fstd <= 7 then { fstd <- fstd + 0; } else { fstd <- fstd - 7; } fi; if fstd=1 then { out_string("Sunday"); } else if fstd=2 then { out_string("Monday"); } else if fstd=3 then { out_string("Tuesday"); } else if fstd=4 then { out_string("Wednesday"); } else if fstd=5 then { out_string("Thursday"); } else if fstd=6 then { out_string("Friday"); } else { out_string("Saturday"); } fi fi fi fi fi fi; } }; -- higher level routine that calls lower level routines to perform the subtasks compute() : SELF_TYPE { { noduly(); actdat(); } }; }; -- the main class class Main inherits IO { -- variables to hold the input date d : Int; m : Int; y : Int; obj : Date; -- for the object of the Date class -- main function main() : Object { { out_string("Enter any date on or after 1st Jan, 1980.\nThe corresponding day will be displayed.\n"); out_string("dd : "); d <- in_int(); out_string("mm : "); m <- in_int(); out_string("yyyy : "); y <- in_int(); obj <- new Date; obj.init(d,m,y); obj.compute(); } }; };