From 614e5c53d6df92b93666af627a18b30185a18e91 Mon Sep 17 00:00:00 2001 From: Weezy58 Date: Fri, 3 Apr 2026 15:50:07 +0300 Subject: [PATCH 1/4] feat : lab3 complete - ENE212-0085/2023 --- starter/lab3_task1.php | 114 ++++++++++++++++++++++++++--------------- 1 file changed, 74 insertions(+), 40 deletions(-) diff --git a/starter/lab3_task1.php b/starter/lab3_task1.php index f396a7e..11a5a2d 100644 --- a/starter/lab3_task1.php +++ b/starter/lab3_task1.php @@ -3,68 +3,102 @@ * 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 [wayne naum ] + * @student [ENE 212-0085/2023] * @lab Lab 3 of 14 * @unit ICS 2371 * @date [Date completed] */ -// ══════════════════════════════════════════════════════════════ +// ========================================== // EXERCISE A — Temperature Alert System -// ══════════════════════════════════════════════════════════════ -// Declare $temperature = 39.2 -// Use separate if statements (not if-else) to print: -// "Normal" if temp is between 36.1 and 37.5 inclusive -// "Fever" if temp > 37.5 -// "Hypothermia Warning" if temp < 36.1 -// Test with: 36.8, 39.2, 34.5 — screenshot each +// ========================================== +echo "

Exercise A

"; -// TODO: Exercise A — your code here +// 1. Declare the variable (Starting with the first test value) +$temperature = 34.5; +// 2. Check if Normal (between 36.1 and 37.5 inclusive) +if ($temperature >= 36.1 && $temperature <= 37.5) { + echo "Normal
"; +} -// ══════════════════════════════════════════════════════════════ -// EXERCISE B — Even or Odd -// ══════════════════════════════════════════════════════════════ -// Declare $number = 47 -// Use if-else to print "$number is EVEN" or "$number is ODD" -// Also check divisibility by 3, by 5, and by both 3 and 5 — one line each +// 3. Check if Fever (greater than 37.5) +if ($temperature > 37.5) { + echo "Fever
"; +} -// TODO: Exercise B — your code here +// 4. Check for Hypothermia (less than 36.1) +if ($temperature < 36.1) { + echo "Hypothermia Warning
"; +} +echo "
"; +// ========================================== +// EXERCISE B — Even or Odd +// ========================================== +echo "

Exercise B

"; -// ══════════════════════════════════════════════════════════════ -// EXERCISE C — Comparison Chain -// ══════════════════════════════════════════════════════════════ -// Run this code EXACTLY as written. -// Record all six outputs in your report and explain each result. +// 1. Declare the variable +$number = 47; -$x = 10; $y = "10"; $z = 10.0; +// 2. Check if Even or Odd using an if-else statement +if ($number % 2 == 0) { + echo "$number is EVEN
"; +} else { + echo "$number is ODD
"; +} -var_dump($x == $y); // A: ? -var_dump($x === $y); // B: ? -var_dump($x == $z); // C: ? -var_dump($x === $z); // D: ? -var_dump($y === $z); // E: ? -var_dump($x <=> $y); // F: spaceship — what type? what value? +// 3. Divisibility checks (using separate if statements) +if ($number % 3 == 0) { + echo "$number is divisible by 3
"; +} +if ($number % 5 == 0) { + echo "$number is divisible by 5
"; +} +if ($number % 3 == 0 && $number % 5 == 0) { + echo "$number is divisible by both 3 and 5
"; +} -// Your explanation of each result goes in your PDF report (not here). +echo "
"; +// ========================================== +// EXERCISE C — Comparison Chain +// ========================================== +echo "

Exercise C

"; + +$x = 10; +$y = "10"; +$z = 10.0; +var_dump($x == $y); echo "
"; // A +var_dump($x === $y); echo "
"; // B +var_dump($x == $z); echo "
"; // C +var_dump($x === $z); echo "
"; // D +var_dump($y === $z); echo "
"; // E +var_dump($x <=> $y); echo "
"; // F -// ══════════════════════════════════════════════════════════════ +echo "
"; +// ========================================== // EXERCISE D — Null & Default Values -// ══════════════════════════════════════════════════════════════ -// Run this code as written, then extend it as instructed below. +// ========================================== +echo "

Exercise D

