Method libraries baloon prank Method Libraries Repositories for

  • Slides: 15
Download presentation
Method libraries – baloon prank

Method libraries – baloon prank

Method Libraries • Repositories for related methods • Example: Math class • Section objective:

Method Libraries • Repositories for related methods • Example: Math class • Section objective: build two method libraries Alice in Action with Java 2

Problem Description: Ballooning a Bedroom • Problem context – Your friend who plays practical

Problem Description: Ballooning a Bedroom • Problem context – Your friend who plays practical jokes is away – You want to play a practical joke on your friend – You plan to fill your friend’s room with balloons • Question: how many balloons should you purchase • Alice. The question will be answered by a in Action with Java 3

Program Design • The problem is concerned with volumes – Find out how many

Program Design • The problem is concerned with volumes – Find out how many balloon volumes fit in a room volume • The balloon is approximated by a sphere – volumesphere = 4/3 x PI x radius 3 • The room is approximated by a box – volumebox = length x width x height • Another issue: whether to use large or Alice in Action with Java 4

Program Design (continued) • Essentials of the user story – Query the user for

Program Design (continued) • Essentials of the user story – Query the user for the radius of the balloon – Read the radius from the keyboard – Compute the volume of one balloon – Compute the volume of the bedroom • Note: dimensions of room are declared as constants – Compute number of balloons needed to fill the bedroom Alice in Action with Java – Display the required number of balloons, 5

Program Design (continued) Alice in Action with Java 6

Program Design (continued) Alice in Action with Java 6

Program Design (continued) Alice in Action with Java 7

Program Design (continued) Alice in Action with Java 7

Program Design (continued) Alice in Action with Java 8

Program Design (continued) Alice in Action with Java 8

Program Implementation • First decision: write methods to compute volumes – Rationale: methods allow

Program Implementation • First decision: write methods to compute volumes – Rationale: methods allow computations to be reused • Second decision: store methods in separate classes – Rationale: makes the program more modular • Three classes will be used to Alice in Action with Java 9

Program Implementation (continued) Alice in Action with Java 10

Program Implementation (continued) Alice in Action with Java 10

Program Implementation (continued) Alice in Action with Java 11

Program Implementation (continued) Alice in Action with Java 11

Program Implementation (continued) Alice in Action with Java 12

Program Implementation (continued) Alice in Action with Java 12

Unit Testing • The sole purpose of a test class – Ensure that methods

Unit Testing • The sole purpose of a test class – Ensure that methods in the program or library work • How to implement unit testing – Build a test class with test methods • One test method for each method in a program or library – Run the test methods • Illustration of unit testing: Alice in Action with Java 13

Unit Testing (continued) Alice in Action with Java 14

Unit Testing (continued) Alice in Action with Java 14

Test-Driven Development • Reversing the normal testing process – Build the test (this is

Test-Driven Development • Reversing the normal testing process – Build the test (this is the starting point) – Use the test to drive subsequent method development • Application to the development of methods – Method call indicates number of arguments needed – Number of arguments indicates number of 15 Alice in Action with Java