BONUS (4 marks: all or nothing)

advertisement
Name: ______________________ Score: ______ / 31
Computer Science 20S Honours / Kozub
Lab 5: Structures
For all problems, use the new complete design recipe for structures (including templates). For any
functions you define, include the usual design recipe with a contract, purpose, examples, etc.
1. Provide a structure definition and template for a cow with name, weight and color properties. (2
marks for the definition and template)
a) Develop a function paintCow that consumes a cow and a color and produces a cow with the new
color. (3 marks)
b) Develop a function milkCow that consumes a cow and produces a cow that weighs 10 pounds
less. (3 marks)
2. Provide a structure definition and template for a person with a first name, a height (in inches), a
weight (in pounds), a gender and an age. (2 marks)
a) Develop a function calculateBMI that inputs a person and produces the BMI index of that
person. The formula for BMI index is: 703 x weight ÷ height ÷ height. (3 marks)
b) Develop a function idealWeight that inputs a person and produces the idealWeight for that
person. For men, the formula is 115 pounds + 4.2 pounds per inch over 5 feet.
If a man is less than 5 feet, then make the idealWeight 115 pounds.
For women, the formula is 108 pounds + 3.75 pounds per inch over 5 feet.
If a woman is less than 5 feet, then make the idealWeight 108 pounds.
Hint: use cond to see what gender the person is. (4 marks)
3.
Provide a structure definition and template for a structure called date with properties of day,
month and year. (2 marks) Then:
a) Develop the function isLeapYear? that consumes a date and produces true or false depending
on whether the year is a leap year. Use these steps to determine leap years: (4 marks)
Step 1: A year is a leap year if it is divisible by 400 (e.g. 2000 was a leap year)
Step 2: A year is not a leap year if it is divisible by 100 (e.g. 1900 was not a leap year)
Step 3: A year is a leap year if it is divisible by 4 (e.g. 2012 is a leap year)
(isLeapYear? (make-date 15 10 2005)) “sb false”
(isLeapYear? (make-date 15 10 2004)) “sb true”
You have to use a cond to check these steps in the order described above. Some of it may seem
contradictory. See here if you want more information about leap years:
https://www.mathsisfun.com/leap-years.html
b) Develop the function numDaysInMonth that consumes a date and produces the number of days
in that month. Be sure to take into account leap years. (Hint: Use isLeapYear? as a helper
function) (4 marks)
(numDaysInMonth (make-date 15 10 2005))”sb 31”
(numDaysInMonth (make-date 1 2 2004))”sb 29”
c) Develop the function validDate? that consumes a date and produces true or false depending on
whether the date is valid or not. For example: (4 marks)
(validDate? (make-date 31 6 2002)) “should be false”
(validDate? (make-date 29 2 2000)) “should be true”
(validDate? (make-date 29 2 1900)) “should be false”
BONUS (4 marks: all or nothing)
This question must be entirely correct to be awarded any marks. You must include a contract, purpose,
tests and examples. A partial answer will not be given any marks.
d) Develop the function tomorrowsDate that consumes a date and produces a date representing
tomorrow’s date. For example:
(tomorrowsDate (make-date 15 10 2005))”sb (make-date 16 10 2005)”
(tomorrowsDate (make-date 31 10 2005))”sb (make-date 1 11 2005)”
(tomorrowsDate (make-date 28 2 2005))”sb (make-date 1 3 2005)”
(tomorrowsDate (make-date 28 2 2004))”sb (make-date 29 2 2004)”
(tomorrowsDate (make-date 31 12 2005))”sb (make-date 1 1 2006)”
Labs handed in at an exceptionally late time will not be accepted.
Download