Put your work for this lab into a new folder public_html\cs1109\lab7
.
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.
$y = 2($x * $x * $x) + 4
$y = 2 * $x * $x * $x + 4
$y = 2 * ($x * $x * $x + 4)
$y = (2 * ($x * ($x * ($x)))) + 4
$y = 2 * ($x * ($x * ($x + 4)))
$x
after each statement:
$x = 4 + 3 * 4 / 6 - 5;
$x = 18 / 4 + 18 % 4;
$x = 3 % 3 + 3 * 3 - 3 / 3;
$x = (2 * 5 * (2 + (2 * 6 / (4))));
$x = 20 / 5 / 3;
$x = 20 / 5.0;
$x = "Happy " . 5 . "th Birthday!";
$x = ('Happy ' . 6) . "th Birthday!";
$x = "Happy " . (7 . 'th Birthday!');
$x = 'Happy ' . (9 + 9) . 'th Birthday!';
$x
after each statement:
$x = ceil(5 / 2);
$x = floor(5 / 2);
$x = round(5 / 2);
$x = (int) (5 / 2);
$x = ceil(-5 / 2);
$x = floor(-5 / 2);
$x = round(-5 / 2);
$x = (int) (-5 / 2);
$x = 4;
if ( $x < 0 )
{
$x = $x * $x;
}
$x = $x % 5;
$x = 4;
if ( $x % 2 == 0 )
{
$x = $x / 2;
}
$x = ((int) $x) * 2.2;
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:
Pints | Ml |
---|---|
3.3 | 1875.3 |
If the user enters 3.0 pints, the output is:
Pints | Ml |
---|---|
3 | 1704.8 |
Note that, for whole numbers, the decimal places are not displayed — that's fine; don't attempt to fix it.
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: | 5 | foot/feet | 10 | inch(es) |
---|---|---|---|---|
Metric: | 1 | metre(s) | 78 | centimetre(s) |
You can link to table.css
if you want to make the output look nicer.
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.
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 gallons | Tax (eurines) |
---|---|
0 | 120 + 0 + 0 = 120 |
1000 | 120 + (1000 * 0.05) + 0 = 170 |
12000 | 120 + (12000 * 0.05) + 0 = 720 |
12001 | 120 + (12000 * 0.05) + 0 = 720 |
12999 | 120 + (12000 * 0.05) + 0 = 720 |
13000 | 120 + (12000 * 0.05) + (1 * 60) = 780 |
14000 | 120 + (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.
Resume work on your personal web site (Part 6 of Lab 6).