Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Empty file added git
Empty file.
43 changes: 38 additions & 5 deletions starter/lab3_task1.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 [Michelle Muthoni Mwangi]
* @student [ENE212-0064/2023]
* @lab Lab 3 of 14
* @unit ICS 2371
* @date [Date completed]
* @date [08/04/2026]
*/

// ══════════════════════════════════════════════════════════════
Expand All @@ -20,7 +20,20 @@
// "Hypothermia Warning" if temp < 36.1
// Test with: 36.8, 39.2, 34.5 — screenshot each

// TODO: Exercise A — your code here
// Exercise A implementation
$temperature = 39.2;

if ($temperature >= 36.1 && $temperature <= 37.5) {
echo "Normal<br>";
}

if ($temperature > 37.5) {
echo "Fever<br>";
}

if ($temperature < 36.1) {
echo "Hypothermia Warning<br>";
}


// ══════════════════════════════════════════════════════════════
Expand All @@ -30,7 +43,20 @@
// 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 B implementation
$number = 47;

// Even or Odd
if ($number % 2 === 0) {
echo "$number is EVEN<br>";
} else {
echo "$number is ODD<br>";
}

// Divisibility checks
echo ($number % 3 === 0) ? "$number is divisible by 3<br>" : "$number is not divisible by 3<br>";
echo ($number % 5 === 0) ? "$number is divisible by 5<br>" : "$number is not divisible by 5<br>";
echo ($number % 3 === 0 && $number % 5 === 0) ? "$number is divisible by both 3 and 5<br>" : "$number is not divisible by both 3 and 5<br>";


// ══════════════════════════════════════════════════════════════
Expand Down Expand Up @@ -67,4 +93,11 @@
$result = $config_val ?? $env_val ?? $default;
echo "Config: $result<br>";

// Additional chained null coalescing example
$user_pref = null; // user has not set a preference
$system_pref = "dark"; // system-wide preference
$fallback = "light"; // ultimate fallback
$theme = $user_pref ?? $system_pref ?? $fallback;
echo "Theme: $theme<br>";

