diff --git a/starter/lab3_task1.php b/starter/lab3_task1.php index f396a7e..2a2ca38 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 Robin Wanyonyi + * @student ENE212-0208/2023 * @lab Lab 3 of 14 * @unit ICS 2371 - * @date [Date completed] + * @date 03/04/2026 */ // ══════════════════════════════════════════════════════════════ @@ -31,7 +31,40 @@ // Also check divisibility by 3, by 5, and by both 3 and 5 — one line each // TODO: Exercise B — your code here +"; + +if ($temperature >= 36.1 && $temperature <= 37.5) { + echo "Temperature: Normal" . "
";} +if ($temperature > 37.5) { + echo "Temperature: Fever" . "
";} +if ($temperature < 36.1) { + echo "Temperature: Hypothermia Warning" . "
";} + +echo "
"; + +$temperature = 36.8; +echo $temperature . "
"; +if ($temperature >= 36.1 && $temperature <= 37.5) { + echo "Temperature: Normal" . "
";} +if ($temperature > 37.5) { + echo "Temperature: Fever" . "
";} +if ($temperature < 36.1) { + echo "Temperature: Hypothermia Warning" . "
";} + +echo "
"; + +$temperature = 34.5; +echo $temperature . "
"; + +if ($temperature >= 36.1 && $temperature <= 37.5) { + echo "Temperature: Normal" . "
";} +if ($temperature > 37.5) { + echo "Temperature: Fever" . "
";} +if ($temperature < 36.1) { + echo "Temperature: Hypothermia Warning" . "
";} // ══════════════════════════════════════════════════════════════ // EXERCISE C — Comparison Chain @@ -57,8 +90,9 @@ // Run this code as written, then extend it as instructed below. $username = null; +$intro = "Welcome to the site!"; $display = $username ?? "Guest"; -echo "Welcome, $display
"; +echo "Welcome, $display
" . $intro . "
"; // Chained null coalescing $config_val = null; @@ -67,4 +101,11 @@ $result = $config_val ?? $env_val ?? $default; echo "Config: $result
"; +// Another chained example +$user_pref = null; +$site_pref = null; +$global_pref = "dark_mode"; +$theme = $user_pref ?? $site_pref ?? $global_pref; +echo "Theme: $theme
"; + // TODO: Add one more chained ?? example of your own and explain it in your report. diff --git a/starter/lab3_task2.php b/starter/lab3_task2.php index b03e13c..7e0af89 100644 --- a/starter/lab3_task2.php +++ b/starter/lab3_task2.php @@ -7,20 +7,19 @@ * 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 Robin Wanyonyi + * @student ENE212-0208/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 = "SET E" . "
"; // student name +echo $name . "
"; + +echo "
"; + // ── Grade Rules (implement using if-elseif-else, ordered highest first) ──── // A (Distinction): Total >= 70 @@ -30,18 +29,72 @@ // C (Pass Lower): Total >= 50 // D (Marginal Pass): Total >= 40 // E (Fail): Total < 40 +$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 + +echo "CAT 1 Score: $cat1 *
"; +echo "CAT 2 Score: $cat2 *
"; +echo "CAT 3 Score: $cat3 *
"; +echo "CAT 4 Score: $cat4 *
"; +echo "Exam Score: $exam *
"; + +$cats_attended = 0; +if ($cat1 > 0) $cats_attended++; +if ($cat2 > 0) $cats_attended++; +if ($cat3 > 0) $cats_attended++; +if ($cat4 > 0) $cats_attended++; + +$total = $cat1 + $cat2 + $cat3 + $cat4 + $exam; +$eligible = ($cats_attended >= 3 && $exam >= 20); + +if ($eligible) { + echo "Total Score: $total * - "; + echo "Eligible for Grade Classification
"; + if ($total >= 70) { + echo $grade = "A (Distinction) "; + } elseif ($total >= 65) { + echo $grade = "B+ (Credit Upper) "; + } elseif ($total >= 60) { + echo $grade = "B (Credit Lower) "; + } elseif ($total >= 55) { + echo $grade = "C+ (Pass Upper) "; + } elseif ($total >= 50) { + echo $grade = "C (Pass Lower) "; + } elseif ($total >= 40) { + echo $grade = "D (Marginal Pass) "; + } else { + echo $grade = "E (Fail) "; + } +} else { + echo "DISQUALIFIED — Exam conditions not met
"; + echo "Not worthy of a grade Classification
"; + $grade = "N/A"; +} + +echo "
"; // ── 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" +// ── STEP 2: Count CATs attended ─────────────────────────────────────────── +// TODO: compute $cats_attended (each CAT > 0 counts as attended) + + +// ── STEP 1: Compute total ───────────────────────────────────────────────── +// TODO: compute $total + + + + // ── Supplementary Rule (implement using ternary) ────────────────────────── // If grade is D: "Eligible for Supplementary Exam" // Otherwise: "Not eligible for supplementary" -// ── STEP 1: Compute total ───────────────────────────────────────────────── -// TODO: compute $total // ── STEP 2: Count CATs attended ─────────────────────────────────────────── @@ -58,9 +111,98 @@ // 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 +?> + + + + + Student Result Card + + + +

Required Test Data Sets

