Put your work for this lab into a new folder public_html\cs1109\lab9
.
Do not skip this exercise.
Without using your computer, answer the following.
Put your answer into a file called quiz.txt
, which you should store in your
lab9
folder.
When you have completed it, call one of us over and we will correct your
answer.
Variable $rm
contains a float denoting the magnitude
of an earthquake on the Richter scale. The following PHP tests the
value of $rm
and, depending on the value, outputs
a textual description of the severity of the earthquake
(simplified from this table).
if ( $rm < 5 ) { echo 'Light'; } elseif ( $rm < 6 ) { echo 'Moderate'; } elseif ( $rm < 7 ) { echo 'Strong'; } else { echo 'Major'; }
Which of (a)-(d) below are equivalent to the cascading
if
above?
(More than one of them may be equivalent to it. List all that you
think are equivalent to it.)
if ( $rm >= 7 ) { echo 'Major'; } elseif ( $rm >= 6 ) { echo 'Strong'; } elseif ( $rm >= 5 ) { echo 'Moderate'; } else { echo 'Light'; }
if ( $rm < 5 ) { echo 'Light'; } if ( $rm < 6 ) { echo 'Moderate'; } if ( $rm < 7 ) { echo 'Strong'; } if ( $rm >= 7 ) { echo 'Major'; }
$msg = 'Major'; if ( $rm < 7 ) { $msg = 'Strong'; } if ( $rm < 6 ) { $msg = 'Moderate'; } if ( $rm < 5 ) { $msg = 'Light'; } echo $msg;
if ( $rm < 5 ) { echo 'Light'; } elseif ( $rm >= 5 || $rm < 6 ) { echo 'Moderate'; } elseif ( $rm >= 6 || $rm < 7 ) { echo 'Strong'; } elseif ( $rm >= 7 ) { echo 'Major'; }
In the final-year of the BSc in Witchcraft at the National University of Nerdland, students are judged on their exams, continuous assessment and a project, as follows
Exams ≥ 40% | Y | Y | Y | Y | N | N | N | N |
---|---|---|---|---|---|---|---|---|
Cont. ass. ≥ 40% | Y | Y | N | N | Y | Y | N | N |
Project ≥ 40% | Y | N | Y | N | Y | N | Y | N |
Pass | × | |||||||
Resubmit cont. ass. | × | × | ||||||
Re-do project | × | × | ||||||
Fail | × | × | × | × |
Take a copy of outcome.html; do not modify
it. It allows the user to enter his/her grades.
You must write outcome.php
, which takes data from the form and tells the
student the outcome based on the table.
Try to make your program as efficient, clear and maintainable as possible. Test it carefully.
Take a copy of bmi.html; do not modify
it. It allows the user to enter his/her weight (kg) and height (metres).
You must write bmi.php
, which computes:
(There is enough information above for you to write down the formula for BMI and then, using very simple algebra, obtain the formula for the user's ideal weight. However, if you cannot work out these formulae for yourself, or you are unsure, ask us and we will help you.)
Your program should output the following:
In previous years, CS1109 students wrote a Rock-Scissors-Paper game in PHP. As you probably know, in this game:
Here are the files from one student's solution. Take a copy of them:
rsp.html
: This is the form
that allows the user to choose rock, scissors or paper. Do not
edit this file.
rsp.phps
: Save this file
as rsp.php
.
This is the PHP program that plays the game. Specifically, it:
Unfortunately, the student's PHP program is buggy. Fix it!
Important! Do not rewrite the student's program from scratch. The idea of this exercise is to work out what is wrong with the student's program and make minimal changes to fix the bug(s).
Re-acquaint yourself with Part 4 of lab sheet 7.
Here are files based on a student solution. Take a copy of them:
water.html
. Do not
edit this file.
water.phps
: Save this file
as water.php
.
Unfortunately, the student's solution is buggy. Fix it!
Important! Do not rewrite the student's program from scratch.
The idea of this exercise is to work out what is wrong with the student's program
and make minimal changes to fix the bug(s). For example, keep the same number of
if
s. You can change the Boolean expressions; you can change the
assignment statements; you can move assignment statements or insert extra
assignment statements.
In this part, you will write a Stamp Duty Calculator for residential properties in Nerdland.
Take a copy of duty.html
; do not edit it.
It contains a form that has:
You must write duty.php
, which computes
the Stamp Duty that the user will have to pay on a house purchase.
Use the following
explanation of Nerdland's Stamp Duty rules:
Try to make your program as efficient, clear and maintainable as possible. Test it carefully.
Deadline for Parts 1, 2, 3, 4 & 5 of Lab 9: 10am, Thursday 13th December. Note. Different day; different time.
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).
Remember that challenge exercises are always optional. They do not form part of your year's work and they are not worth any marks. They are designed for those students who finish the main exercise quickly and easily, and wish to explore further.
Take a copy of challenge.html
.
It's basically the same as rsp.html
.
Your challenge is to write challenge.php
, which plays the
Rock-Scissors-Paper game. The output should be the same as before, i.e.
it outputs the outcome of the game using a sentence such as
"Computer's Rock beats User's Scissors", or
"User's Paper beats Computer's Rock", etc. But the goal is to work out the outcome
with little more than a single if-elseif-else
.
Hint: Arithmetic.
Additional explanation, added 06/12/12: What I'm saying here is that you need
just an if-elseif-else
and some arithmetic to work out the
outcome, i.e. computer wins, user wins, or draw. You will need
some additional code to map from numbers to weapon names, e.g. from 0 to Rock,
etc.
Put your solution in your lab9
folder, and I will take a look at it.