// TODO: Add one more chained ?? example of your own and explain it in your report.
113 changes: 68 additions & 45 deletions starter/lab3_task2.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,60 +7,83 @@
* 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 [Michelle Muthoni Mwangi]
* @student [ENE212-0064/2023]
* @lab Lab 3 of 14
* @unit ICS 2371
* @date [Date completed]
* @date [08/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
// ── Test Data Sets (A–E)
$testSets = [
'Set A' => [
'name' => 'Michelle Muthoni Mwangi',
'cat1' => 8, 'cat2' => 7, 'cat3' => 9, 'cat4' => 6, 'exam' => 52
],
'Set B' => [
'name' => 'Michelle Muthoni Mwangi',
'cat1' => 9, 'cat2' => 8, 'cat3' => 0, 'cat4' => 9, 'exam' => 55
],
'Set C' => [
'name' => 'Michelle Muthoni Mwangi',
'cat1' => 0, 'cat2' => 0, 'cat3' => 7, 'cat4' => 0, 'exam' => 48
],
'Set D' => [
'name' => 'Michelle Muthoni Mwangi',
'cat1' => 5, 'cat2' => 4, 'cat3' => 6, 'cat4' => 3, 'exam' => 22
],
'Set E' => [
'name' => 'Michelle Muthoni Mwangi',
'cat1' => 0, 'cat2' => 0, 'cat3' => 0, 'cat4' => 0, 'exam' => 15
],
];

// ── 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
function generate_report($label, $name, $cat1, $cat2, $cat3, $cat4, $exam) {
// STEP 1
$total = $cat1 + $cat2 + $cat3 + $cat4 + $exam;

// ── 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
$cats_attended = 0;
foreach ([$cat1, $cat2, $cat3, $cat4] as $cat) {
if ($cat > 0) $cats_attended++;
}

// ── Supplementary Rule (implement using ternary) ──────────────────────────
// If grade is D: "Eligible for Supplementary Exam"
// Otherwise: "Not eligible for supplementary"
// STEP 3
if ($cats_attended >= 3 && $exam >= 20) {
$eligibility = 'ELIGIBLE';

// ── STEP 1: Compute total ─────────────────────────────────────────────────
// TODO: compute $total
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'; }

$supplementary = ($grade === 'D') ? 'Eligible for Supplementary Exam' : 'Not eligible for supplementary';

// ── STEP 2: Count CATs attended ───────────────────────────────────────────
// TODO: compute $cats_attended (each CAT > 0 counts as attended)
} else {
$eligibility = 'DISQUALIFIED — Exam conditions not met';
$grade = 'N/A';
$remark = 'DISQUALIFIED';
$supplementary = 'Not eligible for supplementary';
}

// STEP 4: Output
echo "<hr><h3>$label</h3>";
echo "<strong>Name:</strong> $name<br>";
echo "<strong>CAT1:</strong> $cat1 &nbsp; <strong>CAT2:</strong> $cat2 &nbsp; <strong>CAT3:</strong> $cat3 &nbsp; <strong>CAT4:</strong> $cat4<br>";
echo "<strong>Exam:</strong> $exam<br>";
echo "<strong>Total:</strong> $total<br>";
echo "<strong>CATs attended:</strong> $cats_attended<br>";
echo "<strong>Eligibility:</strong> $eligibility<br>";
echo "<strong>Grade:</strong> $grade<br>";
echo "<strong>Remark:</strong> $remark<br>";
echo "<strong>Supplementary:</strong> $supplementary<br>";
}

// ── 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
// Run reports for all test sets
foreach ($testSets as $label => $data) {
generate_report($label, $data['name'], $data['cat1'], $data['cat2'], $data['cat3'], $data['cat4'], $data['exam']);
}
?>
79 changes: 71 additions & 8 deletions starter/lab3_task3.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 [Michelle Muthoni Mwangi]
* @student [ENE212-0064/2023]
* @lab Lab 3 of 14
* @unit ICS 2371
* @date [Date completed]
* @date [08/04/2026]
*/

// ══════════════════════════════════════════════════════════════
Expand All @@ -21,7 +21,31 @@

$day = 3; // change this to test all cases

// TODO: switch-case for day classifier
// Exercise A: switch-case for day classifier
switch ($day) {
case 1:
echo "Monday — Lecture day<br>";
break;
case 2:
echo "Tuesday — Lecture day<br>";
break;
case 3:
echo "Wednesday — Lecture day<br>";
break;
case 4:
echo "Thursday — Lecture day<br>";
break;
case 5:
echo "Friday — Lecture day<br>";
break;
case 6:
case 7:
echo "Weekend<br>";
break;
default:
echo "Invalid day (must be 1–7)<br>";
break;
}


// ══════════════════════════════════════════════════════════════
Expand All @@ -39,15 +63,54 @@

$status_code = 404;

// TODO: switch-case for HTTP status
// switch-case implementation
switch ($status_code) {
case 200:
$explain = "OK — request succeeded";
break;
case 301:
$explain = "Moved Permanently — resource relocated";
break;
case 400:
$explain = "Bad Request — client error";
break;
case 401:
$explain = "Unauthorized — authentication required";
break;
case 403:
$explain = "Forbidden — access denied";
break;
case 404:
$explain = "Not Found — resource missing";
break;
case 500:
$explain = "Internal Server Error — server fault";
break;
default:
$explain = "Unknown status code";
break;
}

echo "Switch: $status_code -> $explain<br>";


// ══════════════════════════════════════════════════════════════
// 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.
// Rewrite Exercise B using match (strict comparison)

$explain_match = 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 "Match: $status_code -> $explain_match<br>";

// TODO: match expression for HTTP status — same logic as Exercise B

Expand Down
Loading
Loading