"; $username = null; -$display = $username ?? "Guest"; +$display = $username ?? "Guest"; echo "Welcome, $display
"; -// Chained null coalescing $config_val = null; -$env_val = null; -$default = "system_default"; -$result = $config_val ?? $env_val ?? $default; +$env_val = null; +$default = "system_default"; +$result = $config_val ?? $env_val ?? $default; echo "Config: $result
"; -// TODO: Add one more chained ?? example of your own and explain it in your report. +// Custom Chained Example +echo "
Custom Example:
"; +$user_theme_preference = null; +$system_dark_mode = null; +$fallback_theme = "light_mode"; + +// It checks preference first, then system, then falls back to default +$active_theme = $user_theme_preference ?? $system_dark_mode ?? $fallback_theme; +echo "Active Theme: $active_theme
"; From a5f555b348424d59e5e7fa6529818ee9035f4e49 Mon Sep 17 00:00:00 2001 From: Weezy58 Date: Fri, 3 Apr 2026 15:52:40 +0300 Subject: [PATCH 2/4] feat : lab3 complete - ENE212-0085/2023 --- starter/lab3_task2.php | 102 +++++++++++++++++++++++------------------ 1 file changed, 58 insertions(+), 44 deletions(-) diff --git a/starter/lab3_task2.php b/starter/lab3_task2.php index b03e13c..ff68b05 100644 --- a/starter/lab3_task2.php +++ b/starter/lab3_task2.php @@ -7,60 +7,74 @@ * 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 [wayne naum] + * @student [ENE212-0060/2023] * @lab Lab 3 of 14 * @unit ICS 2371 * @date [Date completed] */ -// ── 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 +echo "

Task 2: Grade Classification System

"; -// ── Grade Rules (implement using if-elseif-else, ordered highest first) ──── -// A (Distinction): Total >= 70 -// B+ (Credit Upper): Total >= 65 -// B (Credit Lower): Total >= 60 -// C+ (Pass Upper): Total >= 55 -// C (Pass Lower): Total >= 50 -// D (Marginal Pass): Total >= 40 -// E (Fail): Total < 40 +// ========================================== +// 1. INPUTS (Currently set to Test Set A) +// ========================================== +$cat1 = 0; +$cat2 = 0; +$cat3 = 0; +$cat4 = 0; +$exam = 15; -// ── Eligibility Rule (implement using nested if) ─────────────────────────── -// Must have attended at least 3 of 4 CATs (CAT score > 0 counts as attended) -// AND exam score >= 20 -// Otherwise: "DISQUALIFIED — Exam conditions not met" +// ========================================== +// 2. SEQUENCE: Calculations +// ========================================== +$total = $cat1 + $cat2 + $cat3 + $cat4 + $exam; +$cats_attended = 0; -// ── Supplementary Rule (implement using ternary) ────────────────────────── -// If grade is D: "Eligible for Supplementary Exam" -// Otherwise: "Not eligible for supplementary" +if ($cat1 > 0) { $cats_attended++; } +if ($cat2 > 0) { $cats_attended++; } +if ($cat3 > 0) { $cats_attended++; } +if ($cat4 > 0) { $cats_attended++; } -// ── STEP 1: Compute total ───────────────────────────────────────────────── -// TODO: compute $total +echo "Scores: CATs Attended: $cats_attended/4 | Exam: $exam | Total Score: $total/100

