|
home—lects—hws
D2L—breeze (snow day)
Last time, we talked about writing test cases and code for a function
Before we even revisit the code, let's talk about rucs:
Be aware: rucs and ruacad are not the campus web server, nor the php server! You don't have accounts on those machines (although your H: drive is shared by those machines).
We left our program as follows:
1 <?php 2 /** Return a blend of two words: 3 * @param $word1 The word to take the first half from. 4 * @param $word2 The word to take the second half from. 5 * @return the first half of $word1 concatenated to the second half of $word2. 6 * (If a word is odd length, we round the halfway-point down.) 7 */ 8 function blend( $str1, $str2 ) { 9 $mid1 = strlen($str1)/2; 10 $mid2 = strlen($str2)/2; 11 $firstHalf = substr($str1, 0, $mid1 ); 12 $secondHalf = substr($str2, $mid2 ); 13 return $firstHalf . $secondHalf; 14 } 15 16 17 18 19 20 21 /* 22 function blend( $word1, $word2 ) { 23 $fHalf = substr($word1, 0, strlen($word1)/2 ); 24 $lHalf = substr( $word2, strlen($word2)/2 ); 25 return $fHalf . $lHalf; 26 } 27 28 */ 29 30 // Run our tests: 31 echo "\nexpect: " . "motel"; 32 echo "\nactual: " . blend("motor", "hotel"); 33 34 echo "\nexpect: " . "smog"; 35 echo "\nactual: " . blend("smoke", "fog"); 36 37 echo "\nexpect: " . "Iland"; 38 echo "\nactual: " . blend("Ian", "Barland"); 39 40 echo "\nexpect: " . ""; 41 echo "\nactual: " . blend("a", ""); 42 43 echo "\nexpect: " . "a"; 44 echo "\nactual: " . blend("", "a"); 45 46 echo "\nexpect: " . "bc"; 47 echo "\nactual: " . blend("", "abc"); 48 49 echo "\nexpect: " . "a"; 50 echo "\nactual: " . blend("abc", ""); 51 52 echo "\nexpect: " . ""; 53 echo "\nactual: " . blend("", ""); 54 55 echo "\nexpect: " . "1"; 56 echo "\nactual: " . blend("a", "1"); 57 58 echo "\nexpect: " . "a2"; 59 echo "\nactual: " . blend("ab", "12"); 60 61 62 63 echo "\n"; 64 echo " Hi Ian Barland -- I shall call you ", blend("Ian","Barland"), "\n"; 65 echo " (How do you like that?)\n"; 66 ?> |
We can use
1 <?php 2 3 /** Return a blend of two words: 4 * @param $word1 The word to take the first half from. 5 * @param $word2 The word to take the second half from. 6 * @return the first half of $word1 concatenated to the second half of $word2. 7 * (If a word is odd length, we round the halfway-point down.) 8 */ 9 function blend( $word1, $word2 ) { 10 $mid1 = strlen($word1)/2; 11 $mid2 = strlen($word2)/2; 12 $firstHalf = substr($word1, 0, $mid1 ); 13 $secondHalf = substr($word2, $mid2 ); 14 return $firstHalf . $secondHalf; 15 } 16 17 18 /* We could equally well write use two fewer variables: 19 20 function blend( $word1, $word2 ) { 21 $fHalf = substr($word1, 0, strlen($word1)/2); 22 $lHalf = substr($word2, strlen($word2)/2); 23 return $fHalf . $lHalf; 24 } 25 26 Or even two fewer variables: 27 28 function blend( $word1, $word2 ) { 29 return substr($word1, 0, strlen($word1)/2) . substr($word2, strlen($word2)/2); 30 } 31 */ 32 33 ?> |
1 <?php 2 3 require_once( 'blend.php' ); 4 5 6 // Run our tests: 7 echo "\nexpect: ", "motel"; 8 echo "\nactual: ", blend("motor", "hotel"); 9 10 echo "\nexpect: ", "smog"; 11 echo "\nactual: ", blend("smoke", "fog"); 12 13 echo "\nexpect: ", "Iland"; 14 echo "\nactual: ", blend("Ian", "Barland"); 15 16 echo "\nexpect: ", ""; 17 echo "\nactual: ", blend("a", ""); 18 19 echo "\nexpect: ", "a"; 20 echo "\nactual: ", blend("", "a"); 21 22 echo "\nexpect: ", "bc"; 23 echo "\nactual: ", blend("", "abc"); 24 25 echo "\nexpect: ", "a"; 26 echo "\nactual: ", blend("abc", ""); 27 28 echo "\nexpect: ", ""; 29 echo "\nactual: ", blend("", ""); 30 31 echo "\nexpect: ", "1"; 32 echo "\nactual: ", blend("a", "1"); 33 34 echo "\nexpect: ", "a2"; 35 echo "\nactual: ", blend("ab", "12"); 36 ?> |
1 <?php 2 require_once('blend.php'); 3 4 echo "<html>\n"; 5 echo " <head>\n"; 6 echo " <title>Name Blender</title>\n"; 7 echo " </head>\n"; 8 echo ""; 9 echo " <body>\n"; 10 echo " <p>\n"; 11 echo " Hi Ian Barland -- I shall call you ", blend("Ian","Barland"), "\n"; 12 echo " (How do you like that?)\n"; 13 echo " </p>\n"; 14 echo " </body>\n"; 15 echo "</html>\n"; 16 ?> |
home—lects—hws
D2L—breeze (snow day)
©2013, Ian Barland, Radford University Last modified 2014.Jan.24 (Fri) |
Please mail any suggestions (incl. typos, broken links) to ibarlandradford.edu |