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% YYYYNNNN
Cont. ass. ≥ 40% YYNNYYNN
Project ≥ 40% YNYNYNYN
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.

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:

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

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:

Here are the files from one student's solution. Take a copy of them:

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 ifs. 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:

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.

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.