From ebe4901b4e81ca6211388e02ca2ae022f75ac3fb Mon Sep 17 00:00:00 2001 From: backyardscientist254 Date: Fri, 3 Apr 2026 11:34:47 +0300 Subject: [PATCH] feat: lab3 complete - ENE212-0071/2023 --- starter/lab3_task1.php | 90 +++++++++++++++++++-------- starter/lab3_task2.php | 99 ++++++++++++++++++++++++------ starter/lab3_task3.php | 134 ++++++++++++++++++++++++++++++++--------- starter/lab3_task4.php | 70 ++++++++++++++++++--- 4 files changed, 313 insertions(+), 80 deletions(-) diff --git a/starter/lab3_task1.php b/starter/lab3_task1.php index f396a7e..3fc10dd 100644 --- a/starter/lab3_task1.php +++ b/starter/lab3_task1.php @@ -3,58 +3,91 @@ * 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 [Joshua Mativo] + * @student [ENE212-0071/2023] * @lab Lab 3 of 14 * @unit ICS 2371 - * @date [Date completed] + * @date [2/4/2026] */ +echo "

Task 1 — Warm-Up Exercises

"; +echo "
"; + // ══════════════════════════════════════════════════════════════ // 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 — Temperature Alert System

"; + +$temperature = 34.5; // change to 36.8 and 34.5 for other test screenshots -// TODO: Exercise A — your code here +echo "Temperature: {$temperature}°C  →  "; +if ($temperature >= 36.1 && $temperature <= 37.5) { + echo "Normal"; +} +if ($temperature > 37.5) { + echo "Fever"; +} +if ($temperature < 36.1) { + echo "Hypothermia Warning"; +} +echo "
"; // ══════════════════════════════════════════════════════════════ // 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 +echo "

Exercise B — Even or Odd

"; + +$number = 47; -// TODO: Exercise B — your code here +if ($number % 2 === 0) { + echo "$number is EVEN
"; +} else { + echo "$number is ODD
"; +} +// Divisibility checks +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
"; +} // ══════════════════════════════════════════════════════════════ // EXERCISE C — Comparison Chain // ══════════════════════════════════════════════════════════════ -// Run this code EXACTLY as written. -// Record all six outputs in your report and explain each result. +echo "

Exercise C — Comparison Chain

"; +echo "
";
 
 $x = 10; $y = "10"; $z = 10.0;
 
-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?
+echo "x = 10 (int),  y = \"10\" (string),  z = 10.0 (float)\n\n";
 
-// Your explanation of each result goes in your PDF report (not here).
+echo "A: var_dump(\$x == \$y)  → "; var_dump($x == $y);   // bool(true)
+echo "B: var_dump(\$x === \$y) → "; var_dump($x === $y);  // bool(false)
+echo "C: var_dump(\$x == \$z)  → "; var_dump($x == $z);   // bool(true)
+echo "D: var_dump(\$x === \$z) → "; var_dump($x === $z);  // bool(false)
+echo "E: var_dump(\$y === \$z) → "; var_dump($y === $z);  // bool(false)
+echo "F: var_dump(\$x <=> \$y) → "; var_dump($x <=> $y); // int(0)
 
+echo "
"; // ══════════════════════════════════════════════════════════════ // EXERCISE D — Null & Default Values // ══════════════════════════════════════════════════════════════ -// Run this code as written, then extend it as instructed below. +echo "

Exercise D — Null Coalescing Operator (??)

"; $username = null; $display = $username ?? "Guest"; @@ -68,3 +101,12 @@ echo "Config: $result
"; // TODO: Add one more chained ?? example of your own and explain it in your report. +// Custom example — simulating a user profile with fallback display name +$db_name = null; // not found in database +$session_name = null; // not set in session +$cookie_name = "Alice"; // found in cookie +$fallback = "Anonymous"; + +$display_name = $db_name ?? $session_name ?? $cookie_name ?? $fallback; +echo "Display name (db → session → cookie → fallback): $display_name
"; +echo "Explanation: \$db_name and \$session_name are null, so PHP falls through to \$cookie_name = 'Alice'.
"; diff --git a/starter/lab3_task2.php b/starter/lab3_task2.php index b03e13c..6ac0548 100644 --- a/starter/lab3_task2.php +++ b/starter/lab3_task2.php @@ -7,20 +7,23 @@ * 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 [Joshua Mativo] + * @student [ENE212-0071/2023] * @lab Lab 3 of 14 * @unit ICS 2371 - * @date [Date completed] + * @date [2/4/2026] */ +echo "

