Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
121 changes: 118 additions & 3 deletions starter/lab3_task1.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@
* ICS 2371 — Lab 3: Control Structures I
* Task 1: Simple if and if-else — Warm-Up Exercises [5 marks]
*
* @author [Your Full Name]
* @student [Your Reg Number, e.g. SCT212-XXXX/2024]
* @author [Collins Njoroge Muchiri]
* @student [ENE212-0059/2023]
* @lab Lab 3 of 14
* @unit ICS 2371
* @date [Date completed]
* @date [Date complete 02/04/2026]
*/

// ══════════════════════════════════════════════════════════════
Expand All @@ -23,6 +23,41 @@
// TODO: Exercise A — your code here


// Exercise A: Temperature Check

echo "<h3>Exercise A - Temperature Alert System</h3>";

// Test case 1: 39.2
$temperature = 39.2;
$result = "";
if ($temperature >= 36.1 && $temperature <= 37.5) { $result = "Normal"; }
if ($temperature > 37.5) { $result = "Fever"; }
if ($temperature < 36.1) { $result = "Hypothermia Warning"; }
echo "Temperature: {$temperature}°C - {$result}<br>";

echo "<br>";

// Test case 2: 36.8
$temperature = 36.8;
$result = "";
if ($temperature >= 36.1 && $temperature <= 37.5) { $result = "Normal"; }
if ($temperature > 37.5) { $result = "Fever"; }
if ($temperature < 36.1) { $result = "Hypothermia Warning"; }
echo "Temperature: {$temperature}°C - {$result}<br>";

echo "<br>";

// Test case 3: 34.5
$temperature = 34.5;
$result = "";
if ($temperature >= 36.1 && $temperature <= 37.5) { $result = "Normal"; }
if ($temperature > 37.5) { $result = "Fever"; }
if ($temperature < 36.1) { $result = "Hypothermia Warning"; }
echo "Temperature: {$temperature}°C - {$result}<br>";

echo "<br><hr><br>";


// ══════════════════════════════════════════════════════════════
// EXERCISE B — Even or Odd
// ══════════════════════════════════════════════════════════════
Expand All @@ -32,6 +67,39 @@

// TODO: Exercise B — your code here

// Exercise B

echo "<h3>Exercise B - Even or Odd with Divisibility</h3>";

$number = 47;

// Check if number is even or odd
if ($number % 2 == 0) {
echo "$number is EVEN<br>";
} else {
echo "$number is ODD<br>";
}

// Check divisibility
if ($number % 3 == 0) {
echo "$number is divisible by 3<br>";
} else {
echo "$number is NOT divisible by 3<br>";
}

if ($number % 5 == 0) {
echo "$number is divisible by 5<br>";
} else {
echo "$number is NOT divisible by 5<br>";
}

if ($number % 3 == 0 && $number % 5 == 0) {
echo "$number is divisible by both 3 and 5<br>";
} else {
echo "$number is NOT divisible by both 3 and 5<br>";
}

echo "<br><hr><br>";

// ══════════════════════════════════════════════════════════════
// EXERCISE C — Comparison Chain
Expand All @@ -50,21 +118,68 @@

// Your explanation of each result goes in your PDF report (not here).

// Exercise C

echo "<h3>Exercise C - Comparison Operators</h3>";

$x = 10; $y = "10"; $z = 10.0;

var_dump($x == $y); // A: True
echo "<br>";
var_dump($x === $y); // B: False
echo "<br>";
var_dump($x == $z); // C: True
echo "<br>";
var_dump($x === $z); // D: False
echo "<br>";
var_dump($y === $z); // E: False
echo "<br>";
var_dump($x <=> $y); // F: spaceship — returns 0
echo "<br>";

echo "<br><hr><br>";

// ══════════════════════════════════════════════════════════════
// EXERCISE D — Null & Default Values
// ══════════════════════════════════════════════════════════════
// Run this code as written, then extend it as instructed below.

// Exercise D

echo "<h3>Exercise D - Null Coalescing Operator (??)</h3>";

$username = null;
$display = $username ?? "Guest";
echo "Welcome, $display<br>";

// Chained null coalescing

$config_val = null;
$env_val = null;
$default = "system_default";
$result = $config_val ?? $env_val ?? $default;
echo "Config: $result<br>";

echo "<br>";

// TODO: Add one more chained ?? example of your own and explain it in your report.


// Example 1
$username = "Collins";
$display = $username ?? "Guest";
echo "Welcome, $display<br>";

$config_val = "config_value";
$env_val = "env_value";
$default = "system_default";
$result = $config_val ?? $env_val ?? $default;
echo "Config: $result<br>";
?>







122 changes: 113 additions & 9 deletions starter/lab3_task2.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,20 +7,14 @@
* report BEFORE writing any code below. Marks are awarded for all
* three components: pseudocode, flowchart, and working code.
*
* @author [Your Full Name]
* @student [Your Reg Number, e.g. SCT212-XXXX/2024]
* @author [Collins Njoroge Muchiri]
* @student [ENE212-0059/2023]
* @lab Lab 3 of 14
* @unit ICS 2371
* @date [Date completed]
* @date [Date complete 02/04/2026]
*/

