On this page:
2.5.1 Objectives
2.5.2 Exercises (due 2011/  09/  23 08:  45:  00)
2.5.3 Optional Exercises (due 2011/  09/  28 08:  45:  00)
2.5.4 Notes

2.5 Objects

The world was barren

It’s bad for int to be alone

Let there be Point*

2.5.1 Objectives

At the end of this class, you should know:

At the end of this class, you should be able:

2.5.2 Exercises (due 2011/09/23 08:45:00)

1. Write a data definition for a baseball player. Your definition should record the player’s name, batting average, number of home runs, number of runs batted in, and the player’s position in the field. Give two examples of baseball players. Finally, write a function worthAMillion that returns true if a given player has more than 20 home runs, more than 100 RBI’s, and an average above .280.

2. Write a data definition that represents an airforce’s jet fighters. Assume that a fighter has four essential properties: designation ("f22", "tornado", or "mig22"), acceleration, top speed, and range. Then develop the function withinRange. The function consumes a fighter and the distance of a target from the (fighter’s) base. It determines whether the fighter can reach the intended target.

3. Develop the function reduceRange. The function consumes a jet fighter and produces a fighter in which the range field is reduced to 80% of the original value.

4. Write a data definition for three digit numbers, where there are three integers. Write a function called reveal that takes three arguments: a ’chosen’ three digit number, a ’current’ three digit number, and ’guess’ integer. The function produces a new ’current’ number where the digits are the same as before, unless the ’guess’ is the same as one of the digits, in which case it is "revealed". For example,

reveal( new_TDN(1, 2, 3), new_TDN(0, 0, 0), 1) = new_TDN( 1, 0, 0)

reveal( new_TDN(1, 2, 3), new_TDN(1, 0, 0), 3) = new_TDN( 1, 0, 3)

reveal( new_TDN(1, 2, 3), new_TDN(1, 0, 3), 6) = new_TDN( 1, 0, 3)

reveal( new_TDN(1, 2, 3), new_TDN(1, 0, 3), 2) = new_TDN( 1, 2, 3)

reveal( new_TDN(1, 2, 2), new_TDN(1, 0, 3), 2) = new_TDN( 1, 2, 2)

This is basically Hangman!

2.5.3 Optional Exercises (due 2011/09/28 08:45:00)

5. Write a data definition for movies. Create example objects for two of your favourite movies. Print out their contents with printf

6. Write a data definition that represents time since midnight as hours, minutes, and seconds. Write a function timeToSeconds that produces the number of seconds since midnight.

2.5.4 Notes

These notes are primarily for my sake, but I don’t see any reason to hide them from you.

Core of a game or animation

 

Try to write translate

 

Functions rarely take just one piece of data (number, boolean); instead they take many pieces

 

Compound data combines a few atomic things---a description of a cd, a description of a point

 

> define Posn

-- data definition

-- typedef struct Posn

-- Posn* new_Posn ...

 

> example points (3,4) (2*3, 2*4), (12,6-1)

 

> pulling out pieces

 

> distanceToPoint (from origin)

 

design recipe "take stock" step has grown

 

> translate (original, dx, dy)

 

> time since midnight (hours, mins, secs)

> boyfriend (name, hair, eyes, phone)

> cheerleader (name, number)