diff --git a/starter/lab3_task1.php b/starter/lab3_task1.php index f396a7e..52435b0 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 [MUTWIRI KELVIN MWENDA] + * @student [ENE212-0067/2023] * @lab Lab 3 of 14 * @unit ICS 2371 - * @date [Date completed] + * @date [4TH APRIL 2026] */ // ══════════════════════════════════════════════════════════════ @@ -20,8 +20,17 @@ // "Hypothermia Warning" if temp < 36.1 // Test with: 36.8, 39.2, 34.5 — screenshot each -// TODO: Exercise A — your code here +$temperature = 39.2; +if ($temperature >= 36.1 && $temperature <= 37.5) { + echo "Normal
"; +} +if ($temperature > 37.5) { + echo "Fever
"; +} +if ($temperature < 36.1) { + echo "Hypothermia Warning
"; +} // ══════════════════════════════════════════════════════════════ // EXERCISE B — Even or Odd @@ -30,7 +39,17 @@ // 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; + +if ($number % 2 == 0) { + echo "$number is EVEN
"; +} else { + echo "$number is ODD
"; +} + +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
"; // ══════════════════════════════════════════════════════════════ @@ -67,4 +86,13 @@ $result = $config_val ?? $env_val ?? $default; echo "Config: $result
"; -// TODO: Add one more chained ?? example of your own and explain it in your report. +// Extended chained ?? example: Cloudinary API Image Fallback +$cloudinary_api_url = null; // Primary source: API didn't return an image +$local_upload_url = null; // Secondary source: No local file was uploaded +$placeholder_image = "default_avatar.jpg"; // Fallback source + +$final_image_to_load = $cloudinary_api_url ?? $local_upload_url ?? $placeholder_image; + +echo "Image source to load: $final_image_to_load
"; + +?> \ No newline at end of file diff --git a/starter/lab3_task2.php b/starter/lab3_task2.php index b03e13c..6f40622 100644 --- a/starter/lab3_task2.php +++ b/starter/lab3_task2.php @@ -7,60 +7,84 @@ * 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 [MUTWIRI KELVIN MWENDA] + * @student [ENE212-0067/2023] * @lab Lab 3 of 14 * @unit ICS 2371 - * @date [Date completed] + * @date [4TH APRIL 2026] */ // ── Test Data Set A (change values to run other test sets) ───────────────── -$name = "Your Name"; +$name = "MUTWIRI KELVIN MWENDA"; $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 -// 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 - -// ── 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" - -// ── 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 - +$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 +if ($cats_attended >= 3 && $exam >= 20) { + $eligibility_status = "Eligible"; + + // Eligible — determine grade and remark + 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"; + } +} else { + // Not eligible + $eligibility_status = "Not Eligible"; + $grade = "DISQUALIFIED — Exam conditions not met"; + $remark = "N/A"; +} +// Supplementary Rule (ternary) +$supplementary_status = ($grade == "D") ? "Eligible for Supplementary Exam" : "Not eligible for supplementary"; // ── 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 - +echo "

JKUAT Grade Report Card

"; +echo "

Student Name: $name

"; +echo "

CAT Scores: $cat1, $cat2, $cat3, $cat4

"; +echo "

Exam Score: $exam

"; +echo "

Total Score: $total

"; +echo "

CATs Attended: $cats_attended

"; +echo "

Eligibility Status: $eligibility_status

"; +echo "

Grade: $grade

"; +echo "

Remark: $remark

"; +echo "

Supplementary Status: $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 +// Set A: cat1=8, cat2=7, cat3=9, cat4=6, exam=52 → expect grade A (Total 82) +// Set B: cat1=9, cat2=8, cat3=0, cat4=9, exam=55 → expect grade A (Total 81, eligible) +// Set C: cat1=0, cat2=0, cat3=7, cat4=0, exam=48 → expect DISQUALIFIED (Only 1 CAT) +// Set D: cat1=5, cat2=4, cat3=6, cat4=3, exam=22 → expect grade D + supp eligible (Total 40) +// Set E: cat1=0, cat2=0, cat3=0, cat4=0, exam=15 → expect DISQUALIFIED (0 CATs, exam < 20) +?> \ No newline at end of file diff --git a/starter/lab3_task3.php b/starter/lab3_task3.php index d9e00c8..a6a7088 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 [MUTWIRI KELVIN MWENDA] + * @student [ENE212-0067/2023] * @lab Lab 3 of 14 * @unit ICS 2371 - * @date [Date completed] + * @date [4TH APRIL 2026] */ // ══════════════════════════════════════════════════════════════ @@ -17,45 +17,86 @@ // 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. $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"; +} +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"; + 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. - -// TODO: match expression for HTTP status — same logic as Exercise B +$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 . "
"; // ══════════════════════════════════════════════════════════════ -// EXERCISE D — Rewrite comparison +// EXERCISE D — Rewrite comparison (To be answered in PDF report) // ══════════════════════════════════════════════════════════════ -// 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? +?> \ No newline at end of file diff --git a/starter/lab3_task4.php b/starter/lab3_task4.php index 2a3d225..a07ebd3 100644 --- a/starter/lab3_task4.php +++ b/starter/lab3_task4.php @@ -6,36 +6,14 @@ * 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 [MUTWIRI KELVIN MWENDA] + * @student [ENE212-0067/2023] * @lab Lab 3 of 14 * @unit ICS 2371 - * @date [Date completed] + * @date [4TH APRIL 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" // ── Test data (change to test all branches) ─────────────────────────────── $enrolled = true; @@ -44,12 +22,43 @@ $previous_loan = false; // ── STEP 1: Outer enrollment check ──────────────────────────────────────── -// TODO: nested if structure implementing all rules above +$decision = ""; +if ($enrolled) { + // INNER CHECK 1 — GPA requirement + if ($gpa >= 2.0) { + + // Ternary for renewal vs new application + $app_type = $previous_loan ? " | Renewal application" : " | New application"; + + // INNER CHECK 2 — Household income bracket + if ($annual_income < 100000) { + $decision = "Eligible — Full loan award" . $app_type; + } elseif ($annual_income < 250000) { + $decision = "Eligible — Partial loan (75%)" . $app_type; + } elseif ($annual_income < 500000) { + $decision = "Eligible — Partial loan (50%)" . $app_type; + } else { + $decision = "Not eligible — household income exceeds threshold"; + } + + } else { + $decision = "Not eligible — GPA below minimum (2.0)"; + } +} else { + $decision = "Not eligible — must be an active student"; +} // ── STEP 2: Display result ──────────────────────────────────────────────── -// TODO: output formatted result showing all input values and the decision +$enrolled_display = $enrolled ? "Yes" : "No"; +$previous_display = $previous_loan ? "Yes" : "No"; +echo "

HELB Loan Application Status

"; +echo "Enrolled: " . $enrolled_display . "
"; +echo "GPA: " . $gpa . "
"; +echo "Annual Income: KES " . number_format($annual_income) . "
"; +echo "Previous Loan: " . $previous_display . "

"; +echo "DECISION: " . $decision . "

"; // ── Required Test Data Sets — screenshot each ───────────────────────────── // Set A: enrolled=true, gpa=3.1, income=180000, previous=false → Partial 75% @@ -57,3 +66,4 @@ // 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 +?> \ No newline at end of file