"; +// ========================================== +// 3. SELECTION: Eligibility & Grading +// ========================================== +if ($cats_attended >= 3 && $exam >= 20) { + + // --- Grade Classification (if-elseif-else) --- + if ($total >= 70) { + $grade = "A"; + $desc = "Distinction"; + } elseif ($total >= 65) { + $grade = "B+"; + $desc = "Credit Upper"; + } elseif ($total >= 60) { + $grade = "B"; + $desc = "Credit Lower"; + } elseif ($total >= 55) { + $grade = "C+"; + $desc = "Pass Upper"; + } elseif ($total >= 50) { + $grade = "C"; + $desc = "Pass Lower"; + } elseif ($total >= 40) { + $grade = "D"; + $desc = "Marginal Pass"; + } else { + $grade = "E"; + $desc = "Fail"; + } -// ── STEP 2: Count CATs attended ─────────────────────────────────────────── -// TODO: compute $cats_attended (each CAT > 0 counts as attended) + echo "Status: Grade $grade ($desc)
"; + // --- Supplementary Check (Ternary Operator) --- + $supp_status = ($grade === "D") ? "Eligible for Supplementary Exam" : "Not eligible for supplementary"; + echo "Supplementary: $supp_status
"; -// ── STEP 3: Eligibility check (nested if) ───────────────────────────────── -// TODO: nested if — eligibility → grade classification → supplementary ternary - - -// ── STEP 4: Display formatted HTML report card ──────────────────────────── -// TODO: output a clear, formatted report card showing: -// student name, each CAT score, exam score, total, -// cats attended, eligibility status, grade, remark, supplementary status - - -// ── Required Test Data Sets — screenshot each ───────────────────────────── -// Set A: cat1=8, cat2=7, cat3=9, cat4=6, exam=52 → expect grade B -// Set B: cat1=9, cat2=8, cat3=0, cat4=9, exam=55 → expect grade A (check cats_attended) -// 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 +} else { + // --- Disqualified Branch --- + echo "Status: DISQUALIFIED — Exam conditions not met
"; +} +?> From 167b47637c1edfde40600b89aa40dd324d9c679d Mon Sep 17 00:00:00 2001 From: Weezy58 Date: Fri, 3 Apr 2026 15:54:33 +0300 Subject: [PATCH 3/4] feat : lab3 complete - ENE212-0085/2023 --- starter/lab3_task3.php | 110 ++++++++++++++++++++++++----------------- 1 file changed, 65 insertions(+), 45 deletions(-) diff --git a/starter/lab3_task3.php b/starter/lab3_task3.php index d9e00c8..49e2d5d 100644 --- a/starter/lab3_task3.php +++ b/starter/lab3_task3.php @@ -3,59 +3,79 @@ * 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 [wayne naum ] + * @student [ENE 212-0060/2023] * @lab Lab 3 of 14 * @unit ICS 2371 * @date [Date completed] */ -// ══════════════════════════════════════════════════════════════ +// ========================================== // EXERCISE A — Day of Week Classifier -// ══════════════════════════════════════════════════════════════ -// Given $day (integer 1–7, where 1=Monday): -// Use switch-case to print the day name. -// Group Saturday and Sunday under "Weekend". -// All weekdays print their name and "— Lecture day". -// Remember: break is NOT optional. Missing break = fall-through. +// ========================================== +echo "

Exercise A — Day of Week Classifier

"; -$day = 3; // change this to test all cases +$day = 4; -// TODO: switch-case for day classifier +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 number. Please enter 1-7.
"; + break; +} +echo "
"; -// ══════════════════════════════════════════════════════════════ +// ========================================== // EXERCISE B — HTTP Status Code Explainer -// ══════════════════════════════════════════════════════════════ -// Given $status_code, use switch-case to explain it: -// 200 → "OK — request succeeded" -// 301 → "Moved Permanently — resource relocated" -// 400 → "Bad Request — client error" -// 401 → "Unauthorized — authentication required" -// 403 → "Forbidden — access denied" -// 404 → "Not Found — resource missing" -// 500 → "Internal Server Error — server fault" -// default → "Unknown status code" - -$status_code = 404; - -// TODO: switch-case for HTTP status - - -// ══════════════════════════════════════════════════════════════ -// EXERCISE C — PHP 8 match Expression -// ══════════════════════════════════════════════════════════════ -// Rewrite Exercise B using PHP 8 match instead of switch-case. -// Note: match uses STRICT comparison (===). No break needed. -// Observe the difference in syntax and behaviour. - -// TODO: match expression for HTTP status — same logic as Exercise B - - -// ══════════════════════════════════════════════════════════════ -// EXERCISE D — Rewrite comparison -// ══════════════════════════════════════════════════════════════ -// In your PDF report, answer: -// 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? +// ========================================== +echo "

Exercise B

"; + +$status_code = 404; + +switch ($status_code) { + case 200: echo "200: OK — The request succeeded.
"; break; + case 301: echo "301: Moved Permanently — The URL has been changed.
"; break; + case 400: echo "400: Bad Request — The server could not understand the request.
"; break; + case 401: echo "401: Unauthorized — Authentication is required.
"; break; + case 403: echo "403: Forbidden — You do not have permission to access this.
"; break; + case 404: echo "404: Not Found — The requested resource could not be found.
"; break; + case 500: echo "500: Internal Server Error — The server encountered a problem.
"; break; + default: echo "$status_code: Unknown Status Code.
"; break; +} + +echo "
"; + +// ========================================== +// EXERCISE C — PHP 8 match Rewrite +// ========================================== +echo "

Exercise C

