RU beehive logo ITEC dept promo banner
ITEC 325
2017fall
ibarland

homelectshws
D2Lbreeze (snow day)

hw01
hw01: A first php program

Due 2017.Sep.11 (Mon) at the start of class.
Submit on D2L, and a hardcopy.
For this homework, you'll submit three files: utils.php, utils-test.php, and silly.php. The files silly.php does not need to be viewable on the web.

Notes:

  1. (10pts) Write a php function pluralize which takes in any number, and a singular noun, and returns a user-friendly version of that number-and-noun1. For example, pluralize( 7, "t-shirt" ) === "7 t-shirts". We might use this function on future web-pages, when (say) confirming somebody's order, or generating a message about the current tempurature: the number might well come from the database, and we want to make sure we create a message with correct-grammar, for any possible number.
    1. Be sure to include at least three test cases, each covering a different situation.
      The provided string will always be a singular-noun (in particular, it will never be the empty string), so you don't need to check for that. But what other corner-cases should you handle?
    2. You can presume that a plural is always formed by just adding “s” to any word.
      (A small amount of extra credit: incorporate a few more-correct rules about how plurals are formed in English.
      You can also be thankful that we're not writing this function for Finnish, or Welsh, or Arabic, or Turkish, or ….)
    3. Note that you can include test-cases that don't pass — e.g. the expected output for pluralize(4,"goose") must be "4 geese"; don't brain-damage tests just so they appear to pass! (Admittedly, better term than “expected output” would be “desired output”.)
      But you don't need write a comprehensive set of tests for this problem, fortunately.
    4. You do not need to check for being given a non-number for the first argument (or, not a singular-noun for the second); that's what a type system would check/guarantee (and in a big project, you might add type-annotations and run-time type-checking, if you valued a consistent approach to correctness).
  2. (10pts) Write a function hyperlink($url, $linkTxt ) which takes in a string $url and a string-or-boolean $linkTxt, and returns returns a string of html for a hyperlink to $url. If the provided $linkTxt is false (instead of a string), then use the $url as the link-text as well.
    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 rendered 300px wide?
      Include your answer in a comment, next to your test-case (from part (c) below).
      Note that you don't actually need a picture; we're just asking for the html-element for an image.
    2. What is the html for such an image which is also a link — where clicking the image takes you to that image-file (not an html page — just that image-file).
      Include your answer in a comment, next to your test-case (from part (c) below).
    3. Turn your previous answer into a (runnable) 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.

      Do, however, 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 in week2b's lecture.)

    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 image2.

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

  4. (5pts) If you haven't already: move your test-cases for these functions to utils-test.php, leaving the definitions in utils.php. (You'll use require_once as in lecture, so that utils-test.php is able to get the definitions in utils.php.
  5. (5pts) Make a file silly.php which:
    1. Prints a web-page. A silly, exceedingly short web-page.
      Of course, it should be proper (x)html(5), as one of the standard-requirements for this class. Mostly, that means making sure every tag closed (including br and p tags), and any attributes should have a value which is enclosed in quotes).
    2. It must call pluralize, hyperlink, and thumbnail somewhere.
    3. It should contain “<?phpexactly once at the very top, end with “?>”, and be filled with echo statements in between.
    4. Yes, this is exactly like what we did in lecture, with sample.php (and our function blend which we put inside utils.php next to utils-test.php).

1 Since the noun may or may not be actually get pluralized, depending on the provided number, some students don't like the name “pluralize”. In that case, you are free to name it “numberAndNounWhereTheNounAgreesWithTheNumberWhetherPluralOrSingularOrWhatever” instead. In general though, it's not uncommon to say that the general act of correctly-pluralizing includes specific “trivial” cases, in the same way “paying taxes” can plausibly be interepreted to include the case where one pays zero taxes (and hence has completed the task trivially), and if the police tell all the partiers to “go home”, the host might be in compliance trivially even though they didn't go anywhere.

Let me know if you have a better name for the function; an ibuck awarded if I accept it.

     
2 Since the width can be as big as you like, sometimes students don't think of a (say) 300px-wide image as a thumbnail. In that case, you are free to name it “imageOfGivenWidthWhichWhenClickedOnGivesYouTheOriginalFullSizeImage” instead. In general though, it's not unreasonable to name a task after its most common use-case, and also realize that it can be used in mildly-counterintuitive ways as well.

Let me know if you have a better name for the function; an ibuck awarded if I accept it.

     

homelectshws
D2Lbreeze (snow day)


©2017, Ian Barland, Radford University
Last modified 2017.Sep.07 (Thu)
Please mail any suggestions
(incl. typos, broken links)
to ibarlandradford.edu
Rendered by Racket.