# CS1109 Lab 9

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

# A preliminary exercise

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

1. ```if ( \$rm >= 7 )
{
echo 'Major';
}
elseif ( \$rm >= 6 )
{
echo 'Strong';
}
elseif ( \$rm >= 5 )
{
echo 'Moderate';
}
else
{
echo 'Light';
}```
2. ```if ( \$rm < 5 )
{
echo 'Light';
}
if ( \$rm < 6 )
{
echo 'Moderate';
}
if ( \$rm < 7 )
{
echo 'Strong';
}
if ( \$rm >= 7 )
{
echo 'Major';
}```
3. ```\$msg = 'Major';
if ( \$rm < 7 )
{
\$msg = 'Strong';
}
if ( \$rm < 6 )
{
\$msg = 'Moderate';
}
if ( \$rm < 5 )
{
\$msg = 'Light';
}
echo \$msg;```
4. ```if ( \$rm < 5 )
{
echo 'Light';
}
elseif ( \$rm >= 5 || \$rm < 6 )
{
echo 'Moderate';
}
elseif ( \$rm >= 6 || \$rm < 7 )
{
echo 'Strong';
}
elseif ( \$rm >= 7 )
{
echo 'Major';
}```

# Part 1: Burn the witch?

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% Cont. ass. ≥ 40% Project ≥ 40% Pass Resubmit cont. ass. Re-do project Fail Y Y Y Y N N N N Y Y N N Y Y N N Y N Y N Y N Y N × × × × × × × × ×

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.

# Part 2: Does my BMI look big in this?

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:

• the user's Body Mass Index (BMI), which is the ratio of his/her weight to the square of his/her height; and
• the user's ideal weight, i.e. the amount that a person of this height would need to weigh to achieve a BMI of 21.75.

(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:

• the user's BMI;
• the user's ideal weight; and
• the user's BMI category based on the first two columns of this table. (N.B. This table is ambiguous. In what way? You'll need to decide how to resolve this ambiguity.)

# Part 3: Rock-Scissors-Paper

In previous years, CS1109 students wrote a Rock-Scissors-Paper game in PHP. As you probably know, in this game:

• Rock beats Scissors (it blunts them);
• Scissors beats Paper (they cut it); and
• Paper beats Rock (it wraps it).

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:
1. simulates the computer's choice of Rock, Scissors or Paper by generating a random number, 0, 1 or 2, where 0 means Rock, 1 means Scissors and 2 means Paper;
2. compares the user's input to the computer's choice; and
3. outputs the outcome using a sentence such as "Computer's Rock beats User's Scissors", or "User's Paper beats Computer's Rock", etc.

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

# Part 4: That bloody water tax thing again

Re-acquaint yourself with Part 4 of lab sheet 7.

Here are files based on a student solution. Take a copy of them:

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.

# Part 5: Stamp Duty

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:

• a pair of radio buttons: New House, and Second-Hand House
• a pair of radio buttons: Owner Occupier, and Investor
• a text field: Property Price (eurines)
• a text field: Property Area (square metres)

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:

• All new properties are exempt from Stamp Duty, provided they are bought by a first time buyer for owner occupation, regardless of size and price.
• New properties are also exempt from Stamp Duty if they measure less than 125 square metres, irrespective of who buys them.
• In all other cases, Stamp Duty is paid as follows:
• Stamp Duty is paid at 0% for the first 125,000 eurines
• Stamp Duty is paid at 7% for the next 875,000 eurines
• Stamp Duty is paid at 9% for the remainder

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.

# Part 6: On-going work for the next few weeks

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

# Challenge exercise

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.