diff --git a/starter/lab3_task1.php b/starter/lab3_task1.php index f396a7e..2eb3630 100644 --- a/starter/lab3_task1.php +++ b/starter/lab3_task1.php @@ -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] */ // ══════════════════════════════════════════════════════════════ @@ -23,6 +23,41 @@ // TODO: Exercise A — your code here +// Exercise A: Temperature Check + +echo "

Exercise A - Temperature Alert System

"; + +// 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}
"; + +echo "
"; + +// 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}
"; + +echo "
"; + +// 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}
"; + +echo "


"; + + // ══════════════════════════════════════════════════════════════ // EXERCISE B — Even or Odd // ══════════════════════════════════════════════════════════════ @@ -32,6 +67,39 @@ // TODO: Exercise B — your code here +// Exercise B + +echo "

Exercise B - Even or Odd with Divisibility

"; + +$number = 47; + +// Check if number is even or odd +if ($number % 2 == 0) { + echo "$number is EVEN
"; +} else { + echo "$number is ODD
"; +} + +// Check divisibility +if ($number % 3 == 0) { + echo "$number is divisible by 3
"; +} else { + echo "$number is NOT divisible by 3
"; +} + +if ($number % 5 == 0) { + echo "$number is divisible by 5
"; +} else { + echo "$number is NOT divisible by 5
"; +} + +if ($number % 3 == 0 && $number % 5 == 0) { + echo "$number is divisible by both 3 and 5
"; +} else { + echo "$number is NOT divisible by both 3 and 5
"; +} + +echo "


"; // ══════════════════════════════════════════════════════════════ // EXERCISE C — Comparison Chain @@ -50,21 +118,68 @@ // Your explanation of each result goes in your PDF report (not here). +// Exercise C + +echo "

Exercise C - Comparison Operators

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


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

Exercise D - Null Coalescing Operator (??)

"; + $username = null; $display = $username ?? "Guest"; echo "Welcome, $display
"; // Chained null coalescing + $config_val = null; $env_val = null; $default = "system_default"; $result = $config_val ?? $env_val ?? $default; echo "Config: $result
"; +echo "
"; + // 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
"; + +$config_val = "config_value"; +$env_val = "env_value"; +$default = "system_default"; +$result = $config_val ?? $env_val ?? $default; +echo "Config: $result
"; +?> + + + + + + + diff --git a/starter/lab3_task2.php b/starter/lab3_task2.php index b03e13c..59ff5ce 100644 --- a/starter/lab3_task2.php +++ b/starter/lab3_task2.php @@ -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 @@ -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 "

Grade Classification System

"; + +// DATASET A +echo "

Student A

"; +$cat1 = 8; $cat2 = 7; $cat3 = 9; $cat4 = 6; +$exam = 52; +processStudent($cat1, $cat2, $cat3, $cat4, $exam); + +// DATASET B +echo "

Student B

"; +$cat1 = 9; $cat2 = 8; $cat3 = 0; $cat4 = 9; +$exam = 55; +processStudent($cat1, $cat2, $cat3, $cat4, $exam); + +// DATASET C +echo "

Student C

"; +$cat1 = 0; $cat2 = 0; $cat3 = 7; $cat4 = 0; +$exam = 48; +processStudent($cat1, $cat2, $cat3, $cat4, $exam); + +// DATASET D +echo "

Student D

"; +$cat1 = 5; $cat2 = 4; $cat3 = 6; $cat4 = 3; +$exam = 22; +processStudent($cat1, $cat2, $cat3, $cat4, $exam); + +// DATASET E +echo "

Student E

