CS1109 Lab 12

Put the work for this lab into a new folder public_html\cs1109\lab12.

Use whatever built-in functions you wish, unless the question explicitly says otherwise.

Part 1: Working for the Yankee dollar

In Part 5 of lab 6, you wrote a program to add up in cents how much cash an American has in his/her pockets. Here, you are going to rewrite this program to use an associative array.

Take copies of my version of the form, cash.html, and its stylesheet, cash.css, and store them in your lab12 folder; do not modify them.

Write cash.php. It should use $_GET, but also a second associative array, and foreach.

Part 2: Gyeorugi

Take a copy of class.html; do not edit it. It allows a user to enter his/her weight (in kg) and sex. You must write class.php, which outputs his/her Taekwondo weight class, based on the table that you can find on this page. For example, if a female user weighs 58kg, her class is Welterweight.

Important: Use one or more associative arrays and a foreach-loop with a simple if-statement inside it. Do not use a huge if-statement or a large number of if-statements.

Part 3: Seek the geek inside

This one doesn't involve associative arrays. It's just a short exercise to prepare you for Part 4.

We know how to create a form with radio buttons. Let's see how to create a form with checkboxes. The difference between a group of radio buttons and a group of checkboxes is that only one of the radio buttons can be selected at a time, whereas zero, one or more of the checkboxes can be selected.

The HTML for a checkbox is done with an input element with type="checkbox". Just like a group of related radio buttons, a group of related checkboxes are given the same name.

As an example, take a copy of languages.html; do not edit it. Bring it up in your browser. Look closely at the source to see how it is done. In particular, notice the square brackets that are part of the name.

Did you do what I just asked? Did you bring up the form and look at its source? Did you notice the square brackets? Do not proceed until you have done this.

Now write languages.php. Instead of this...

$languages = $_GET['languages'];

...one statement in your PHP might be:

if ( isset( $_GET['languages'] )
{
    $languages = $_GET['languages'];
}
else // the user selected none
{
    $languages = array(); // hence, we create an empty array
}

The important thing to realize is that, because of the square brackets mentioned above, the variable $languages will be an indexed array of the user's choices. E.g. if the user checked the HTML and CSS boxes, the $languages array will contain the strings HTML and CSS. (If the user selects none of the boxes, no data will be sent from the browser, and this is what the if above tests for.)

If you are struggling to make sense of this, then ask us to explain.

The remainder of your program should use a foreach loop to tell the user which languages s/he selected. So the output might be, for example,

You like: HTML CSS

or

You like: HTML CSS PHP

or

You like:

etc., depending on what was selected by the user.

Part 4: The Pants Pizza Parlour

Take a copy of pizza.html; do not edit it. With this form, the user can order a pizza.

You must write pizza.php, which outputs the cost of the pizza.

The information you need is as follows:

Part 5: There are only 10 kinds of people in the world

Take a copy of from_binary.html; do not modify it. It allows a user to enter a string of '0's and '1's. E.g. s/he might enter "1101". You can assume that your user is 100% cooperative: s/he never enters any incorrect characters.

You must write from_binary.php, which computes and outputs the positive decimal integer that corresponds to the bit string that the user has entered. For example, if the user enters "1101", your program computes the integer 13 and outputs it.

Hint: very similar to a program you have written in previous CS1109 labs. So similar, you could copy it and make minor modifications!

Useful and allowed built-in functions: str_split, str_len or count, and pow (although there is a solution that uses only the first of these). You may not use any other built-in functions; in particular, use of bindec is not allowed.

Deadline for Lab 12: 1pm, Tuesday 29th January.

If you have named your files and folders correctly, your work will be collected automatically at that time by my software.

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 to_roman.html; do not edit it. The user enters an integer into the form.

Write to_roman.php, which outputs the Roman numerals that correspond to the user's input, e.g. if the user enters 1014, your program outputs MXIV.

To make it easier, limit your program to decimal integers between 0 and 3999, inclusive. (Without this limit, you might need to use for-loops, which we haven't covered yet. Of course, you are welcome to look ahead, learn them for yourself, and use them in your solution — but this isn't required.)

Try to avoid looking for algorithms on the Web!

Leave both files in your lab12 folder, and I will take a look at them.