From d39ce97c165da9725b4db581321c902b5112e270 Mon Sep 17 00:00:00 2001 From: Sundra731 Date: Fri, 3 Apr 2026 18:16:10 +0300 Subject: [PATCH] feat: lab3 complete - ENE212-0148/2023 --- starter/lab3_task1.php | 62 +++++++++- starter/lab3_task2.php | 254 ++++++++++++++++++++++++++++++++++++++--- starter/lab3_task3.php | 80 +++++++++++-- starter/lab3_task4.php | 63 ++++++++-- 4 files changed, 421 insertions(+), 38 deletions(-) diff --git a/starter/lab3_task1.php b/starter/lab3_task1.php index f396a7e..2932176 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 [SUNDRA EVANS] + * @student [ENE212-0148/2023] * @lab Lab 3 of 14 * @unit ICS 2371 - * @date [Date completed] + * @date [03/04/2026] */ // ══════════════════════════════════════════════════════════════ @@ -20,8 +20,19 @@ // "Hypothermia Warning" if temp < 36.1 // Test with: 36.8, 39.2, 34.5 — screenshot each -// TODO: Exercise A — your code here +$temperature = 36.8; +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 @@ -30,8 +41,37 @@ // 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 -// TODO: Exercise B — your code here +$number = 47; +// Even or Odd +if ($number % 2 == 0) { + echo "$number is EVEN
"; +} else { + echo "$number is ODD
"; +} + +// Divisible by 3 +if ($number % 3 == 0) { + echo "$number is divisible by 3
"; +} else { + echo "$number is NOT divisible by 3
"; +} + +// Divisible by 5 +if ($number % 5 == 0) { + echo "$number is divisible by 5
"; +} else { + echo "$number is NOT divisible by 5
"; +} + +// Divisible by both 3 and 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 @@ -48,6 +88,9 @@ var_dump($y === $z); // E: ? var_dump($x <=> $y); // F: spaceship — what type? what value? +echo "
"; +echo "
"; + // Your explanation of each result goes in your PDF report (not here). @@ -60,6 +103,8 @@ $display = $username ?? "Guest"; echo "Welcome, $display
"; +echo "
"; + // Chained null coalescing $config_val = null; $env_val = null; @@ -67,4 +112,11 @@ $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. +$input_name = null; +$cookie_name = null; +$session_name = "Mwenda"; +$final_name = $input_name ?? $cookie_name ?? $session_name ?? "Anonymous"; +echo "User: $final_name
"; diff --git a/starter/lab3_task2.php b/starter/lab3_task2.php index b03e13c..c72c912 100644 --- a/starter/lab3_task2.php +++ b/starter/lab3_task2.php @@ -7,20 +7,20 @@ * 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 [SUNDRA EVANS] + * @student [ENE212-0148/2023] * @lab Lab 3 of 14 * @unit ICS 2371 - * @date [Date completed] + * @date [03/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 +$name = "Sundra Evans"; +$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,15 +41,60 @@ // 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 +$eligible = 'Disqualified'; +$grade = 'N/A'; +$description = 'DISQUALIFIED'; +$remark = 'Exam conditions not met'; +$supplementary = 'Not eligible for supplementary'; + +if ($cats_attended >= 3 && $exam >= 20) { + $eligible = 'Eligible'; + // Grade using 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'; + } + + $supplementary = ($grade === 'D') + ? 'Eligible for Supplementary Exam' + : 'Not eligible for supplementary'; + + $status = "Grade {$grade} — {$description}"; + $remark = $description; + + + } else { + + $eligible = 'Disqualified'; + $grade = 'N/A'; + $description = 'DISQUALIFIED'; + $remark = 'Exam conditions not met'; + $supplementary = 'Not eligible for supplementary'; + $status = 'DISQUALIFIED — Exam conditions not met'; +} // ── STEP 4: Display formatted HTML report card ──────────────────────────── @@ -57,10 +102,183 @@ // student name, each CAT score, exam score, total, // cats attended, eligibility status, grade, remark, supplementary status +?> + + + + + Student Report Card + + + +
+ +
+

JKUAT — Student Report Card

+

Academic Performance Summary

+
+ +
+ +
+ + + + + + + + + + + + + + + + + + + + + + + + + +
CAT 1 / 10
CAT 2 / 10
CAT 3 / 10
CAT 4 / 10
Final Exam / 60
Total Score / 100
CATs Attended / 4
Eligibility + $eligible"; + ?> +
Supplementary + $supplementary"; + ?> +
+ + +
+
+
+
+ +
+
+ + + -// ── 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 diff --git a/starter/lab3_task3.php b/starter/lab3_task3.php index d9e00c8..b0dbd04 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 [SUNDRA EVANS] + * @student [ENE212-0148/2023] * @lab Lab 3 of 14 * @unit ICS 2371 - * @date [Date completed] + * @date [03/04/2026] */ // ══════════════════════════════════════════════════════════════ @@ -21,8 +21,33 @@ $day = 3; // change this to test all cases -// 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"; + break; +} +echo "
"; +echo "
"; // ══════════════════════════════════════════════════════════════ // EXERCISE B — HTTP Status Code Explainer @@ -37,10 +62,37 @@ // 500 → "Internal Server Error — server fault" // default → "Unknown status code" -$status_code = 404; +$status_code = 200; // change this to test different status codes -// TODO: switch-case for HTTP status +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"; + break; +} +echo "
"; +echo "
"; // ══════════════════════════════════════════════════════════════ // EXERCISE C — PHP 8 match Expression @@ -49,7 +101,21 @@ // 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 + +$status_code = 500; + +$explanation = 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 not recognized.", +}; + +echo $explanation; // ══════════════════════════════════════════════════════════════ diff --git a/starter/lab3_task4.php b/starter/lab3_task4.php index 2a3d225..ad751fe 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 [SUNDRA EVANS] + * @student [ENE212-0148/2023] * @lab Lab 3 of 14 * @unit ICS 2371 - * @date [Date completed] + * @date [03/04/2026] */ // ── Problem: Student Loan Eligibility System ─────────────────────────────── @@ -39,16 +39,63 @@ // ── 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 +$result = ""; // store final decision + +// Outer check +if ($enrolled == false) { + $result = "Not eligible: must be an active student"; + +} else { + + // GPA check + if ($gpa < 2.0) { + $result = "Not eligible: GPA below minimum"; + + } else { + + // Income check + if ($annual_income < 100000) { + $loan = "Full loan"; + + } elseif ($annual_income < 250000) { + $loan = "Partial 75%"; + + } elseif ($annual_income < 500000) { + $loan = "Partial 50%"; + + } else { + $result = "Not eligible: household income exceeds threshold"; + } + + // Only assign status if eligible + if ($result == "") { + $status = ($previous_loan) + ? "Renewal application" + : "New application"; + + $result = "$loan | $status"; + } + } +} + + // ── STEP 2: Display result ──────────────────────────────────────────────── -// TODO: output formatted result showing all input values and the decision +echo "
"; +echo "HELB Loan Eligibility Result

"; + +echo "Enrolled: " . ($enrolled ? "Yes" : "No") . "
"; +echo "GPA: $gpa
"; +echo "Annual Income: KES $annual_income
"; +echo "Previous Loan: " . ($previous_loan ? "Yes" : "No") . "

"; + +echo "Decision: $result
"; // ── Required Test Data Sets — screenshot each ─────────────────────────────