CS1109 Lab 8

Put your work for this lab into a new folder public_html\cs1109\lab8.

A preliminary exercise

Do not skip this exercise.

Without using your computer, answer the following. Put your answers into a file called quiz.txt, which you should store in your lab8 folder. When you have completed them, call one of us over and we will correct your answers.

  1. Suppose you have been asked to write a PHP script as follows:
    • if $nationality is Nerd or Geek, then add 1 to $count
    But you write the following:
    $count = 0;
    if ( $nationality == 'Nerd' || 'Geek' )
    {
        $count = $count + 1;
    }
    This is not correct. Write a correct version.
  2. Suppose you have been asked to write a PHP script as follows:
    • if $x is between 1 and 10 inclusive, i.e. 1 ≤ $x ≤ 10, then add 1 to $count
    But you write the following:
    $count = 0;
    if ( 1 <= $x <= 10 )
    {
        $count = $count + 1;
    }
    This is not correct. Write a correct version.
  3. Simplify these Boolean expressions as much as you can by eliminating the separate negation operators (!). (Hint: for the last three of them recall de Morgan's Laws from CS1105.)
    1. if ( ! $x != 3 )
    2. if ( ! $x > 3 )
    3. if ( ! $x >= 3 )
    4. if ( ! ($y == 4 && $y == 5) )
    5. if ( ! ($y == 4 || $y == 5) )
    6. if ( ! ($z != 12 || $z < 3) )
  4. Consider this nested-if:
    if ( $x < 3)
    {
        if ( $y == 10 || $y == 20 )
        {
            echo 'OK';
        }
    }
    Which of (a)-(d) below are equivalent to the nested-if above? (More than one of them may be equivalent to it. List all that you think are equivalent to it.)
    1. if ( ($x < 3 && $y == 10) || ($x < 3 && $y == 20) )
      {
          echo 'OK';
      }
    2. if ( $x < 3 )
      {
          if ( $y == 10 )
          {
              echo 'OK';
          }
          if ( $y == 20 )
          {
              echo 'OK';
          }
      }
    3. if ( $y == 10 || $y == 20 )
      {
          if ( $x < 3 )
          {
              echo 'OK';
          }
      }
    4. if ( $y == 10 && $x < 3 )
      {
          echo 'OK';
      }
      if ( $y == 20 && $x < 3 )
      {
          echo 'OK';
      }

This week's work: Part 1

In lab7, you wrote PHP for a slightly simplified game of Kitsune bakuchi. We will improve this now. Take a copy of bakuchi1.html; do not modify it. As before, it allows the user to enter the number of eurines s/he wishes to stake.

You must write bakuchi1.php, which plays Kitsune bakuchi, as follows. Three (not two!) dice are rolled. The user's winnings are zero eurines unless all three dice show the same value; in the latter cases the user's winnings are 4 times his/her stake. Echo the values of the three dice and the winnings.

Important. I want you to write the answer to this exercise using several one-armed conditionals that may be nested and/or in sequence. Do not use disjunction of conjunction (||, &&). And do not use two-armed conditionals (if-else), which we have not covered in CS1109 yet.

This week's work: Part 2

Take a copy of bakuchi2.html; do not modify it. Again it allows the user to enter the number of eurines s/he wishes to stake in a dice game. You must write bakuchi2.php. It plays exactly the same dice game as in Part 1 above. However, this time you are allowed to use just a single one-armed conditional (no nesting, no sequence of conditionals). But you can use conjunction (&&) and/or disjunction (||).

This week's work: Part 3

Warning: quite challenging!

Take a copy of sort.html; do not modify it. The user enters into the form three integers (in any order). Write sort.php, which echoes the three integers in descending order. E.g. if the user inputs 12, 3, 14, your script outputs 14, 12, 3; or if the user enters 5, 4, 5, your script outputs 5, 5, 4; and so on. Try to make your if-statement(s) as elegant as possible.

Test your program carefully.

Hint. Suppose we want to swap the values that are stored in variables $x and $y, so that what used to be stored in $x is now in $y, and what used to be stored in $y is now in $x. Here's how to do it:

$tmp = $x;
$x = $y;
$y = $tmp;

You can see we use an extra variable to temporarily hold the value that was in $x so that we don't lose it when, in the second line, we copy the value of $y into $x.

Note. The answer to this exercise can be written using one-armed conditionals. Do not use two-armed conditionals (if-else), which we have not covered in CS1109 yet. Also, do not use built-in PHP functions, such as the sort function.

This week's work: Part 4

Take a copy of votes.html; do not modify it. It provides a form with three groups of radio buttons so that users can indicate whether they are adults or not, whether they are resident in Ireland or not, and their nationality.

Write votes.php, which outputs an unordered list of the user's voting rights, i.e. zero, one or more of the following: Local, European, Dáil, Presidential, and Referenda.

Adult residents can vote, but only as follows:

Try to make your if-statements as elegant as possible.

Make sure the output validates. Test your program carefully.

Note. The answer to this exercise can be written using one-armed conditionals. Do not use two-armed conditionals (if-else), which we have not covered in CS1109 yet.

Deadline for Lab 8 Parts 1-4: 1pm, Tuesday 4th December.

If you have named your files and folders correctly, your work will be collected automatically at that time by my software.

Part 5: On-going work for the next few weeks

Resume work on your personal web site (Part 6 of Lab 6).