RU beehive logo ITEC dept promo banner
ITEC 325
2013spring
ibarland

homelectsexamshws
D2Lbreeze (snow day)

hw02
hw02: A first php program

Due Feb.07 (Thu) 23:59.

For this homework, make a folder itec325/hw02 on your H: drive. Inside this directory, you'll have two files for this assignment: utils.php that will contain php function definitions only, and utils-test.php that will contain test-cases.

Note that for this assignment, we are not making any web pages! We are simply writing php functions which return strings; some those strings just happen to have angle-brackets in them, and they happen to be html elements, but we're not (yet) including that into any web page.

  1. (10pts) Write a php function string pluralize( int $num, string $noun ) which returns a user-friendly version of a number+noun: pluralize( 7, "t-shirt" ) === "7 t-shirts". We'll use this function on future web-pages, when (say) confirming somebody's order.
    1. Be sure to include at least three test cases, each covering a different situation.
      (The noun will never be the empty string, so you don't even need to check for that. But what other corner-cases should you handle? (Many students lose points by not thinking through test cases closely enough, and then not having code that covers such corner cases.)
    2. You can presume that a plural is always formed by just adding “s” to any word.
      (Extra credit, open-ended: incorporate more-correct rules about how plurals are formed in English.)
  2. (10pts) Write a function string hyperlink( string $url, string-or-boolean $linkTxt ) which returns a string of html. If the provided $linkTxt is false (instead of a string), then use the $url as the link text.
    hyperlink( "http://www.gutenberg.org", "free books!" ) === "<a href='http://www.gutenberg.org'>free books!</a>"
    hyperlink( "myLocalFile.html", false ) === "<a href='myLocalFile.html'>myLocalFile.html</a>"

    Note that this is just a regular ol' function that takes in strings and returns strings!
    The fact that the strings happen to have angle-brackets in them means that the result might be useful to people generating html, but the function itself does't really care.

    (You don't need to come up with additional test cases, but you do need to actually include these tests or their equivalent. You can assume that neither string is ever empty.)

    Note how the input strings don't contain any quote-characters, but the returned result does (since HTML attributes must be quoted). I recommend that your generated html uses single quote marks around attributes, but you'll get full credit for either single- or double-quote marks, as long as it is valid HTML. Make sure your test case(s) express exactly what you want the desired output should be!

  3. (10pts) Thumbnails:
    1. What is the html element for an image with source pony.jpg (in the same directory), that is 300px wide?
      Include your answer in a comment, in your test-file.
    2. What is the html for such an image which is also a link — where the link would take you to that file (not a html page — just that file).
      Include your answer in a comment, in your test-file.
    3. Make a test-case for thumbnail("pony.jpg",300); the expected-output should be a string that happened to be your answer.
      You don't need further test-cases for this problem.

      Pay close attention to the exact spacing you want to have. I recommend using single-quotes (') around the attribute-values. (We'll discuss this more on Monday.)

    4. Finally, implement the function thumbnail that takes in a string (a URL) and a number (the width in pixels), and returns a string that is an html link containing an image.

      For full credit, your function should call hyperlink as already written!

homelectsexamshws
D2Lbreeze (snow day)


©2012, Ian Barland, Radford University
Last modified 2013.Feb.02 (Sat)
Please mail any suggestions
(incl. typos, broken links)
to ibarlandradford.edu
Powered by PLT Scheme