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

homelectshws
D2Lbreeze (snow day)

hw01
hw01: A first php program

Due 2017.Jan.31 (Tue) at the start of class.
Submit on D2L, and (for in-class section) a hardcopy.
For this homework, you'll submit one file which contains three functions and their test-cases.

Notes:

  1. (10pts) Write a php function string pluralize( int $num, string $noun ) which returns a user-friendly version of a number+noun1. 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.
      (A small amount of extra credit: incorporate some 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, a non-string 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 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 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 the link would 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!


1 Since the noun may or may not be actually plural, depending on the 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 trivially in compliance 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.Jan.30 (Mon)
Please mail any suggestions
(incl. typos, broken links)
to ibarlandradford.edu
Rendered by Racket.