"; +$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
"; + echo "Exam Score: $exam
"; + + + // STEP 1: CALCULATE TOTAL MARKS + $totalCATs = $cat1 + $cat2 + $cat3 + $cat4; + $total = $totalCATs + $exam; + echo "Total CATs: $totalCATs
"; + echo "Total Score: $total
"; + + // 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
"; + + // 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 "Status: ELIGIBLE
"; + echo "Grade: $grade - $description
"; + echo "Supplementary: $supplementary
"; + + } else { + // DISPLAY DISQUALIFIED + echo "DISQUALIFIED — EXAM CONDITIONS NOT MET
"; + + // 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 "
"; + } + + echo "
"; +} +?> \ No newline at end of file diff --git a/starter/lab3_task3.php b/starter/lab3_task3.php index d9e00c8..d9ae232 100644 --- a/starter/lab3_task3.php +++ b/starter/lab3_task3.php @@ -3,11 +3,11 @@ * ICS 2371 — Lab 3: Control Structures I * Task 3: switch-case and match Expression [6 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] */ // ══════════════════════════════════════════════════════════════ @@ -59,3 +59,89 @@ // 1. What is the key difference between switch (==) and match (===)? // 2. Give one example where this difference changes the output. // 3. When would you prefer switch over match, and why? + + +// Exercise A + +echo "

Exercise A— Day of Week Classifier

"; + +$day = 3; // change this value to test + +switch ($day) { + case 1: + echo "Monday — Lecture day"; + break; + case 2: + echo "Tuesday — Lecture day"; + break; + case 3: + echo "Wednesday — Lecture day"; + break; + case 4: + echo "Thursday — Lecture day"; + break; + case 5: + echo "Friday — Lecture day"; + break; + case 6: + case 7: + echo "Weekend"; + break; + default: + echo "Invalid day"; +} + +// Exercise B + +echo "

Exercise B— HTTP Status Code Explainer (switch)

"; + +$status_code = 404; // change to test + +switch ($status_code) { + case 200: + echo "OK"; + break; + case 301: + echo "Moved Permanently"; + break; + case 400: + echo "Bad Request"; + break; + case 401: + echo "Unauthorized"; + break; + case 403: + echo "Forbidden"; + break; + case 404: + echo "Not Found"; + break; + case 500: + echo "Internal Server Error"; + break; + default: + echo "Unknown Status Code"; +} + +//Exercise C + +echo "

Exercise C— Rewrite using match (PHP 8)

"; + + +$status_code = 404; + +$result = match ($status_code) { + 200 => "OK", + 301 => "Moved Permanently", + 400 => "Bad Request", + 401 => "Unauthorized", + 403 => "Forbidden", + 404 => "Not Found", + 500 => "Internal Server Error", + default => "Unknown Status Code" +}; + +echo $result; + + +?> \ No newline at end of file diff --git a/starter/lab3_task4.php b/starter/lab3_task4.php index 2a3d225..090a201 100644 --- a/starter/lab3_task4.php +++ b/starter/lab3_task4.php @@ -6,11 +6,11 @@ * IMPORTANT: You must complete pseudocode AND flowchart in your PDF * report BEFORE writing any code below. * - * @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] */ // ── Problem: Student Loan Eligibility System ─────────────────────────────── @@ -57,3 +57,43 @@ // Set C: enrolled=false, gpa=3.5, income=60000, previous=true → Not enrolled // Set D: enrolled=true, gpa=2.5, income=600000, previous=true → Income fail // Set E: enrolled=true, gpa=2.0, income=50000, previous=true → Full | Renewal + + +function checkEligibility($enrolled, $gpa, $annual_income, $previous_loan) { + + if ($enrolled == false) { + return "Not enrolled"; + } + + if ($gpa < 2.0) { + return "GPA fail"; + } + + if ($annual_income >= 500000) { + return "Income fail"; + } + + // Loan category + if ($annual_income < 100000) { + $loan = "Full"; + } elseif ($annual_income < 250000) { + $loan = "Partial 75%"; + } else { + $loan = "Partial 50%"; + } + + // Ternary for application type + $type = $previous_loan ? "Renewal" : "New"; + + return "$loan | $type"; +} + +// TEST DATA SETS + +echo "Set A: " . checkEligibility(true, 3.1, 180000, false) . "
"; +echo "Set B: " . checkEligibility(true, 1.8, 80000, false) . "
"; +echo "Set C: " . checkEligibility(false, 3.5, 60000, true) . "
"; +echo "Set D: " . checkEligibility(true, 2.5, 600000, true) . "
"; +echo "Set E: " . checkEligibility(true, 2.0, 50000, true) . "
"; + +?> \ No newline at end of file