|
| 1 | +<html> |
| 2 | + <head> |
| 3 | + <title>Test-First Teaching: learn_ruby: calculator</title> |
| 4 | + <link href="../assets/style.css" media="screen" rel="stylesheet" type="text/css" /> |
| 5 | + </head> |
| 6 | + <body> |
| 7 | + <div class="header"> |
| 8 | + <h1><a href="http://testfirst.org">TestFirst.org</a></h1> |
| 9 | + <h2>the home of test-first teaching</h2> |
| 10 | + </div> |
| 11 | + <div class="nav"> |
| 12 | + <h2><a href="../index.html">learn_ruby</a></h2> |
| 13 | + <b>Labs:</b> |
| 14 | + <ul> |
| 15 | + <li><a href="../00_hello/index.html">00 Hello</a></li> |
| 16 | + <li><a href="../01_temperature/index.html">01 Temperature</a></li> |
| 17 | + <li>02 Calculator</li> |
| 18 | + <li><a href="../03_simon_says/index.html">03 Simon Says</a></li> |
| 19 | + <li><a href="../04_pig_latin/index.html">04 Pig Latin</a></li> |
| 20 | + <li><a href="../05_silly_blocks/index.html">05 Silly Blocks</a></li> |
| 21 | + <li><a href="../06_performance_monitor/index.html">06 Performance Monitor</a></li> |
| 22 | + <li><a href="../07_hello_friend/index.html">07 Hello Friend</a></li> |
| 23 | + <li><a href="../08_book_titles/index.html">08 Book Titles</a></li> |
| 24 | + <li><a href="../09_timer/index.html">09 Timer</a></li> |
| 25 | + <li><a href="../10_temperature_object/index.html">10 Temperature Object</a></li> |
| 26 | + <li><a href="../11_dictionary/index.html">11 Dictionary</a></li> |
| 27 | + <li><a href="../12_rpn_calculator/index.html">12 Rpn Calculator</a></li> |
| 28 | + <li><a href="../13_xml_document/index.html">13 Xml Document</a></li> |
| 29 | + <li><a href="../14_array_extensions/index.html">14 Array Extensions</a></li> |
| 30 | + <li><a href="../15_in_words/index.html">15 In Words</a></li> |
| 31 | + </ul> |
| 32 | + </div> |
| 33 | + <h1>calculator</h1> |
| 34 | + <div class="content"><div class="rspec_file"> <div class="intro"><h1>Topics</h1> |
| 35 | + |
| 36 | +<ul> |
| 37 | +<li>functions</li> |
| 38 | +<li>math</li> |
| 39 | +<li>arrays</li> |
| 40 | +<li>iterating/looping</li> |
| 41 | +</ul> |
| 42 | + |
| 43 | + |
| 44 | +<h1>Calculator</h1> |
| 45 | + |
| 46 | +<p>you will build a simple calculator script with the following methods:</p> |
| 47 | + |
| 48 | +<p><code>add</code> takes two parameters and adds them</p> |
| 49 | + |
| 50 | +<p><code>subtract</code> takes two parameters and subtracts the second from the first</p> |
| 51 | + |
| 52 | +<p><code>sum</code> takes an <em>array</em> of parameters and adds them all together</p> |
| 53 | + |
| 54 | +<h1>Warning</h1> |
| 55 | + |
| 56 | +<p>You may not have enough knowledge yet to complete <code>sum</code>. You will probably |
| 57 | +need to use <strong>loops</strong> (e.g. <code>while</code>) or <strong>iterators</strong> (e.g. <code>each</code>) to |
| 58 | +get the tests to pass.</p> |
| 59 | + |
| 60 | +<h1>Bonus</h1> |
| 61 | + |
| 62 | +<p>There are also some bonus exercises for when you finish the regular ones. The bonus round will help teach you test-driven <em>development</em>, not simply test-guided <em>learning</em>.</p> |
| 63 | + |
| 64 | +<p>Your mission, should you choose to accept it, is to write <em>tests</em> for three new methods:</p> |
| 65 | + |
| 66 | +<ul> |
| 67 | +<li><code>multiply</code> which multiplies two numbers together</li> |
| 68 | +<li><code>power</code> which raises one number to the power of another number</li> |
| 69 | +<li><code>[factorial](http://en.wikipedia.org/wiki/Factorial)</code> (check Wikipedia if you forgot your high school math).</li> |
| 70 | +</ul> |
| 71 | + |
| 72 | +</div> |
| 73 | + <div class="tests"> |
| 74 | + <h1>Tests</h1> |
| 75 | +<a class="raw_file" href="calculator_spec.rb">calculator_spec.rb</a> |
| 76 | + <pre> |
| 77 | +require "calculator" |
| 78 | + |
| 79 | +describe "add" do |
| 80 | + it "adds 0 and 0" do |
| 81 | + add(0,0).should == 0 |
| 82 | + end |
| 83 | + |
| 84 | + it "adds 2 and 2" do |
| 85 | + add(2,2).should == 4 |
| 86 | + end |
| 87 | + |
| 88 | + it "adds positive numbers" do |
| 89 | + add(2,6).should == 8 |
| 90 | + end |
| 91 | +end |
| 92 | + |
| 93 | +describe "subtract" do |
| 94 | + it "subtracts numbers" do |
| 95 | + subtract(10,4).should == 6 |
| 96 | + end |
| 97 | +end |
| 98 | + |
| 99 | +describe "sum" do |
| 100 | + it "computes the sum of an empty array" do |
| 101 | + sum([]).should == 0 |
| 102 | + end |
| 103 | + |
| 104 | + it "computes the sum of an array of one number" do |
| 105 | + sum([7]).should == 7 |
| 106 | + end |
| 107 | + |
| 108 | + it "computes the sum of an array of two numbers" do |
| 109 | + sum([7,11]).should == 18 |
| 110 | + end |
| 111 | + |
| 112 | + it "computes the sum of an array of many numbers" do |
| 113 | + sum([1,3,5,7,9]).should == 25 |
| 114 | + end |
| 115 | +end |
| 116 | + |
| 117 | +# Extra Credit Test-Driving Bonus: |
| 118 | +# once the above tests pass, |
| 119 | +# write tests and code for the following: |
| 120 | + |
| 121 | +describe "#multiply" do |
| 122 | + |
| 123 | + it "multiplies two numbers" |
| 124 | + |
| 125 | + it "multiplies several numbers" |
| 126 | + |
| 127 | +end |
| 128 | + |
| 129 | +describe "#power" do |
| 130 | + it "raises one number to the power of another number" |
| 131 | +end |
| 132 | + |
| 133 | +# http://en.wikipedia.org/wiki/Factorial |
| 134 | +describe "#factorial" do |
| 135 | + it "computes the factorial of 0" |
| 136 | + it "computes the factorial of 1" |
| 137 | + it "computes the factorial of 2" |
| 138 | + it "computes the factorial of 5" |
| 139 | + it "computes the factorial of 10" |
| 140 | +end</pre> |
| 141 | + </div> |
| 142 | +</div> |
| 143 | +</div> |
| 144 | + <div class="footer"><a href="http://testfirst.org">TestFirst.org</a></div> |
| 145 | + </body> |
| 146 | +</html> |
0 commit comments