programming games drawing logo. expressions. formulas. credit cards. form validation. homework: your...

17
Programming games Drawing logo. Expressions. Formulas. Credit cards. Form validation. Homework: your credit card program. Continue uploading work.

Upload: edwin-ryan

Post on 18-Jan-2018

221 views

Category:

Documents


0 download

DESCRIPTION

Formulas Mathematical expressions are combinations of operators and terms –examples of operators: +, -, *, /, ==, ===, !=, &&, … –examples of terms: variable name, constant Programming languages have features for expressing mathematical formulas distance = velocity x time Code, assume distance, velocity, time all variables distance = velocity * time; multiplication === checks for datatype & value

TRANSCRIPT

Page 1: Programming games Drawing logo. Expressions. Formulas. Credit cards. Form validation. Homework: your credit card program. Continue uploading work

Programming games

Drawing logo. Expressions. Formulas. Credit cards. Form validation.

Homework: your credit card program. Continue uploading work.

Page 2: Programming games Drawing logo. Expressions. Formulas. Credit cards. Form validation. Homework: your credit card program. Continue uploading work

HTML5 logo

• http://faculty.purchase.edu/jeanine.meyer/html5/html5logoscale.html

• Examine code

• Divide up code: each group explains section.

Page 3: Programming games Drawing logo. Expressions. Formulas. Credit cards. Form validation. Homework: your credit card program. Continue uploading work

Formulas• Mathematical expressions are combinations of operators

and terms– examples of operators: +, -, *, /, ==, ===, !=, &&, …– examples of terms: variable name, constant

• Programming languages have features for expressing mathematical formulasdistance = velocity x time

Code, assume distance, velocity, time all variablesdistance = velocity * time;

multiplication

=== checks for datatype & value

Page 4: Programming games Drawing logo. Expressions. Formulas. Credit cards. Form validation. Homework: your credit card program. Continue uploading work

Function expressing formulafunction distance (velocity, time) { return velocity * time;}Give me velocity and time and I'll [the

function] will return the distance.

The function header indicates the [incoming] parameters (aka arguments). NOTE: in many languages, the function header also indicates the datatype of each parameter and the datatype of the returned value. This makes it possible to check that the programmer is NOT using the functions incorrectly…at least as far as datatype.

Page 5: Programming games Drawing logo. Expressions. Formulas. Credit cards. Form validation. Homework: your credit card program. Continue uploading work

Temperature conversion

Tempfahrenheit = Tempcentigrade*(9/5)+32;

Check by putting in points for boiling:

212 Fahrenheit and 100 CentigradeFor freezing

32 Fahrenheit and 0 CentigradeWhat is formula for… the other direction?

Page 6: Programming games Drawing logo. Expressions. Formulas. Credit cards. Form validation. Homework: your credit card program. Continue uploading work

Caution• Recall: Programming systems store whole

numbers (0, 1, 2, -10, etc.) differently than numbers with fractions (0.5, 10.23, -2.3333333, etc.)

• Need to make sure that none of the intermediate steps are truncated to whole numbers!– One approach: write 9.0 and 5.0 and 32.0– Note: problems occurs with the division, not

multiplication or addition

Page 7: Programming games Drawing logo. Expressions. Formulas. Credit cards. Form validation. Homework: your credit card program. Continue uploading work

Precedence

• Many programming courses start off with rules of precedencea*b+cIs evaluated as (a*b)+c. The multiplication is done

firstThe alternative is a* (b+c)

• Recommendation: put in parentheses!• MAYBE: avoid long statements—use

multiple lines

Page 8: Programming games Drawing logo. Expressions. Formulas. Credit cards. Form validation. Homework: your credit card program. Continue uploading work

Conditionals• Suppose a formula (for ticket price or score or

…) involves a conditional test:– One Tuesdays, drinks are half priceLogic: if it is Tuesday, dcost = .5*dcost;

• Implementation: use if statement– Alternative: conditional operator. Show later.

• But how do we know if it is Tuesday?• Implementation: use Date

– Remember from first HTML example!

Page 9: Programming games Drawing logo. Expressions. Formulas. Credit cards. Form validation. Homework: your credit card program. Continue uploading work

Date code• Suppose policy of half-price on Tuesdays today = new Date(); dayofweek = today.getDay(); //encoding is 0 for Sunday, 1 for Mon., // 2 for Tuesday if (dayofweek==2) { dcost = .5 * dcost; }

Page 10: Programming games Drawing logo. Expressions. Formulas. Credit cards. Form validation. Homework: your credit card program. Continue uploading work

Conditional operator

• Operator with 3 operands condition ? value_if_true : value_if_false

… dcost = (dayofweek==2) ? (.5*dcost) : dcost;

Comfortable_with_conditional ? Use_It : if_statement

Page 11: Programming games Drawing logo. Expressions. Formulas. Credit cards. Form validation. Homework: your credit card program. Continue uploading work

A man walks into a bar…

See also: http://faculty.purchase.edu/jeanine.meyer/creditcard.html

Notes: http://faculty.purchase.edu/jeanine.meyer/credit.doc

Page 12: Programming games Drawing logo. Expressions. Formulas. Credit cards. Form validation. Homework: your credit card program. Continue uploading work

Credit cards!!!!

• Calculation (as indicated by code)balance = old_balance + purchasesSubtract the payment:

balance = balance – paymentCompute interest

Divide ANNUAL interest by 12.REMEMBER DECIMAL POINTbalance = balance + (balance * interest)

Add fee if required: balance = balance + fee

Page 13: Programming games Drawing logo. Expressions. Formulas. Credit cards. Form validation. Homework: your credit card program. Continue uploading work

Estimating

• 24% ANNUAL interest is 2% per month– 24/12 is 2!

• What is 2% of $50?– 2% of 100 is 2. So…2% of 50 would be 1– What is 10% of 50? 5, so 2% would be less

than that• 15% ANNUAL interest. Monthly would

be…more than 1 less than 2 (1.25)

Page 14: Programming games Drawing logo. Expressions. Formulas. Credit cards. Form validation. Homework: your credit card program. Continue uploading work

Compounding• The interest is added to what you owe. • The new balance is what you haven't paid plus

the interest on what you haven't paid.• Next month, you will be paying interest on the

interest.• This doesn't include fees for not paying the

minimum • My program does not do compounding on a

daily basis. Actual rules mean you pay more!• NOTE: it still is a free loan if you pay off balance.

– There are charges to merchants.

Page 15: Programming games Drawing logo. Expressions. Formulas. Credit cards. Form validation. Homework: your credit card program. Continue uploading work

Compounding

• is your friend, when you are saving (or investing)– You earn interest and then interest on the

interest.• is your enemy if you owe money

• Interests are very low now, but they (probably) will rise!

Page 16: Programming games Drawing logo. Expressions. Formulas. Credit cards. Form validation. Homework: your credit card program. Continue uploading work

Describe 3 mistakes in the following code for calculating days in the month

switch(mon { case “Sep”: case “Apr”: case “Jun”: case “Nov”:

daysInMonth = “This month has 30 days.”; case “Feb”:

daysInMonth = “This month has 28 days.”; break;default: daysInMonth = “This month has 31 days.”;}

Page 17: Programming games Drawing logo. Expressions. Formulas. Credit cards. Form validation. Homework: your credit card program. Continue uploading work

Classwork/homework

• Prepare your own [simplified] credit card calculator

• Do something original– use pictures.– Look up how to specify input to be numbers

and within limits and use this for the input fields.

– ?????