CS1109 Lab 7

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

A preliminary exercise

Do not skip over this preliminary exercise.

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

  1. A mathematician has an equation y = 2x3 + 4. Which of the following are correct PHP statements for this equation? (Several may be correct. Give all of them.)
    1. $y = 2($x * $x * $x) + 4
    2. $y = 2 * $x * $x * $x + 4
    3. $y = 2 * ($x * $x * $x + 4)
    4. $y = (2 * ($x * ($x * ($x)))) + 4
    5. $y = 2 * ($x * ($x * ($x + 4)))
  2. Evaluate each of the following as PHP would, and state the value and type of $x after each statement:
    1. $x = 4 + 3 * 4 / 6 - 5;
    2. $x = 18 / 4 + 18 % 4;
    3. $x = 3 % 3 + 3 * 3 - 3 / 3;
    4. $x = (2 * 5 * (2 + (2 * 6 / (4))));
    5. $x = 20 / 5 / 3;
    6. $x = 20 / 5.0;
    7. $x = "Happy " . 5 . "th Birthday!";
    8. $x = ('Happy ' . 6) . "th Birthday!";
    9. $x = "Happy " . (7 . 'th Birthday!');
    10. $x = 'Happy ' . (9 + 9) . 'th Birthday!';
  3. Evaluate each of the following as PHP would, and state the value and type of $x after each statement:
    1. $x = ceil(5 / 2);
    2. $x = floor(5 / 2);
    3. $x = round(5 / 2);
    4. $x = (int) (5 / 2);
    5. $x = ceil(-5 / 2);
    6. $x = floor(-5 / 2);
    7. $x = round(-5 / 2);
    8. $x = (int) (-5 / 2);
    9. $x = 4;
      if ( $x < 0 )
      {
          $x = $x * $x;
      }
      $x = $x % 5;
    10. $x = 4;
      if ( $x % 2 == 0 )
      {
          $x = $x / 2;
      }
      $x = ((int) $x) * 2.2;

This week's work: Part 1

Copy pints.html and table.css from your lab6 folder into your lab7 folder.

Write a new version of pints.php so that (a) the user's input is rounded to one decimal place, and then (b) this number is converted to millilitres, also rounded to one decimal place. You need to know that one pint is 568.26125 millilitres. For example, if the user enters 3.27 pints, the output would now be like the following:

A pint of plain is your only man
PintsMl
3.31875.3

If the user enters 3.0 pints, the output is:

A pint of plain is your only man
PintsMl
31704.8

Note that, for whole numbers, the decimal places are not displayed — that's fine; don't attempt to fix it.

This week's work: Part 2

Take a copy of height.html; do not modify it. It allows a user to enter his/her height in feet-and-inches. For example, you might enter 5 into the first textfield and 10 into the second textfield: 5 feet, 10 inches.

You must write height.php to convert from feet-and-inches to metres-and-centimetres, to the nearest centimetre.

We will assume that 1 inch = 2.54 centimetres. (You will also need to know that there are 100 centimetres in a metre, and 12 inches in a foot…but I assume you knew that.)

Here's an example of the output:

Imperial:5foot/feet10inch(es)
Metric:1metre(s)78centimetre(s)

You can link to table.css if you want to make the output look nicer.

This week's work: Part 3

PHP has a built-in function for generating random numbers (in fact, pseudo-random numbers). Each time you invoke this function, it returns a seemingly randomly-chosen integer.

The function is called rand. Look up its entry in the PHP manual: rand. It's the second prototype that we will be using in this exercise.

By writing this, we can simulate the roll of a dice:

$roll = rand(1, 6);

i.e. we use the rand function to generate a random integer between 1 and 6 inclusive, and here I have chosen to store the result that the function returns into a variable called $roll. Once you have understood this, you are ready to proceed with this exercise.

Take a copy of bakuchi.html; do not modify it. It allows the user to enter the number of eurines s/he wishes to stake in a (slightly simplified) version of the dice game known as Kitsune bakuchi.

You must write bakuchi.php which plays Kitsune bakuchi, as follows. Two dice are rolled. The user's winnings are zero eurines unless both dice show the same value; in the latter case the user's winnings are 4 times his/her stake. Echo three things: the values of both dice and the winnings.

Note to students who have studied programming before. The answer to this exercise can be written using a one-armed conditional. Do not use two-armed conditionals (if-else), which we have not covered in CS1109 yet. Do not use disjunction or conjunction (||, &&), which we have not covered yet.

This week's work: Part 4

The Government of Nerdland is contemplating the introduction of annual household water charges. In the scheme that they have in mind, every household pays a flat rate of 120 eurines, no matter how much water they use. On top of this, you pay for usage. The first 12000 gallons are taxed at 0.05 eurines per gallon. Then, for each additional 1000 gallons, the household must pay another 60 eurines. Here are some examples:

Number of gallonsTax (eurines)
0120 + 0 + 0 = 120
1000120 + (1000 * 0.05) + 0 = 170
12000120 + (12000 * 0.05) + 0 = 720
12001120 + (12000 * 0.05) + 0 = 720
12999120 + (12000 * 0.05) + 0 = 720
13000120 + (12000 * 0.05) + (1 * 60) = 780
14000120 + (12000 * 0.05) + (2 * 60) = 840

Take a copy of water.html; do not modify it. It allows a user to enter the number of gallons s/he uses in a year. You must write water.php to calculate how much tax the user will pay.

Note to students who have studied programming before. The answer to this exercise can be written using a one-armed conditional. Do not use two-armed conditionals (if-else), which we have not covered in CS1109 yet. Do not use disjunction or conjunction (||, &&), which we have not covered yet.

Deadline for Parts 1-4: 1pm, Tuesday 27th November.

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