diff --git a/starter/lab3_task1.php b/starter/lab3_task1.php index f396a7e..e0f2f10 100644 --- a/starter/lab3_task1.php +++ b/starter/lab3_task1.php @@ -1,70 +1,105 @@ - 37.5 -// "Hypothermia Warning" if temp < 36.1 -// Test with: 36.8, 39.2, 34.5 — screenshot each - -// TODO: Exercise A — your code here - - -// ══════════════════════════════════════════════════════════════ -// 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 - -// TODO: Exercise B — your code here - - -// ══════════════════════════════════════════════════════════════ -// EXERCISE C — Comparison Chain -// ══════════════════════════════════════════════════════════════ -// Run this code EXACTLY as written. -// Record all six outputs in your report and explain each result. - -$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? - -// Your explanation of each result goes in your PDF report (not here). - - -// ══════════════════════════════════════════════════════════════ -// EXERCISE D — Null & Default Values -// ══════════════════════════════════════════════════════════════ -// Run this code as written, then extend it as instructed below. - -$username = null; -$display = $username ?? "Guest"; -echo "Welcome, $display
"; - -// Chained null coalescing -$config_val = null; -$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. + 37.5 +// "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; +echo "Test temperature: $temperature \n"; + +if ($temperature >= 36.1 && $temperature <= 37.5) { + echo "Result:Normal \n"; +} +if ($temperature > 37.5) { + echo "Result:Fever \n"; +} +if ($temperature < 36.1) { + echo "Result:Hypothermia Warning \n"; +} +//line break +echo "\n"; + + +// ══════════════════════════════════════════════════════════════ +// 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 + +// TODO: Exercise B — your code here +$number = 47; +if ($number % 2 == 0) { + echo "$number is EVEN"; +} else { + echo "$number is ODD"; +} + +//fizzbuzz type of problem +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"; +} + + +// ══════════════════════════════════════════════════════════════ +// EXERCISE C — Comparison Chain +// ══════════════════════════════════════════════════════════════ +// Run this code EXACTLY as written. +// Record all six outputs in your report and explain each result. + +$x = 10; $y = "10"; $z = 10.0; + +var_dump($x == $y); // A: boolean true — because == does type juggling and considers 10 and "10" equal +var_dump($x === $y); // B: boolean false — because === checks for both value and type, and 10 (integer) is not the same type as "10" (string) +var_dump($x == $z); // C: boolean true — because == does type juggling and considers 10 and 10.0 equal (both are numeric and have the same value) +var_dump($x === $z); // D: boolean false — because === checks for both value and type, and 10 (integer) is not the same type as 10.0 (float) +var_dump($y === $z); // E: boolean false — because === checks for both value and type, and "10" (string) is not the same type as 10.0 (float) +var_dump($x <=> $y); // F: int(0) — because the spaceship operator <=> returns 0 when the two operands are equal in value (after type juggling) + +// Your explanation of each result goes in your PDF report (not here). + + +// ══════════════════════════════════════════════════════════════ +// EXERCISE D — Null & Default Values +// ══════════════════════════════════════════════════════════════ +// Run this code as written, then extend it as instructed below. + +$username = null; +$display = $username ?? "Guest"; +echo "Welcome, $display
"; + + +$config_val = null; +$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_val = null; +$result2 = $custom_val ?? $env_val ?? $default; +echo "Custom: $result2
"; + diff --git a/starter/lab3_task2.php b/starter/lab3_task2.php index b03e13c..08116f2 100644 --- a/starter/lab3_task2.php +++ b/starter/lab3_task2.php @@ -1,66 +1,109 @@ -= 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 - - -// ── STEP 2: Count CATs attended ─────────────────────────────────────────── -// TODO: compute $cats_attended (each CAT > 0 counts as attended) - - -// ── 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 += 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) { + // eligible — now determine grade + if ($total >= 70) { + $grade = "A (Distinction)"; + } elseif ($total >= 65) { + $grade = "B+ (Credit Upper)"; + } elseif ($total >= 60) { + $grade = "B (Credit Lower)"; + } elseif ($total >= 55) { + $grade = "C+ (Pass Upper)"; + } elseif ($total >= 50) { + $grade = "C (Pass Lower)"; + } elseif ($total >= 40) { + $grade = "D (Marginal Pass)"; + } else { + $grade = "E (Fail)"; + } +} else { + // not eligible + $grade = "DISQUALIFIED — Exam conditions not met"; +} + + + +// ── 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 "

Report Card

"; +echo "

Student Name: $name

"; +echo "

CAT 1: $cat1

"; +echo "

CAT 2: $cat2

"; +echo "

CAT 3: $cat3

"; +echo "

CAT 4: $cat4

"; +echo "

Exam: $exam

"; +echo "

Total: $total

"; +echo "

CATs Attended: $cats_attended

"; +echo "

Eligibility Status: " . ($cats_attended >= 3 && $exam >= 20 ? "Eligible" : "DISQUALIFIED") . "

"; +echo "

Grade: $grade

"; +echo "

Remark: " . ($grade == "E (Fail)" ? "Needs Improvement" : "Well Done") . "

"; +echo "

Supplementary Status: " . ($grade == "D (Marginal Pass)" ? "Eligible for Supplementary Exam" : "Not eligible for 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..a2337cf 100644 --- a/starter/lab3_task3.php +++ b/starter/lab3_task3.php @@ -1,61 +1,125 @@ - "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 $result . "\n"; +// ══════════════════════════════════════════════════════════════ +// 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? diff --git a/starter/lab3_task4.php b/starter/lab3_task4.php index 2a3d225..5779da7 100644 --- a/starter/lab3_task4.php +++ b/starter/lab3_task4.php @@ -1,59 +1,81 @@ -= 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 += 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 = 2.0; +$annual_income = 50000; +$previous_loan = true; +$decision = ""; // to store final decision message + +// ── STEP 1: Outer enrollment check ──────────────────────────────────────── +// TODO: nested if structure implementing all rules above +if (!$enrolled) { + $decision = "Not eligible — must be an active student"; +} else { + if ($gpa < 2.0) { + $decision = "Not eligible — GPA below minimum (2.0)"; + } else { + if ($annual_income < 100000) { + $decision = "Eligible — Full loan award"; + } elseif ($annual_income < 250000) { + $decision = "Eligible — Partial loan (75%)"; + } elseif ($annual_income < 500000) { + $decision = "Eligible — Partial loan (50%)"; + } else { + $decision = "Not eligible — household income exceeds threshold"; + } + } +} + +// ── STEP 2: Display result ──────────────────────────────────────────────── +// TODO: output formatted result showing all input values and the decision +echo "Loan Application Result:\n"; +echo "Enrolled: " . ($enrolled ? "Yes" : "No") . "\n"; +echo "GPA: " . $gpa . "\n"; +echo "Annual Income: " . $annual_income . "\n"; +echo "Previous Loan: " . ($previous_loan ? "Yes" : "No") . "\n"; +echo "Decision: " . $decision . "\n"; + +// ── 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