9 Miniproject 1
(1 activity)
9.1 Extend the "day-span" procedure to handle dates in different years.(1 step)
9.1.1 (Display page) Here are the miniproject details.

Background

The "Difference between Dates" case study dealt with the problem of finding the number of days between two dates in the same year. For this assignment, you will solve a more general problem, that of finding the number of days between two dates in different years. (You can use the code you write to find out how many days old you are.)

Problem

Write a procedure century-day-span that takes two dates as arguments, and returns the number of days between them, including the argument dates themselves. Assume that the first argument date is earlier than the second. Each date is a three-word that represents a legal date between January 1, 1900 and December 31, 2999. The first word is a month name (one of january, february, ..., december). The second is an integer between 1 and the number of days in the specified month, inclusive. The third is an integer between 1900 and 2999, inclusive; it represents a year in the 20th, 21st, 22nd, 23rd, 24th, 25th, 26th, 27th, 28th, 29th, or 30th century. Your procedure must be able to deal with leap years. A leap year between 1900 and 2999 is any year that's divisible by 400, or by 4 and not by 100. (Thus 2000 was a leap year and 1900 was not.) A leap year's February has 29 days rather than 28, and therefore it has 366 days rather than 365.

Examples

The table below lists some sample calls to century-day-span and the desired return values.
expressiondesired return value
(century-day-span '(january 3 1990) '(january 9 1990))7
(century-day-span '(march 30 1989) '(february 2 1990))310
(century-day-span '(january 1 1984) '(january 1 1985))367
(century-day-span '(january 1 2001) '(january 1 2005))1462

Miscellaneous requirements

You should write helper procedures and call them from century-day-span, rather than doing all the computation in century-day-span itself. Restrict your use of Scheme to material covered in Simply Scheme chapters 3 through 6. Test your century-day-span procedure at least on the following:
  • dates in the same month;
  • dates in the same year in different months;
  • dates in different years, where the month of the first date precedes the month of the second date;
  • dates in different years, where the month of the first date follows the month of the second date;
  • dates in different years that do not span a "leap day";
  • dates in different years that span a "leap day";
  • dates that span the leap day in the year 2000;
  • dates that span the non-leap day in the year 1900;
  • dates in different years with the first date in a leap year;
  • dates in different years with the second date in a leap year;
  • dates in different years with both dates in a leap year.
