On this page:
2.2.1 Objectives
2.2.2 Exercises (due 2011/  09/  09 08:  45:  00)
2.2.3 Optional Exercises (due 2011/  09/  14 08:  45:  00)
2.2.4 Notes

2.2 Expressions and Functions

2.2.1 Objectives

At the end of this class, you should know:

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

2.2.2 Exercises (due 2011/09/09 08:45:00)

1. Define a function called tip that will add a 15% tip to a given number. Define a function called shareWithTip that takes a pizza price and the number of slices consumed computes the share of the cost of a pizza, assuming it has 8 slices, including the cost of the tip (refer to assignment 1, exercise 1), using tip.

2. Define the function fahrenheitToCelsius, which consumes a Fahrenheit temperature and produces the Celsius equivalent. Use Google or Wikipedia to find the conversion formula.

3. Define the function convert3, which consumes three digits, starting with the least significant (meaning right-most when written positionally) digit, and produces the corresponding number. So, convert3(1,2,3) should produces 321 and convert3(3,5,1) should produces 153.

4. Formulate n^2 + 10 as a function. Ensure your function’s correctness on 2 and 9.

5. The local supermarket needs a program that can compute the value of a bag of coins. Define the function sum_coins. It consumes four numbers: the number of pennies, nickels, dimes, and quarters in the bag; it produces the amount of money in the bag.

6. An old-style movie theater has a simple profit function. Each customer pays $5 per ticket. Every performance costs the theater $20, plus $.50 per attendee. Develop the function total_profit. It consumes the number of attendees (of a show) and produces how much income the attendees produce.

2.2.3 Optional Exercises (due 2011/09/14 08:45:00)

7. Define the function dollarToYen, which consumes a number of dollars and produces a number of yen. Use the Google search "1 dollar = yen" to find the current conversion.

8. Define the function areaOfTriangle, which consumes the length of a triangle’s side and the perpendicular height and produces the area. Use Google or Wikipedia to find the formula.

9. Formulate (1/2)*n^2 + 20 as a function. Ensure your function’s correctness on 2 and 9.

2.2.4 Notes

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

area of a disk of radius r in math

 

what is area when r = 5?

 

we can name the value

 

these math "rules" are functions

 

write areaOfDisk

 

once we create functions, they are indistinguishable from what comes with Java

 

evaluate them with algebra (substitution)

what about areaOfRing

 

We rarely turn math into programs

 

"Company XYZ & Co. pays all its employees $12 per hour. A typical employee works between 20 and 65 hours per week. Develop a program that determines the wage of an employee from the number of hours of work."

 

What is the actual problem?

 

12 * h

 

Discuss errors

 

Design recipe...

> understand the purpose

-- contract

-- header

-- purpose statement

 

> understand the problem

-- examples

 

> template

 

> write the body

-- fill in the ...

 

> test

-- run the examples

 

Discuss some sample problems

 - distance

 - tithe

 - areaOfSquare

 - circumOfRect

 - clampVolume