1 / 32

CSE 115

CSE 115. Introduction to Computer Science I. Announcements. Must have 3 stars on both Module 1's Calling Functions & Defining Functions to take THIS week's lab Must have 3 stars on Module 1 PreLab to take NEXT week's LAB EXAM. Announcements.

jrich
Download Presentation

CSE 115

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. CSE 115 Introduction to Computer Science I

  2. Announcements • Must have 3 stars on bothModule 1's Calling Functions&Defining Functionsto take THIS week's lab • Must have 3 stars onModule 1 PreLabto take NEXTweek's LAB EXAM

  3. Announcements • Next week's lab exam during 1st half of labNothing happening afterward; lab dark for 2nd half • Schedule differs for future lab exams1st half for that module's lab exam • Make-up of previous module exam in 2nd half of lab • Still need to earn stars to take make-up exam • Will use higher of exam & make-up score

  4. Today's Plan • Review • Functions and control flow exercises

  5. REVIEW REVIEW REVIEW REVIEW REVIEW REVIEW REVIEW Writing a Compound Expression Subgoals • exp1) Determine the left-hand side of the expression • a) Write the left-hand subexpressionb) Determine the type for the value this subexpression evaluates to • exp2) Write the operator • exp3) Determine the right-hand side of the expression • a) Write the right-hand subexpressionb) Determine the type for the value this subexpression evaluates to • exp4) Verify operator valid for subexpressions' types

  6. REVIEW REVIEW REVIEW REVIEW REVIEW REVIEW REVIEW Writing an Assignment Subgoals • assign1) If first use of variable in function, write let keyword • assign2) Write the name of the variable • assign3) Write the assignment operator • assign4) Write the expression whose value will be assigned to the variable

  7. REVIEW REVIEW REVIEW REVIEW REVIEW REVIEW REVIEW Writing a Function Definition Subgoals • fd1) Write comment describing result of evaluating call to this function call (or what function does, if not returning a value) • fd2) Write the function header & delimitersa) Write the function keywordb) Write the function's namei) Choose name reflecting function's SINGLE purposec) Write the parameter list for the function's input(s)i) Choose name(s) expressing value of eachd) Write the function delimiters • fd3) Write the function body inside the delimitersa) Parameter(s) assigned values BEFORE function begins; you should not reassign these valuesb) If call to this function evaluates to a value, make certain that each path through function body ends at a return whose expression evaluates to that value

  8. REVIEW REVIEW REVIEW REVIEW REVIEW REVIEW REVIEW Writing a Function Call Expression Subgoals • fc1) Write the function name • fc2) Write the argument lista) Start the argument list with an open parenthesisb) For each function input in the order they are listed, write the expression whose value is used for that inputc) Write a comma between each pair of argumentsd) End the argument list with a close parenthesis

  9. REVIEW REVIEW REVIEW REVIEW REVIEW REVIEW REVIEW Writing Selection Statement Subgoals • sel1) Determine number of distinct execution paths • sel2) Determine the guard expressions that are neededa) For each execution path, write the guard expression specifying conditions when it should be runb)Order guard expression(s) from most-restrictive to least-restrictive • sel3) Use most-restrictive guard expression for theifsectiona) Write if header with the most-restrictive guard expression in parenthesesb) Write the delimitersc) Write if body for that execution path inside the delimiters • sel4) [If] each guard expression remaining, in order of decreasing restrictiveness, write[else]sectiona) Writeelse ifheader using the guard expressioni)If this is last execution path & run for all remaining cases, [write]else ifheader with elseb) Write the delimitersc) Writeelse iforelsebody for that execution path inside the delimiters

  10. REVIEW REVIEW REVIEW REVIEW REVIEW REVIEW REVIEW Writing Compound Selection Statement Subgoals • sel1) Determine number of distinct execution paths • sel2) Determine the guard expressions that are neededa) For each execution path, write the guard expression specifying conditions when it should be runb) Order guard expression(s) from most-restrictive to least-restrictive • sel3) Use most-restrictive guard expression for theifsectiona) Write if header with the most-restrictive guard expression in parenthesisb) Write the delimitersc) Write if body for that execution path inside the delimiters • sel4) For each guard expression remaining, in order of decreasing restrictiveness, write else if sectiona) Write else ifheader with the guard expression in parenthesisi) If this is last execution path & run for all remaining cases, replace else ifheader with elseb) Write the delimitersc) Write else if or elsebody for that execution path inside the delimiters

  11. REVIEW REVIEW REVIEW REVIEW REVIEW REVIEW REVIEW Selection Statement: if Details Of a JavaScript if Statement sel1a)if keyword sel1a)(guard expression) if ( (a < b) && (a < c) ) { console.log('a is smallest'); smallest = a; • sel1c) code block } sel1b)delimiters

  12. REVIEW REVIEW REVIEW REVIEW REVIEW REVIEW REVIEW Selection Statement: if-else Parts of a if-else Statement if header delimiter let smallest; if ((a < b) && (a < c)) {console.log('a is the smallest'); smallest = a; } else { console.log('a not smallest’); } if body delimiter delimiter delimiter else body else keyword

  13. REVIEW REVIEW REVIEW REVIEW REVIEW REVIEW REVIEW Compound Selection Statement Parts of Compound Selection Statement if header if ( (a < b) && (a < c) ) { else if header console.log('a small');sm = a; if body } else if ( (b < a) && (b < c) ) { else if body console.log('b small');sm = b; else header } { else console.log('c small');sm = c; else body }

  14. REVIEW REVIEW REVIEW REVIEW REVIEW REVIEW REVIEW Compound Selection Statement Control Flow if ((a < b)&&(a < c)){console.log('a');sm = a; } else if ((b<c)&&(x==2)){console.log('b, x'); sm = b; } else { console.log('odd'); z = c;} console.log(sm)

  15. … and finally

  16. Today's Plan • Review • Functions and control flow exercises

  17. totalCost • Want function which takes:number of items sold and thecost per item andreturns the total cost • Work with neighbors and be prepared to:explain your answer (in English or as control flow),write the function header and delimiters (in JavaScript),describe good set of test cases (as function calls & results)write the function body (in JavaScript)

  18. totalCost • Possible declaration: • /* compute the total cost of n items, • given a cost per item (cpi) • */ function totalCost(n, cpi) { // Code will go here }

  19. totalCost • Possible declaration: • Sample test cases: • /* compute the total cost of n items, • given a cost per item (cpi) • */ function totalCost(n, cpi) { // Code will go here } totalCost(0, 23)should return 0 • totalCost(34, 2.3)should return 78.2 • totalCost(2, 0)should return 0 • totalCost(3, 13.3)should return 39.9

  20. totalCost • Possible declaration: • Sample test cases: • /* compute the total cost of n items, • given a cost per item (cpi) • */ • function totalCost(n, cpi) { return n * cpi; } totalCost(0, 23)should return 0 • totalCost(34, 2.3)should return 78.2 • totalCost(2, 0)should return 0 • totalCost(3, 13.3)should return 39.9

  21. shipping • Write shipping function whose input is:cost of an order andreturns the cost to ship the order. • Assume orders $100+ get free shipping, but orders below $100 pay 10% of the pre-tax cost for shipping • Work with neighbors and be prepared to:explain your answer (in English or as control flow),write the function header and delimiters (in JavaScript),describe good set of test cases (as function calls & result)write the function body (in JavaScript)

  22. shipping • Possible declaration: /* compute the shipping cost of an order: orders $100 or higher get free shipping orders under $100 pay 10% in shipping */ function shipping(orderCost) { // Code will go here }

  23. shipping • Possible declaration: • Sample test cases: /* compute the shipping cost of an order: orders $100 or higher get free shipping orders under $100 pay 10% in shipping */ function shipping(orderCost) { // Code will go here } shipping(14.2)should return 1.42 • shipping(99.99)should return 9.999 • shipping(100)should return 0 • shipping(114)should return 0

  24. shipping • Possible declaration: • Sample test cases: /* compute the shipping cost of an order: orders $100 or higher get free shipping orders under $100 pay 10% in shipping */ function shipping(orderCost) { if (orderCost >= 100) { return 0; } else { return orderCost * 0.1; } } shipping(14.2)should return 1.42 • shipping(99.99)should return 9.999 • shipping(100)should return 0 • shipping(114)should return 0

  25. invoiceTotal Write invoiceTotal function with inputs:number of items sold and the cost per item andreturns total cost of items, plus shipping (based on total cost of item), plus 8% sales tax on pre-shipping total Work with neighbors and be prepared to:explain your answer (in English or as control flow),write the function header and delimiters (in JavaScript),describe good set of test cases (as function calls & result)write the function body (in JavaScript)

  26. invoiceTotal • /* compute the invoice total (including shipping and tax) for an order of n items, given a cost per item (cpi) • */ • function invoiceTotal(n, cpi) { // code will go here } • Possible declaration: • Sample test cases:

  27. invoiceTotal • /* compute the invoice total (including shipping and tax) for an order of n items, given a cost per item (cpi) • */ • function invoiceTotal(n, cpi) { // code will go here } • Possible declaration: • Sample test cases: invoiceTotal(5, 30)should return 162 • invoiceTotal(5, 20)should return 108 • invoiceTotal(10, 2.2)should return 25.96 • invoiceTotal(4, 0)should return 0

  28. invoiceTotal • /* compute the invoice total (including shipping and tax) for an order of n items, given a cost per item (cpi) • */ • function invoiceTotal(n, cpi) { let cost = totalCost(n, cpi); let ship = shipping(cost); let afterTax = cost * 1.08; let retVal = ship + afterTax; return retVal; } • Possible declaration: • Sample test cases: invoiceTotal(5, 30)should return 162 • invoiceTotal(5, 20)should return 108 • invoiceTotal(10, 2.2)should return 25.96 • invoiceTotal(4, 0)should return 0

  29. ldl_level • Write ldl_level function with input:ldl reading andreturn name of category for that reading • Work with neighbors and be prepared to:explain your answer (in English or as control flow),write the function header and delimiters (in JavaScript),describe good set of test cases (as function calls & result)write the function body (in JavaScript)

  30. ldl_level // Return the cholesterol category given an ldl level function ldl_level(ldl) { // code will go here } return "optimal"; } else if (ldl < 130) { return "near optimal"; } else if (ldl < 160) { return "borderline high"; } else if (ldl < 190) { return "high"; } return "very high";} • Possible declaration: • Sample test cases:

  31. ldl_level // Return the cholesterol category given an ldl level function ldl_level(ldl) { // code will go here } return "optimal"; } else if (ldl < 130) { return "near optimal"; } else if (ldl < 160) { return "borderline high"; } else if (ldl < 190) { return "high"; } return "very high";} • Possible declaration: • Sample test cases: ldl_level(99) => "optimal" ldl_level(100) => "near optimal"ldl_level(129) => "near optimal" ldl_level(130) => "borderline high" ldl_level(159) => "borderline high" ldl_level(160) => "high" ldl_level(189) => "high" ldl_level(190) => "very high"

  32. ldl_level // Return the cholesterol category given an ldl level function ldl_level(ldl) { if (ldl < 100) { return "optimal"; } else if (ldl < 130) { return "near optimal"; } else if (ldl < 160) { return "borderline high"; } else if (ldl < 190) { return "high"; } return "very high";} • Possible declaration: • Sample test cases: ldl_level(99) => "optimal" ldl_level(100) => "near optimal"ldl_level(129) => "near optimal" ldl_level(130) => "borderline high" ldl_level(159) => "borderline high" ldl_level(160) => "high" ldl_level(189) => "high" ldl_level(190) => "very high"

More Related