Task 2 — JKUAT Grade Classification System

"; +echo "
"; + // ── 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 +$cat1 = 0; // out of 10 +$cat2 = 0; // out of 10 +$cat3 = 0; // out of 10 +$cat4 = 0; // out of 10 +$exam = 15; // out of 60 // ── Grade Rules (implement using if-elseif-else, ordered highest first) ──── // A (Distinction): Total >= 70 @@ -41,23 +44,85 @@ // Otherwise: "Not eligible for supplementary" // ── STEP 1: Compute total ───────────────────────────────────────────────── -// TODO: compute $total - +$total = $cat1 + $cat2 + $cat3 + $cat4 + $exam; // ── STEP 2: Count CATs attended ─────────────────────────────────────────── -// TODO: compute $cats_attended (each CAT > 0 counts as attended) - +$cats_attended = 0; +if ($cat1 > 0) $cats_attended++; +if ($cat2 > 0) $cats_attended++; +if ($cat3 > 0) $cats_attended++; +if ($cat4 > 0) $cats_attended++; // ── STEP 3: Eligibility check (nested if) ───────────────────────────────── -// TODO: nested if — eligibility → grade classification → supplementary ternary +$eligibility = ""; +$grade = ""; +$remark = ""; +$supplementary = ""; +if ($cats_attended >= 3 && $exam >= 20) { -// ── 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 + // Grade classification — highest first + if ($total >= 70) { + $grade = "A"; + $remark = "Distinction"; + } elseif ($total >= 65) { + $grade = "B+"; + $remark = "Credit Upper"; + } elseif ($total >= 60) { + $grade = "B"; + $remark = "Credit Lower"; + } elseif ($total >= 55) { + $grade = "C+"; + $remark = "Pass Upper"; + } elseif ($total >= 50) { + $grade = "C"; + $remark = "Pass Lower"; + } elseif ($total >= 40) { + $grade = "D"; + $remark = "Marginal Pass"; + } else { + $grade = "E"; + $remark = "Fail"; + } + + $eligibility = "ELIGIBLE"; + // Supplementary ternary + $supplementary = ($grade === "D") + ? "Eligible for Supplementary Exam" + : "Not eligible for supplementary"; +} else { + $eligibility = "DISQUALIFIED — Exam conditions not met"; + $grade = "N/A"; + $remark = "N/A"; + $supplementary = "N/A"; +} + +// ── STEP 4: Display formatted HTML report card ──────────────────────────── +$eligible_color = str_starts_with($eligibility, "DISQ") ? "#c0392b" : "#27ae60"; +?> +
+

JKUAT Grade Report Card

+
+ + + + + + + + + + + + + + +
Student:
CAT 1: / 10
CAT 2: / 10
CAT 3: / 10
CAT 4: / 10
Final Exam: / 60
Total: / 100
CATs Attended: of 4
Status:
Grade:
Remark:
Supplementary:
+
+ +Task 3 — switch-case and match Expression"; +echo "
"; + // ══════════════════════════════════════════════════════════════ // 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 = 6; // change this to test all cases (1=Monday … 7=Sunday) -// TODO: switch-case for day classifier +echo "Day number: $day  →  "; +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 — must be 1 to 7"; +} +echo "
"; // ══════════════════════════════════════════════════════════════ -// EXERCISE B — HTTP Status Code Explainer +// EXERCISE B — HTTP Status Code Explainer (switch-case) // ══════════════════════════════════════════════════════════════ -// 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" +echo "

Exercise B — HTTP Status Code (switch-case)

