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

homelectshws
D2Lbreeze (snow day)

hw01
hw01: A first php program

Due: Section 01: 2016.Feb.01 (Mon) 16:00
(Section 11: Feb.02 (Tue) 18:00).

For this homework, you'll submit three files, both on D2L and as hardcopy:

Notes:


  1. 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'll get full credit for (only) handling nouns whose plural is formed by adding “s”.
    3. You don't need to include test cases for nouns whose plural isn't formed by adding “s”; that is, we're not really making a comprehensive set of test cases for this function! If you want to include a test case like pluralize(4,"goose"), note that the expected-result really is "4 geese" — and you can still get full credit w/o passing this test. (Do not, however, have a test-case claiming that the expected answer is "4 gooses: it's cheating to re-define your answer to be whatever your code gives, and then claim that your code is working! Admittedly, a better term than “expected output” would be “correct output”.)
    4. As also mentioned in standard instructions for all homeworks: 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).

1 You may have seen in Web I that text not enclosed in <?php … ?> is a short-cut for printing; we'll use that on following assignments, but for this this assignment use explicit echo statements.      

2 While the defensive-programming can be helpful in bigger projects (esp. in untyped languages like php), it's never interesting code. If you DO want to do the checks anyway, that's fine, but do all the type-validation right away (before you enter the 'real' function-logic), and throw an error if something goes wrong (don't continue and return some sort of odd, undocumented answer). For example:

function foo( $someNum ) {
  // error-check NOT required for this class, but if you want to:
  if !(is_numeric($someNum)) { throw new InvalidArgumentException( 'foo: expected number, given ' . $someNum ); }

  // ...now put your real code, uncluttered by further type-checks.
}
Ideally, you'd include the type-requirements in comments, and then a tool would automatically generate the above boilerplate error-checking for you. That's what Java's type-system gives you, for example.      

homelectshws
D2Lbreeze (snow day)


©2015, Ian Barland, Radford University
Last modified 2016.Jan.27 (Wed)
Please mail any suggestions
(incl. typos, broken links)
to ibarlandradford.edu
Rendered by Racket.