Lab 9, Part 4 ------------- This was the solar panels exercise. It was marked out of 10. Students often complain that exercises are not "real world" enough. This one was "real-world": I simplified the rules from the UK Government's solar panel scheme. When you see how low your mark is, maybe you'll be more ready to put up with exercises that are not "real world"! Key to getting it right were: a) Understand the problem. Step (b) helps with this. b) Produce test data, i.e. produce example inputs and, for each example input, manually work out the expected output. How can you know whether your program is correct if you don't know what output to expect? I don't usually provide a model solution - because there are billions of correct answers. But some of you have done so badly on this one that I'm making an exception. Here's one good solution: $cost = (int) $_GET['cost']; $type = $_GET['type']; $size = $_GET['size']; $location = $_GET['location']; $consumption_kW_per_annum = 4500; if ( $location == 'north' ) { $sunshine_hrs_per_annum = 1000; } else // $location == 'south' { $sunshine_hrs_per_annum = 1200; } $grant_per_kWh = 0.1; if ( $size == 'small' ) { $production_kWh = 2; if ( $type == 'retrofit' ) { $grant_per_kWh = 0.12; } } elseif ( $size == 'medium' ) { $production_kWh = 4; } else // $size == 'large' { $production_kWh = 8; $grant_per_kWh = 0.06; } $production_kW_per_annum = $sunshine_hrs_per_annum * $production_kWh; $grant_per_annum = $production_kW_per_annum * $grant_per_kWh; if ( $production_kW_per_annum > $consumption_kW_per_annum ) // you produced more than you consumed { $production_used_kW_per_annum = $consumption_kW_per_annum; $production_exported_kW_per_annum = $production_kW_per_annum - $consumption_kW_per_annum; } else // you produced less than or equal to what you consumed { $production_used_kW_per_annum = $production_kW_per_annum; $production_exported_kW_per_annum = 0; } $saved_per_annum = $production_used_kW_per_annum * 0.14; $paid_per_annum = $production_exported_kW_per_annum * 0.03; $benefit_per_annum = $grant_per_annum + $saved_per_annum + $paid_per_annum; $total_benefit = $benefit_per_annum * 10; // 10 years $profit = $total_benefit - $cost; // Now echo stuff Note how each calculation appears only once. Notes to advanced students: - You can shorten quite a bit by using the conditional operator, e.g. $sunshine_hrs_per_annum = $location == 'north' ? 1000 : 1200; - You can also replace the last if-else with an imaginative use of PHP's built-in min and max functions. This makes the program much shorter but less efficient and less clear.