Skip to content

Commit ebe4901

Browse files
feat: lab3 complete - ENE212-0071/2023
1 parent 61a1305 commit ebe4901

File tree

4 files changed

+313
-80
lines changed

4 files changed

+313
-80
lines changed

starter/lab3_task1.php

Lines changed: 66 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -3,58 +3,91 @@
33
* ICS 2371 — Lab 3: Control Structures I
44
* Task 1: Simple if and if-else — Warm-Up Exercises [5 marks]
55
*
6-
* @author [Your Full Name]
7-
* @student [Your Reg Number, e.g. SCT212-XXXX/2024]
6+
* @author [Joshua Mativo]
7+
* @student [ENE212-0071/2023]
88
* @lab Lab 3 of 14
99
* @unit ICS 2371
10-
* @date [Date completed]
10+
* @date [2/4/2026]
1111
*/
1212

13+
echo "<h2>Task 1 — Warm-Up Exercises</h2>";
14+
echo "<hr>";
15+
1316
// ══════════════════════════════════════════════════════════════
1417
// EXERCISE A — Temperature Alert System
1518
// ══════════════════════════════════════════════════════════════
16-
// Declare $temperature = 39.2
17-
// Use separate if statements (not if-else) to print:
18-
// "Normal" if temp is between 36.1 and 37.5 inclusive
19-
// "Fever" if temp > 37.5
20-
// "Hypothermia Warning" if temp < 36.1
21-
// Test with: 36.8, 39.2, 34.5 — screenshot each
19+
echo "<h3>Exercise A — Temperature Alert System</h3>";
20+
21+
$temperature = 34.5; // change to 36.8 and 34.5 for other test screenshots
2222

23-
// TODO: Exercise A — your code here
23+
echo "Temperature: {$temperature}°C &nbsp;→&nbsp; ";
2424

25+
if ($temperature >= 36.1 && $temperature <= 37.5) {
26+
echo "<strong>Normal</strong>";
27+
}
28+
if ($temperature > 37.5) {
29+
echo "<strong style='color:red;'>Fever</strong>";
30+
}
31+
if ($temperature < 36.1) {
32+
echo "<strong style='color:blue;'>Hypothermia Warning</strong>";
33+
}
34+
echo "<br>";
2535

2636
// ══════════════════════════════════════════════════════════════
2737
// EXERCISE B — Even or Odd
2838
// ══════════════════════════════════════════════════════════════
29-
// Declare $number = 47
30-
// Use if-else to print "$number is EVEN" or "$number is ODD"
31-
// Also check divisibility by 3, by 5, and by both 3 and 5 — one line each
39+
echo "<h3>Exercise B — Even or Odd</h3>";
40+
41+
$number = 47;
3242

33-
// TODO: Exercise B — your code here
43+
if ($number % 2 === 0) {
44+
echo "$number is <strong>EVEN</strong><br>";
45+
} else {
46+
echo "$number is <strong>ODD</strong><br>";
47+
}
3448

49+
// Divisibility checks
50+
if ($number % 3 === 0) {
51+
echo "$number is divisible by 3<br>";
52+
} else {
53+
echo "$number is NOT divisible by 3<br>";
54+
}
55+
56+
if ($number % 5 === 0) {
57+
echo "$number is divisible by 5<br>";
58+
} else {
59+
echo "$number is NOT divisible by 5<br>";
60+
}
61+
62+
if ($number % 3 === 0 && $number % 5 === 0) {
63+
echo "$number is divisible by both 3 and 5<br>";
64+
} else {
65+
echo "$number is NOT divisible by both 3 and 5<br>";
66+
}
3567

3668
// ══════════════════════════════════════════════════════════════
3769
// EXERCISE C — Comparison Chain
3870
// ══════════════════════════════════════════════════════════════
39-
// Run this code EXACTLY as written.
40-
// Record all six outputs in your report and explain each result.
71+
echo "<h3>Exercise C — Comparison Chain</h3>";
72+
echo "<pre>";
4173

4274
$x = 10; $y = "10"; $z = 10.0;
4375

