CS 1101 - A-term 15

Homework 7 - Programming with Mutable Variables; Accumulator-style programming

Due: Tuesday, October 13 at 5pm

Read the expectations on homework.

Assignment Goals


The Assignment

The Registrar's office has to manage a lot of data. There are many courses offered at WPI. There are also many students. Courses have students, and students are enrolled in courses. In this assignment, you'll define data and write programs to manage course registration.

A Word about Testing

As we discussed in class, testing programs that use mutable variables becomes more complicated than testing programs that don't use mutable variables. If you write helper functions for this assignment that can be tested with check-expect, you should provide test cases for those functions, as usual.

You do not have to show test cases that result in error conditions.

You do not have to show test cases for any function that involves mutation. The graders will be running your programs with their own set of tests (see below). However, make sure you understand how to construct tests for functions that use mutation (you might be asked something about this on the third exam).

Problems

  1. Provide data definitions for a course and a student. Each course is represented by the department offering the course (a String), the course number (a Number), the name of the faculty member teaching the course (a String), the maximum enrollment for the course (a Natural), and a list of students currently enrolled in the course. Each student is represented by the name of the student (String), the student's ID (Number), and a list of courses the student is enrolled in. Note that a course contains a list of students (NOT just student names) and a student contains a list of courses (NOT just course numbers).

  2. Create and document two variables. Courses can contain a list of courses. Students can contain a list of students. Both variables should initially be empty.

  3. Write a function add-student that consumes the name and ID of a student and produces void. The function checks to make sure that a student with the given ID doesn't already exist in Students. If such a student already exists, the function produces an error "student already exists". If there is no such student, the function adds the student to Students. The added student's list of courses is initially empty.

  4. (Note: this problem is very similar to problem 3, and thus will not be graded. However, you need to write it in order to be able to complete the remaining problems.) Write a function add-course that consumes a department, course number, the name of a faculty member, and the maximum enrollment, and produces void. The function checks to make sure that a course with the given department and course number doesn't already exist in Courses. If such a course already exists, the function produces an error "course already exists". If there is no such course, the function adds the course to Courses. The list of students in the course is initially empty.

  5. Write a function add-to-course that consumes a student's ID, a department, and a course number and produces void. If the student or the course doesn't exist, the function produces an appropriate error. If there is still room in the course, the function adds the student with the given ID to the course with the given department/course number, and adds the course with the given department/course number to the student's list of courses. If the maximum enrollment for the course has already been reached, the function produces an appropriate error.

  6. Write a function largest-enrollment that doesn't consume anything. The function produces the course in Courses with the largest current enrollment (you should be returning the course that has the most students in its list of students, not necessarily the course that has the largest maximum enrollment). If there are no courses in Courses, the function produces an appropriate error. You must use accumulator-style programming to solve this problem.

The graders will be running your programs with a set of test cases such as the following (there will be additional tests besides the ones shown):
(add-student "Joe" 123)
(add-student "Ann" 456)
(add-student "Chris" 789)
(add-course "CS" 1101 "Hamel" 100)
(add-course "BI" 1000 "Rulfs" 20)
(add-course "MA" 2201 "Servatius" 50)
(add-to-course 456 "MA" 2201)
(add-to-course 456 "CS" 1101)
(add-to-course 123 "CS" 1101)
(add-to-course 789 "BI" 1000)
(largest-enrollment)
Make sure that your functions adhere to the signature/purpose given, otherwise the test cases won't run. Also, make sure you name the functions as indicated in each problem.


What to Turn In

Using web-based turnin, turn in a single file containing all code and documentation for this assignment. Name your file according to the naming conventions for files. Make sure both partners' names and wpi login names appear in a comment at the top of the file.