(Note that these are not guaranteed to reveal all your bugs.) Test your helper procedures individually. Put your century-day-span procedure and its helper procedures in a file named c.day-span.scm in your mp1 directory. ("mp" stands for "miniproject". You'll need to create this directory.) Include your tests for the century-day-span proceedure in the same file, at the end, surrounded by multi-line comments. Please add comments for each test. If you don't want to do the math to test your code, you can go here instead. However, you will need to add 1 to every answer it gives you. Submit you file electronically in the usual way (i.e., within unix enter the mp1 directory, perhaps via cd ~/mp1, and then type submit mp1). You have longer to work on this assignment than a regular homework assignment. Check with your TA or the course website for the due date.
10 More on the "Difference between Dates" case study
(4 activities)
10.1 Quiz: Difference Between Dates(1 step)
10.1.1 (Student Assessment) Try some difference between dates helper procedures
1.Fill in the blank to complete the following alternative definition of the days-inmonth procedure:
(define (days-in-month month)
     (item (month-number month)
          ) )
2.How would you explain to someone why the second version of calculating the difference between dates is better? Why is it better? (please limit your answer to < 30 words)
10.2 Analyze a claim about the program.(1 step)
10.2.1 (Brainstorm) Can arguments to + in version 2 of day-span be nonnumeric?
A pair of CS 3 students are experimenting with version 2 of the day-span code. In one of their experiments, they called day-span and got an error message saying that the + procedure is being called with a nonnumeric argument. They believe that the crash is in the call to + in the day-span procedure rather than the call to + in day-of-year. Do you believe them? Why or why not?
10.3 Modify the programs.(5 steps)
10.3.1 (Brainstorm) Experiment with the "item" procedure.
The builtin item procedure takes two arguments: an integer and a sentence or word. Describe, as completely as possible, what arguments you can give item so that it won't crash. Also say what item returns, given the arguments you just described.
10.3.2 (Brainstorm) How would the "item" procedure be useful in the case study programs?
Identify all the procedures in the case study programs (both versions) that could be significantly shortened by use of the item procedure. Write the shortened code to one of these procedures using item.
10.3.3 (Display page) Make day-span work for dates in European format.
In Europe, dates are specified with the date-in-month preceding the month, for example, "3 April" rather than "April 3". Define a procedure named europe-day-span whose arguments are dates in European format, that is, with the date-in-month preceding the month name rather than following it. (Assume that month names are those of English: January, February, etc.) Thus the call
    (europe-day-span '(1 january) '(15 may))
should produce the same result as
    (day-span '(january 1) '(may 15))
Write your procedure by modifying the code of the second version of day-span (appendix B). You will need to define a european-day-span procedure; do this without calling the original day-span procedure. You can change any of the other helper procedures, but change as few as possible. Put your solution into the file europe-day-span.scm. It is pretty common that when you are finished, the day-span procedure won't work correctly anymore. You might want to save a copy of your modified day-span procedure that you've been working on.
10.3.4 (Display page) Data abstractions make some changes easier.
Implementing the europe-day-span procedure should have required only a few changes to the rest of the program. This was no accident; the program was designed to insulate as much of the code as possible from how a date is represented. This is done by viewing a date in most of the program as an abstraction with a month name and a date-in-month rather than (say) as a two-word sentence. It's not too hard to think of real-life data abstractions. Here are some examples:
  • Most of us view an automobile as an abstraction with an ignition switch, a steering wheel, a gas pedal, and a brake pedal. Different cars have different internal mechanisms—disc vs. drum brakes, four vs. six vs. eight cylinders, etc.—but drivers generally need not concern themselves with the differences.
  • Speakers in a stereo system are an abstraction with a place to plug them into the sound source. There are a variety of technologies for speakers, but someone plugging them into a stereo receiver doesn't have to worry about that.
  • An apartment is an abstraction with (among other things) a thermostat. Apartment dwellers don't need to know whether their heating system uses gas, electricity, or whatever; they just turn up the thermostat and the apartment gets warmer.
Data abstractions are convenient for the user, but even more convenient for the programmer. Program maintenance tasks often involve changing the internal representation of some of the data, and the programmer wants this task to be as easy as possible. If the details of the representation are hidden in just a few procedures, a change to the representation should require changing only those procedures; the rest of the program shouldn't need modification. In the Difference Between Dates programs, the representation of a date was known only by two accessor (or selector) procedures, month-name and date-in-month.
10.3.5 (Brainstorm) Data Abstraction Examples
We provided a bunch of examples of data abstraction. Now it is your turn. Respond with your own example of data abstraction. Can you come up with one that doesn't involve people?
10.4 Homework(2 steps)
10.4.1 (Display page) This week's homework

Homework for the upcoming week

For homework for the next lab,
  1. go back to the last day's discussion and contribute two polite comments
  2. contribute to the discussion in the following step
  3. start on miniproject 1 (see below). Double check with your TA or the website for when mini-project 1 is due. You'll have some time in class to work on it. An important place to start is to begin with a plan. This could be a diagram or pseudo-code. Show your initial plan to your TA before you start coding.
10.4.2 (Discussion Forum) How can you tell when you've reached a dead end?
Inexperienced programmers sometimes have trouble realizing that they're stuck. Having chosen a solution approach that is basically a dead end, they keep working and working on it rather than trying to rethink their approach.

Provide some advice for how to recognize that they are stuck. Tomorrow, we'll ask you to make two comments on what your classmates have posted.