44-
var_dump($x == $y); // A: ?
45-
var_dump($x === $y); // B: ?
46-
var_dump($x == $z); // C: ?
47-
var_dump($x === $z); // D: ?
48-
var_dump($y === $z); // E: ?
49-
var_dump($x <=> $y); // F: spaceship — what type? what value?
76+
echo "x = 10 (int), y = \"10\" (string), z = 10.0 (float)\n\n";
5077

51-
// Your explanation of each result goes in your PDF report (not here).
78+
echo "A: var_dump(\$x == \$y) → "; var_dump($x == $y); // bool(true)
79+
echo "B: var_dump(\$x === \$y) → "; var_dump($x === $y); // bool(false)
80+
echo "C: var_dump(\$x == \$z) → "; var_dump($x == $z); // bool(true)
81+
echo "D: var_dump(\$x === \$z) → "; var_dump($x === $z); // bool(false)
82+
echo "E: var_dump(\$y === \$z) → "; var_dump($y === $z); // bool(false)
83+
echo "F: var_dump(\$x <=> \$y) → "; var_dump($x <=> $y); // int(0)
5284

85+
echo "</pre>";
5386

5487
// ══════════════════════════════════════════════════════════════
5588
// EXERCISE D — Null & Default Values
5689
// ══════════════════════════════════════════════════════════════
57-
// Run this code as written, then extend it as instructed below.
90+
echo "<h3>Exercise D — Null Coalescing Operator (??)</h3>";
5891

5992
$username = null;
6093
$display = $username ?? "Guest";
@@ -68,3 +101,12 @@
68101
echo "Config: $result<br>";
69102

70103
// TODO: Add one more chained ?? example of your own and explain it in your report.
104+
// Custom example — simulating a user profile with fallback display name
105+
$db_name = null; // not found in database
106+
$session_name = null; // not set in session
107+
$cookie_name = "Alice"; // found in cookie
108+
$fallback = "Anonymous";
109+
110+
$display_name = $db_name ?? $session_name ?? $cookie_name ?? $fallback;
111+
echo "Display name (db → session → cookie → fallback): <strong>$display_name</strong><br>";
112+
echo "<small>Explanation: \$db_name and \$session_name are null, so PHP falls through to \$cookie_name = 'Alice'.</small><br>";

starter/lab3_task2.php

Lines changed: 82 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -7,20 +7,23 @@
77
* report BEFORE writing any code below. Marks are awarded for all
88
* three components: pseudocode, flowchart, and working code.
99
*
10-
* @author [Your Full Name]
11-
* @student [Your Reg Number, e.g. SCT212-XXXX/2024]
10+
* @author [Joshua Mativo]
11+
* @student [ENE212-0071/2023]
1212
* @lab Lab 3 of 14
1313
* @unit ICS 2371
14-
* @date [Date completed]
14+
* @date [2/4/2026]
1515
*/
1616

17+
echo "<h2>Task 2 — JKUAT Grade Classification System</h2>";
18+
echo "<hr>";
19+
1720
// ── Test Data Set A (change values to run other test sets) ─────────────────
1821
$name = "Your Name";
19-
$cat1 = 8; // out of 10
20-
$cat2 = 7; // out of 10
21-
$cat3 = 9; // out of 10
22-
$cat4 = 6; // out of 10
23-
$exam = 52; // out of 60
22+
$cat1 = 0; // out of 10
23+
$cat2 = 0; // out of 10
24+
$cat3 = 0; // out of 10
25+
$cat4 = 0; // out of 10
26+
$exam = 15; // out of 60
2427

2528
// ── Grade Rules (implement using if-elseif-else, ordered highest first) ────
2629
// A (Distinction): Total >= 70
@@ -41,23 +44,85 @@
4144
// Otherwise: "Not eligible for supplementary"
4245

4346
// ── STEP 1: Compute total ─────────────────────────────────────────────────
44-
// TODO: compute $total
45-
47+
$total = $cat1 + $cat2 + $cat3 + $cat4 + $exam;
4648

4749
// ── STEP 2: Count CATs attended ───────────────────────────────────────────
48-
// TODO: compute $cats_attended (each CAT > 0 counts as attended)
49-
50+
$cats_attended = 0;
51+
if ($cat1 > 0) $cats_attended++;
52+
if ($cat2 > 0) $cats_attended++;
53+
if ($cat3 > 0) $cats_attended++;
54+
if ($cat4 > 0) $cats_attended++;
5055