"; -$status_code = 404; +$status_code = 404; // change to test: 200, 301, 400, 401, 403, 404, 500 -// TODO: switch-case for HTTP status +echo "Status code: $status_code  →  "; +switch ($status_code) { + case 200: + echo "OK — request succeeded"; + break; + case 301: + echo "Moved Permanently — resource relocated"; + break; + case 400: + echo "Bad Request — client error"; + break; + case 401: + echo "Unauthorized — authentication required"; + break; + case 403: + echo "Forbidden — access denied"; + break; + case 404: + echo "Not Found — resource missing"; + break; + case 500: + echo "Internal Server Error — server fault"; + break; + default: + echo "Unknown status code"; +} +echo "
"; // ══════════════════════════════════════════════════════════════ // 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. +echo "

Exercise C — HTTP Status Code (match expression)

"; + +echo "Status code: $status_code  →  "; -// TODO: match expression for HTTP status — same logic as Exercise B +$http_message = match($status_code) { + 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", +}; +echo $http_message . "
"; // ══════════════════════════════════════════════════════════════ // 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? +// Answers in PDF report. Key difference demonstration below: +echo "

Exercise D — switch vs match difference demo

"; + +$val = 0; // integer zero + +echo "Testing \$val = 0 (integer):
"; + +echo "switch result: "; +switch ($val) { + case "active": // == comparison: 0 == "active" is true in older PHP, false in PHP 8 + echo "matched 'active' (loose ==)
"; + break; + case 0: + echo "matched 0 (integer)
"; + break; + default: + echo "no match
"; +} + +echo "match result: "; +$match_result = match($val) { + 0 => "matched integer 0 (strict ===)", + "active"=> "matched string 'active'", + default => "no match", +}; +echo $match_result . "
"; +echo "match uses === so 0 and \"active\" are never confused.
"; diff --git a/starter/lab3_task4.php b/starter/lab3_task4.php index 2a3d225..814be4e 100644 --- a/starter/lab3_task4.php +++ b/starter/lab3_task4.php @@ -6,13 +6,16 @@ * 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 [Joshua Mativo] + * @student [ENE212-0071/2023] * @lab Lab 3 of 14 * @unit ICS 2371 - * @date [Date completed] + * @date [2/4/2026] */ +echo "

Task 4 — HELB Loan Eligibility Checker

"; +echo "
"; + // ── Problem: Student Loan Eligibility System ─────────────────────────────── // // A student applies for a HELB loan. Eligibility rules (nested): @@ -39,18 +42,67 @@ // ── Test data (change to test all branches) ─────────────────────────────── $enrolled = true; -$gpa = 3.1; -$annual_income = 180000; -$previous_loan = false; +$gpa = 2.0; +$annual_income = 50000; +$previous_loan = true; // ── STEP 1: Outer enrollment check ──────────────────────────────────────── -// TODO: nested if structure implementing all rules above +$decision = ""; +$is_eligible = false; +if (!$enrolled) { + $decision = "Not eligible — must be an active student"; -// ── STEP 2: Display result ──────────────────────────────────────────────── -// TODO: output formatted result showing all input values and the decision +} else { + // INNER CHECK 1 — GPA + if ($gpa < 2.0) { + $decision = "Not eligible — GPA below minimum (2.0)"; + + } else { + // INNER CHECK 2 — Household income bracket + if ($annual_income < 100000) { + $decision = "Eligible — Full loan award"; + $is_eligible = true; + } elseif ($annual_income < 250000) { + $decision = "Eligible — Partial loan (75%)"; + $is_eligible = true; + } elseif ($annual_income < 500000) { + $decision = "Eligible — Partial loan (50%)"; + $is_eligible = true; + } else { + $decision = "Not eligible — household income exceeds threshold"; + } + // Ternary — renewal vs new application (only if eligible) + if ($is_eligible) { + $application_type = $previous_loan ? "Renewal application" : "New application"; + $decision .= " | " . $application_type; + } + } +} +// ── STEP 2: Display result ──────────────────────────────────────────────── +$status_color = $is_eligible ? "#27ae60" : "#c0392b"; +?> +
+

HELB Loan Eligibility Report

+
+ + + + + + + + + + + + +
Enrolled:
GPA: / 4.00
Annual Household Income:KES
Previous HELB Loan:

Decision:
+
+ +