+ ['cat1'=>8,'cat2'=>7,'cat3'=>9,'cat4'=>6,'exam'=>52, 'expected'=>'Grade B'], + 'B' => ['cat1'=>9,'cat2'=>8,'cat3'=>0,'cat4'=>9,'exam'=>55, 'expected'=>'Grade A'] , + 'C' => ['cat1'=>0,'cat2'=>0,'cat3'=>7,'cat4'=>0,'exam'=>48, 'expected'=>'DISQUALIFIED'], + 'D' => ['cat1'=>5,'cat2'=>4,'cat3'=>6,'cat4'=>3,'exam'=>22, 'expected'=>'Grade D + supp eligible'], + 'E' => ['cat1'=>0,'cat2'=>0,'cat3'=>0,'cat4'=>0,'exam'=>15, 'expected'=>'DISQUALIFIED'], + ]; + + function calc_grade($total) { + if ($total >= 70) return 'A (Distinction)'; + if ($total >= 65) return 'B+ (Credit Upper)'; + if ($total >= 60) return 'B (Credit Lower)'; + if ($total >= 55) return 'C+ (Pass Upper)'; + if ($total >= 50) return 'C (Pass Lower)'; + if ($total >= 40) return 'D (Marginal Pass)'; + return 'E (Fail)'; + } + ?> + + + + + + + + + + + + + + + + + + + + $d): + $cats_attended = count(array_filter([$d['cat1'],$d['cat2'],$d['cat3'],$d['cat4']], fn($x) => $x > 0)); + $total = $d['cat1'] + $d['cat2'] + $d['cat3'] + $d['cat4'] + $d['exam']; + $eligible = ($cats_attended >= 3 && $d['exam'] >= 20); + $grade = $eligible ? calc_grade($total) : 'N/A'; + $supp = $eligible ? 'Not eligible' : 'Eligible'; + $eligText = $eligible ? 'Yes' : 'No'; + ?> + + + + + + + + + + + + + + + + +
SetCAT1CAT2CAT3CAT4ExamTotalAttendedEligibleGradeSupplementaryExpected
+ + diff --git a/starter/lab3_task3.php b/starter/lab3_task3.php index d9e00c8..a149a09 100644 --- a/starter/lab3_task3.php +++ b/starter/lab3_task3.php @@ -1,56 +1,118 @@ + "; + break; + case 2: + echo "Tuesday — Lecture day\n" . "
"; + break; + case 3: + echo "Wednesday — Lecture day\n" . "
"; + break; + case 4: + echo "Thursday — Lecture day\n" . "
"; + break; + case 5: + echo "Friday — Lecture day\n" . "
"; + break; + case 6: + echo "Saturday — Weekend\n" . "
"; + break; + case 7: + echo "Sunday — Weekend\n" . "
"; + break; + default: + echo "Invalid day. Please enter a number between 1 and 7.\n"; + } +} +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 +switch ($status_code) { + case 200: + echo "OK — request succeeded\n"; + break; + case 301: + echo "Moved Permanently — resource relocated\n"; + break; + case 400: + echo "Bad Request — client error\n"; + break; + case 401: + echo "Unauthorized — authentication required\n"; + break; + case 403: + echo "Forbidden — access denied\n"; + break; + case 404: + echo "Not Found — resource missing\n"; + break; + case 500: + echo "Internal Server Error — server fault\n"; + break; + default: + echo "Unknown status code\n"; +} +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. // TODO: match expression for HTTP status — same logic as Exercise B +$status_code = 404; + +$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 $message . "\n"; +echo "<
"; // ══════════════════════════════════════════════════════════════ // EXERCISE D — Rewrite comparison @@ -59,3 +121,4 @@ // 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? +?> diff --git a/starter/lab3_task4.php b/starter/lab3_task4.php index 2a3d225..aee2a95 100644 --- a/starter/lab3_task4.php +++ b/starter/lab3_task4.php @@ -6,54 +6,68 @@ * 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 Robin Wanyonyi + * @student ENE212-0208/2023 * @lab Lab 3 of 14 * @unit ICS 2371 - * @date [Date completed] + * @date 03/04/2026 */ -// ── 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" +// HELB Loan Eligibility Checker +?> + + + + Loan Eligibility + + + +

Required Test Data Sets

+ ['enrolled' => true, 'gpa' => 3.1, 'income' => 180000, 'prev_loan' => false], + 'B' => ['enrolled' => true, 'gpa' => 1.8, 'income' => 80000, 'prev_loan' => false], + 'C' => ['enrolled' => false, 'gpa' => 3.5, 'income' => 60000, 'prev_loan' => true], + 'D' => ['enrolled' => true, 'gpa' => 2.5, 'income' => 600000, 'prev_loan' => true], + 'E' => ['enrolled' => true, 'gpa' => 2.0, 'income' => 50000, 'prev_loan' => true], + ]; -// ── 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 + function determine_eligibility($enrolled, $gpa, $income, $prev_loan) { + if (!$enrolled) return "Not enrolled"; + if ($gpa < 2.0) return "GPA fail"; + if ($income > 500000) return "Income fail"; + if ($prev_loan) return "Full | Renewal"; + return "Partial 75% | New"; + } + ?> + + + + + + + + + + + + + $d): ?> + + + + + + + + + + +
SetEnrolledGPAIncome (KES)Prev loanExpected
+ +