CSC 160
Homework 3

Assigned June 6, due June 13

For all the programming assignments, be sure to follow the design recipe. For functions, write your function contract, examples, and function definition in the Definitions Window, save it to a file, and send me this file. Also test your program: since you've already included examples in the Definitions window, you should be able to hit the Execute button and see all the results (along with what you said they "should be"). Save the resulting Interactions window to a text file and send me this file too. Be sure to choose meaningful names for functions and parameters, and watch for opportunities to re-use functions you, I, or the textbook have already written.

Also turn in a log of how many errors of different kinds you encountered in the assignment, with brief comments describing each one ("mismatched parentheses" is self-explanatory, but more complex errors might need more description). You may do this using the PSP forms, or simply by keeping track in a text file or on paper and turning it in.

When I say "Design a structure", I mean

  1. write an English-language data definition,
  2. write a define-struct,
  3. write down the contracts of all the functions that "come for free" with the define-struct,
  4. write a template for functions that take a parameter of this type. Note that once you've written this for a given struct, you can copy-and-paste it as a starting point for every function that takes in this kind of parameter.

For example, if I were to say

Design a structure to represent an employee, with a name, salary, and employee ID number,
you might answer as follows:

If you need help with the functions that require images or animations, please see the documentation for the tiles-world teachpack.

Stuff from Class

This is really a "warm-up" exercise, since we've done most of it in class or above.

  1. Design a structure to represent an employee, with a name, salary, and employee ID number. (This is done for you in the file employee.scm; please download this file and use it as a starting point for the following two problems.)

  2. Using the template, develop a function named earns-over-100K? which takes an employee and returns a Boolean telling whether the employee earns over $100,000/year.

  3. Using the template, develop a function named give-10%-raise which takes an employee and returns a new employee with the same name and ID number, but earning 10% more.

Computerized Elections

  1. Recall the who-won function from the previous homework. It has a serious disadvantage: since it has the names "Anne", "Bob", and "Charlie" hard-coded in, it'll need to be rewritten every time different candidates run for office. Design a structure to represent a candidate in an election. The structure should contain both the candidate's name and how many votes the candidate received.

  2. Using the template for functions working on candidate structures, develop a function named who-won which takes in three candidate structures and returns the winning candidate structure. For now, you may assume that there are no ties. (Hint: You won't be able to re-use the old who-won function here, because the old function was specific to the names "Anne", "Bob", and "Charlie".)

  3. Develop a function named winner-name which takes in three candidate structures and returns the name of the winner. (This can be done using the template, but there's a much easier way, by re-using previously defined functions.)

  4. Using the template for functions working on candidate structures, develop a function named add-vote which takes in a candidate structure and returns a candidate structure just like it, but with one more vote.

Building an animation

This time we'll animate a rocketship moving around the screen. By the laws of physics, a rocketship keeps moving at its current velocity (i.e. speed and direction) until something happens (e.g. firing the rockets) to change its velocity. Thus to represent a moving rocketship we need several pieces of information: where it is now (in both x and y dimensions), its velocity in the x dimension, and its velocity in the y dimension. (By convention, positive x velocity means moving to the right, and positive y velocity means moving down the screen.)

  1. Design a structure to represent all this information.

  2. Using the template for functions working on rocket structures, develop a function named show-rocket which takes in one of these structures and returns an image with the rocket at the right position. For simplicity, let's just draw the rocket as a circle.
    Extra credit: find a small picture of a rocketship on the Web and use that instead.
    More extra credit: display the picture of the rocketship rotated so as to most closely match the direction it's currently moving. (Recall that the tiles-world teachpack provides three functions rotate-cw, rotate-ccw, and rotate-180. This only gives you four choices of directions, so it won't be as cool as if you could make it point in exactly the right direction, but that's harder.)

  3. Using the template for functions working on rocket structures, develop a function named handle-key which takes in a rocket structure and a key, and changes the velocity of the rocket (but not its position) depending on whether the up, down, left, or right arrow was pressed, returning the resulting rocket structure.

  4. Using the template for functions working on rocket structures, develop a function named handle-tick which takes in a rocket structure and returns a new rocket structure with the same velocity as the old one, but with the position changed by the x velocity in the x dimension, and the y velocity in the y dimension.

  5. Put these together into a working animation.

Computerized Elections Again

This time we'll enhance the who-won and related functions to handle ties correctly. The problem is that who-won is defined to return a candidate structure, but what candidate structure should be returned in case of a tie? There is no good answer to this, so we'll invent another new data type or two.

  1. Define a struct named tie which has no parts at all. It will still have a constructor and a discriminator, but no selector functions. (The template for it isn't very interesting, so don't bother writing it.) The purpose of this data type is just to be identifiable as a tie; it doesn't hold any information.

  2. Design a data type candidate-or-tie which is either a candidate structure or a tie. Hint: This data type is not a struct, so all you have to write is a data definition (which I just gave you in the previous sentence) and a template.

  3. Develop a function named who-won-if-any which takes in three candidate structures and returns either the winning candidate, if there is a clear winner, or a tie if not.

  4. Using the template for functions working on candidate-or-tie objects, develop a function named winner-name-if-any which takes in three candidate structures and returns either the name of the winner, or the string "tie".

Grading standards

Error log:       /35
(I'm not grading on how many or how few errors you encountered, only on whether you recorded them adequately.)

Function or struct name Data def'n Define-struct Contracts for
struct functions
Template Contract Examples Definition Test runs
employee struct given above  
earns-over-100K? function   /5 /5 /10 /5
give-10%-raise function   /5 /5 /10 /5
candidate struct /5 /5 /5 /10  
who-won function   /5 /5 /10 /5
winner-name function   /5 /5 /10 /5
add-vote function   /5 /5 /10 /5
rocket struct /5 /5 /5 /10  
show-rocket function   /5 /5 /10 /5
show-rocket XC     /10 /20 /10
handle-key function   /5 /5 /10 /5
handle-tick function   /5 /5 /10 /5
animation   /20
tie struct /5 /5 /5    
candidate-or-tie data type /5     /5  
who-won-if-any function   /5 /5 /10 /5
winner-name-if-any function   /5 /5 /10 /5

General skills:

Following directions /10
Writing contracts from word problems /10
Choosing examples /10
Choosing names /10
Coding /10
Code re-use and function composition /10

Total:         /500 +    /40 XC


Last modified:
Stephen Bloch / sbloch@adelphi.edu