// ── Test Data Set A (change values to run other test sets) ─────────────────
$name = "Your Name";
$cat1 = 8; // out of 10
$cat2 = 7; // out of 10
$cat3 = 9; // out of 10
$cat4 = 6; // out of 10
$exam = 52; // out of 60

// ── Grade Rules (implement using if-elseif-else, ordered highest first) ────
// A (Distinction): Total >= 70
Expand Down Expand Up @@ -64,3 +58,113 @@
// Set C: cat1=0, cat2=0, cat3=7, cat4=0, exam=48 → expect DISQUALIFIED
// Set D: cat1=5, cat2=4, cat3=6, cat4=3, exam=22 → expect grade D + supp eligible
// Set E: cat1=0, cat2=0, cat3=0, cat4=0, exam=15 → expect DISQUALIFIED

// Grade Classification System

echo "<h2>Grade Classification System</h2>";

// DATASET A
echo "<h3>Student A</h3>";
$cat1 = 8; $cat2 = 7; $cat3 = 9; $cat4 = 6;
$exam = 52;
processStudent($cat1, $cat2, $cat3, $cat4, $exam);

// DATASET B
echo "<h3>Student B</h3>";
$cat1 = 9; $cat2 = 8; $cat3 = 0; $cat4 = 9;
$exam = 55;
processStudent($cat1, $cat2, $cat3, $cat4, $exam);

// DATASET C
echo "<h3>Student C</h3>";
$cat1 = 0; $cat2 = 0; $cat3 = 7; $cat4 = 0;
$exam = 48;
processStudent($cat1, $cat2, $cat3, $cat4, $exam);

// DATASET D
echo "<h3>Student D</h3>";
$cat1 = 5; $cat2 = 4; $cat3 = 6; $cat4 = 3;
$exam = 22;
processStudent($cat1, $cat2, $cat3, $cat4, $exam);

// DATASET E
echo "<h3>Student E</h3>";
$cat1 = 0; $cat2 = 0; $cat3 = 0; $cat4 = 0;
$exam = 15;
processStudent($cat1, $cat2, $cat3, $cat4, $exam);

function processStudent($cat1, $cat2, $cat3, $cat4, $exam) {

// Display input values
echo "CAT Scores: CAT 1; $cat1, CAT 2; $cat2, CAT 3; $cat3, CAT 4; $cat4<br>";
echo "Exam Score: $exam<br>";


// STEP 1: CALCULATE TOTAL MARKS
$totalCATs = $cat1 + $cat2 + $cat3 + $cat4;
$total = $totalCATs + $exam;
echo "Total CATs: $totalCATs<br>";
echo "Total Score: $total<br>";

// STEP 2: VALIDATE CAT SCORES (Count attended CATs where score > 0)
$attendedCount = 0;
if ($cat1 > 0) $attendedCount++;
if ($cat2 > 0) $attendedCount++;
if ($cat3 > 0) $attendedCount++;
if ($cat4 > 0) $attendedCount++;
echo "Attended CATs: $attendedCount<br>";

// STEP 3: CHECK ELIGIBILITY (ARE ATTENDED CATS ≥ 3 AND EXAM SCORE ≥ 20?)
if ($attendedCount >= 3 && $exam >= 20) {

// STEP 4: GRADING LADDER (if-elseif-else, highest first)
if ($total >= 70) {
$grade = "A";
$description = "DISTINCTION";
} elseif ($total >= 65) {
$grade = "B+";
$description = "CREDIT UPPER";
} elseif ($total >= 60) {
$grade = "B";
$description = "CREDIT LOWER";
} elseif ($total >= 55) {
$grade = "C+";
$description = "PASS UPPER";
} elseif ($total >= 50) {
$grade = "C";
$description = "PASS LOWER";
} elseif ($total >= 40) {
$grade = "D";
$description = "MARGINAL PASS";
} else {
$grade = "E";
$description = "FAIL";
}

// STEP 5: SUPPLEMENTARY CHECK (Ternary operator)
$supplementary = ($grade == "D") ? "ELIGIBLE FOR SUPPLEMENTARY EXAM" : "NOT ELIGIBLE FOR SUPPLEMENTARY";

// STEP 6: OUTPUT TOTAL, GRADE, AND DESCRIPTION
echo "<strong>Status: ELIGIBLE</strong><br>";
echo "<strong>Grade: $grade - $description</strong><br>";
echo "<strong>Supplementary: $supplementary</strong><br>";

} else {
// DISPLAY DISQUALIFIED
echo "<strong>DISQUALIFIED — EXAM CONDITIONS NOT MET</strong><br>";

// Show reason for disqualification
echo "Reason: ";
if ($attendedCount < 3 && $exam < 20) {
echo "Attended only $attendedCount CAT(s) AND Exam score $exam is below 20";
} elseif ($attendedCount < 3) {
echo "Attended only $attendedCount CAT(s) (need at least 3)";
} elseif ($exam < 20) {
echo "Exam score $exam is below 20";
}
echo "<br>";
}

echo "<hr>";
}
?>
Loading
Loading