1 / 11

COMPSCI 101 Principles of Programming

COMPSCI 101 Principles of Programming. Lecture 06 – Testing Functions. Learning outcomes. At the end of this lecture, students should be able to: Divide a problem up into different tasks Write a function to perform each task Create a solution to the problem using multiple functions. Recap.

dreama
Download Presentation

COMPSCI 101 Principles of Programming

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. COMPSCI 101Principles of Programming Lecture 06 – Testing Functions

  2. Learning outcomes • At the end of this lecture, students should be able to: • Divide a problem up into different tasks • Write a function to perform each task • Create a solution to the problem using multiple functions COMPSCI 101 - Principles of Programming

  3. Recap • We use test cases to determine if our code works • We automate the testing procedure where possible • Python provides Doctest as a way to automate testing • Doctest consists of two parts defsquare_area(side): """Returns the area of a square … documentation goes here >>> square_area(10) 100 >>> square_area(5.5) 30.25 """ return side ** 2 import doctest doctest.testmod() test cases function call that performs the tests COMPSCI 101 - Principles of Programming

  4. Converting between types • Converting from integer (or string) to float • float( value ) • Converting from float (or string) to integer • int ( value ) • Converting from float or integer to string • str( value ) Math functions to convert float to an int • math.floor( value ) #returns the closest int <= value • math.ceil( value ) #returns the closest int >= value COMPSCI 101 - Principles of Programming

  5. Developing a solution using functions • Two examples: • Calculate the weight of a steel tube, given the dimensions • Calculate the number of trucks required to ship a given volume of coffee beans COMPSCI 101 - Principles of Programming

  6. Calculate the weight of a steel tube • First calculate the amount (volume) of steel • Multiply the volume by the density (weight per square metre) • Q1. How do we calculate the volume of a tube? • Calculate the area of the cross-section and multiply by the length • Q2. What is the density of steel? • Google? • Information needed: • exterior radius • interior radius • length of the tube • density of steel COMPSCI 101 - Principles of Programming

  7. Cross-section of a tube (annulus) COMPSCI 101 - Principles of Programming

  8. Development steps Calculate the weight of a steel tube Calculate the volume of the tube Multiply by the density Calculate the area of the cross-section Multiply by the length Calculate the area of the outer circle Calculate the area of the inner circle Subtract the inner from the outer COMPSCI 101 - Principles of Programming

  9. Start with the building blocks • Start with a function that doesn't rely on other functions • Build up other functions using the pieces you have created • Area of a circle COMPSCI 101 - Principles of Programming

  10. Example • Given the size of a box used to hold coffee beans, and the capacity of a truck used to carry the boxes, calculate the number of trucks required to ship a given volume of coffee beans. • box size: width, height, depth in centimetres • truck capacity: width, height, depth in metres • volume of coffee beans COMPSCI 101 - Principles of Programming

  11. Development steps Calculate the number of trucks Need to know the total number of boxes Need to know how many boxes fit into one truck Calculate the number of boxes Need to know the dimensions of the box Need to know the total volume Calculate the number of boxes per truck Need to know the dimensions of the box Need to know the dimensions of the truck COMPSCI 101 - Principles of Programming

More Related