"; + +$status_code_match = 404; + +// The match expression assigns the result directly to the variable +$explanation = match ($status_code_match) { + 200 => "200: OK — The request succeeded.
", + 301 => "301: Moved Permanently — The URL has been changed.
", + 400 => "400: Bad Request — The server could not understand the request.
", + 401 => "401: Unauthorized — Authentication is required.
", + 403 => "403: Forbidden — You do not have permission to access this.
", + 404 => "404: Not Found — The requested resource could not be found.
", + 500 => "500: Internal Server Error — The server encountered a problem.
", + default => "$status_code_match: Unknown Status Code.
", +}; + +echo $explanation; + +echo "
"; + +// The single closing tag for the whole file! +?> From 3cf02886f0d02a1be5be6fc090aa21e45c9744d0 Mon Sep 17 00:00:00 2001 From: Weezy58 Date: Fri, 3 Apr 2026 15:56:23 +0300 Subject: [PATCH 4/4] feat : lab3 complete - ENE212-0085/2023 --- starter/lab3_task4.php | 105 +++++++++++++++++++++++------------------ 1 file changed, 59 insertions(+), 46 deletions(-) diff --git a/starter/lab3_task4.php b/starter/lab3_task4.php index 2a3d225..eec83b2 100644 --- a/starter/lab3_task4.php +++ b/starter/lab3_task4.php @@ -6,54 +6,67 @@ * 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 [WAYNE NAUM] + * @student [ENE212-0085/2023] * @lab Lab 3 of 14 * @unit ICS 2371 * @date [Date completed] */ +echo "

Task 4: HELB Loan Eligibility Checker

"; -// ── Problem: Student Loan Eligibility System ─────────────────────────────── -// -// A student applies for a HELB loan. Eligibility rules (nested): -// -// OUTER CHECK — Is the student enrolled? -// $enrolled = true/false -// If NOT enrolled → "Not eligible — must be an active student" -// -// INNER CHECK 1 — GPA requirement (if enrolled) -// $gpa (float, 0.0–4.0) -// GPA >= 2.0 → proceed to inner check 2 -// GPA < 2.0 → "Not eligible — GPA below minimum (2.0)" -// -// INNER CHECK 2 — Household income bracket (if enrolled AND GPA >= 2.0) -// $annual_income (KES) -// < 100000 → "Eligible — Full loan award" -// < 250000 → "Eligible — Partial loan (75%)" -// < 500000 → "Eligible — Partial loan (50%)" -// >= 500000 → "Not eligible — household income exceeds threshold" -// -// ADDITIONAL RULE — Ternary for renewal vs new application: -// $previous_loan = true/false -// If eligible: use ternary to append "| Renewal application" or "| New application" - -// ── Test data (change to test all branches) ─────────────────────────────── -$enrolled = true; -$gpa = 3.1; -$annual_income = 180000; -$previous_loan = false; - -// ── STEP 1: Outer enrollment check ──────────────────────────────────────── -// TODO: nested if structure implementing all rules above - - -// ── STEP 2: Display result ──────────────────────────────────────────────── -// TODO: output formatted result showing all input values and the decision - - -// ── Required Test Data Sets — screenshot each ───────────────────────────── -// Set A: enrolled=true, gpa=3.1, income=180000, previous=false → Partial 75% -// Set B: enrolled=true, gpa=1.8, income=80000, previous=false → GPA fail -// 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 +// ========================================== +// 1. INPUTS (Currently set to Test Set A) +// ========================================== +$enrolled = true; +$gpa = 2.0; +$annual_income = 50000; +$previous_loan = true; + +// Format the display for our test output +$enrolled_text = $enrolled ? "Yes" : "No"; +$prev_loan_text = $previous_loan ? "Yes" : "No"; +echo "Data: Enrolled: $enrolled_text | GPA: $gpa | Income: KES " . number_format($annual_income) . " | Prev Loan: $prev_loan_text

"; + +// ========================================== +// 2. NESTED LOGIC +// ========================================== + +// OUTER CHECK: Is the student enrolled? +if ($enrolled === false) { + echo "Result: Not eligible — must be an active student
"; +} else { + + // INNER CHECK 1: GPA Minimum + if ($gpa < 2.0) { + echo "Result: Not eligible — GPA below minimum
"; + } else { + + // INNER CHECK 2: Household Income + $loan_amount = ""; + $is_eligible = true; + + if ($annual_income < 100000) { + $loan_amount = "Full loan"; + } elseif ($annual_income < 250000) { + $loan_amount = "Partial 75%"; + } elseif ($annual_income < 500000) { + $loan_amount = "Partial 50%"; + } else { + $loan_amount = "Not eligible — Income too high"; + $is_eligible = false; // Flag to skip the ternary step + } + + // TERNARY: Renewal vs New Application + if ($is_eligible) { + // Only append the application type if they are actually getting a loan + $app_type = ($previous_loan === true) ? "Renewal application" : "New application"; + echo "Result: $loan_amount | $app_type
"; + } else { + // If income was too high, just print the denial reason + echo "Result: $loan_amount
"; + } + } +} + +echo "
"; +?>