5156
// ── STEP 3: Eligibility check (nested if) ─────────────────────────────────
52-
// TODO: nested if — eligibility → grade classification → supplementary ternary
57+
$eligibility = "";
58+
$grade = "";
59+
$remark = "";
60+
$supplementary = "";
5361

62+
if ($cats_attended >= 3 && $exam >= 20) {
5463

55-
// ── STEP 4: Display formatted HTML report card ────────────────────────────
56-
// TODO: output a clear, formatted report card showing:
57-
// student name, each CAT score, exam score, total,
58-
// cats attended, eligibility status, grade, remark, supplementary status
64+
// Grade classification — highest first
65+
if ($total >= 70) {
66+
$grade = "A";
67+
$remark = "Distinction";
68+
} elseif ($total >= 65) {
69+
$grade = "B+";
70+
$remark = "Credit Upper";
71+
} elseif ($total >= 60) {
72+
$grade = "B";
73+
$remark = "Credit Lower";
74+
} elseif ($total >= 55) {
75+
$grade = "C+";
76+
$remark = "Pass Upper";
77+
} elseif ($total >= 50) {
78+
$grade = "C";
79+
$remark = "Pass Lower";
80+
} elseif ($total >= 40) {
81+
$grade = "D";
82+
$remark = "Marginal Pass";
83+
} else {
84+
$grade = "E";
85+
$remark = "Fail";
86+
}
87+
88+
$eligibility = "ELIGIBLE";
5989

90+
// Supplementary ternary
91+
$supplementary = ($grade === "D")
92+
? "Eligible for Supplementary Exam"
93+
: "Not eligible for supplementary";
6094

95+
} else {
96+
$eligibility = "DISQUALIFIED — Exam conditions not met";
97+
$grade = "N/A";
98+
$remark = "N/A";
99+
$supplementary = "N/A";
100+
}
101+
102+
// ── STEP 4: Display formatted HTML report card ────────────────────────────
103+
$eligible_color = str_starts_with($eligibility, "DISQ") ? "#c0392b" : "#27ae60";
104+
?>
105+
<div style="font-family:Arial;max-width:480px;border:2px solid #2E75B6;border-radius:8px;padding:20px;margin:20px auto;">
106+
<h3 style="color:#1F3864;text-align:center;margin:0 0 12px;">JKUAT Grade Report Card</h3>
107+
<hr style="border-color:#2E75B6;">
108+
<table style="width:100%;border-collapse:collapse;font-size:14px;">
109+
<tr><td><b>Student:</b></td><td><?= htmlspecialchars($name) ?></td></tr>
110+
<tr><td><b>CAT 1:</b></td><td><?= $cat1 ?> / 10</td></tr>
111+
<tr><td><b>CAT 2:</b></td><td><?= $cat2 ?> / 10</td></tr>
112+
<tr><td><b>CAT 3:</b></td><td><?= $cat3 ?> / 10</td></tr>
113+
<tr><td><b>CAT 4:</b></td><td><?= $cat4 ?> / 10</td></tr>
114+
<tr><td><b>Final Exam:</b></td><td><?= $exam ?> / 60</td></tr>
115+
<tr><td><b>Total:</b></td><td><?= $total ?> / 100</td></tr>
116+
<tr><td><b>CATs Attended:</b></td><td><?= $cats_attended ?> of 4</td></tr>
117+
<tr><td><b>Status:</b></td>
118+
<td style="color:<?= $eligible_color ?>;font-weight:bold;"><?= $eligibility ?></td></tr>
119+
<tr><td><b>Grade:</b></td><td style="font-size:1.3em;color:#2E75B6;"><b><?= $grade ?></b></td></tr>
120+
<tr><td><b>Remark:</b></td><td><?= $remark ?></td></tr>
121+
<tr><td><b>Supplementary:</b></td><td><?= $supplementary ?></td></tr>
122+
</table>
123+
</div>
124+
125+
<?php
61126
// ── Required Test Data Sets — screenshot each ─────────────────────────────
62127
// Set A: cat1=8, cat2=7, cat3=9, cat4=6, exam=52 → expect grade B
63128
// Set B: cat1=9, cat2=8, cat3=0, cat4=9, exam=55 → expect grade A (check cats_attended)

0 commit comments

Comments
 (0)