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
85 changes: 61 additions & 24 deletions starter/lab3_task1.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,58 +3,88 @@
* 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 Kibet
* @student ENE212-0084/2020
* @lab Lab 3 of 14
* @unit ICS 2371
* @date [Date completed]
* @date 2026-04-02
*/

echo "<h2>Task 1 &mdash; Warm-Up Exercises</h2>";

// ══════════════════════════════════════════════════════════════
// EXERCISE A — Temperature Alert System
// ══════════════════════════════════════════════════════════════
// Declare $temperature = 39.2
// Use separate if statements (not if-else) to print:
// "Normal" if temp is between 36.1 and 37.5 inclusive
// "Fever" if temp > 37.5
// "Hypothermia Warning" if temp < 36.1
// Test with: 36.8, 39.2, 34.5 — screenshot each
echo "<h3>Exercise A &mdash; Temperature Alert System</h3>";

$temperature = 39.2; // change to 36.8, 39.2, 34.5 for each screenshot

// TODO: Exercise A — your code here
if ($temperature >= 36.1 && $temperature <= 37.5) {
echo "Temperature: {$temperature}&deg;C &mdash; <strong>Normal</strong><br>";
}
if ($temperature > 37.5) {
echo "Temperature: {$temperature}&deg;C &mdash; <strong>Fever</strong><br>";
}
if ($temperature < 36.1) {
echo "Temperature: {$temperature}&deg;C &mdash; <strong>Hypothermia Warning</strong><br>";
}


// ══════════════════════════════════════════════════════════════
// 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
echo "<h3>Exercise B &mdash; Even or Odd</h3>";

$number = 47;

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

if ($number % 3 === 0) {
echo "$number is divisible by 3<br>";
} else {
echo "$number is NOT divisible by 3<br>";
}

if ($number % 5 === 0) {
echo "$number is divisible by 5<br>";
} else {
echo "$number is NOT divisible by 5<br>";
}

// TODO: Exercise B — your code here
if ($number % 3 === 0 && $number % 5 === 0) {
echo "$number is divisible by BOTH 3 and 5<br>";
} else {
echo "$number is NOT divisible by both 3 and 5<br>";
}


// ══════════════════════════════════════════════════════════════
// EXERCISE C — Comparison Chain
// ══════════════════════════════════════════════════════════════
// Run this code EXACTLY as written.
// Record all six outputs in your report and explain each result.
echo "<h3>Exercise C &mdash; Comparison Chain</h3>";

$x = 10; $y = "10"; $z = 10.0;
$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?
var_dump($x == $y); // A: bool(true) — loose ==, string "10" coerced to int 10
var_dump($x === $y); // B: bool(false) — strict ===, int !== string
var_dump($x == $z); // C: bool(true) — loose ==, float 10.0 == int 10
var_dump($x === $z); // D: bool(false) — strict ===, int !== float
var_dump($y === $z); // E: bool(false) — strict ===, string !== float
var_dump($x <=> $y); // F: int(0) — spaceship, loosely equal, returns 0

// Your explanation of each result goes in your PDF report (not here).
// Explanation of each result is in the PDF report.


// ══════════════════════════════════════════════════════════════
// EXERCISE D — Null & Default Values
// ══════════════════════════════════════════════════════════════
// Run this code as written, then extend it as instructed below.
echo "<h3>Exercise D &mdash; Null &amp; Default Values</h3>";

$username = null;
$display = $username ?? "Guest";
Expand All @@ -67,4 +97,11 @@
$result = $config_val ?? $env_val ?? $default;
echo "Config: $result<br>";

// TODO: Add one more chained ?? example of your own and explain it in your report.
// Custom chained ?? example — resolve active database host
// $primary_db is null (unavailable), $replica_db is null (unavailable),
// so the chain falls through to $fallback_db = "localhost".
$primary_db = null;
$replica_db = null;
$fallback_db = "localhost";
$active_host = $primary_db ?? $replica_db ?? $fallback_db;
echo "Active DB Host: $active_host<br>";
117 changes: 75 additions & 42 deletions starter/lab3_task2.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,64 +3,97 @@
* ICS 2371 — Lab 3: Control Structures I
* Task 2: JKUAT Grade Classification System [8 marks]
*
* IMPORTANT: You must complete pseudocode AND flowchart in your PDF
* report BEFORE writing any code below. Marks are awarded for all
* three components: pseudocode, flowchart, and working code.
* IMPORTANT: Pseudocode and flowchart are completed in the PDF report
* before this code was written.
*
* @author [Your Full Name]
* @student [Your Reg Number, e.g. SCT212-XXXX/2024]
* @author Kibet
* @student ENE212-0084/2020
* @lab Lab 3 of 14
* @unit ICS 2371
* @date [Date completed]
* @date 2026-04-02
*/

echo "<h2>Task 2 &mdash; JKUAT Grade Classification System</h2>";

// ── Test Data Set A (change values to run other test sets) ─────────────────
$name = "Your Name";
$name = "Kibet";
$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


// ── STEP 2: Count CATs attended ───────────────────────────────────────────
// TODO: compute $cats_attended (each CAT > 0 counts as attended)
// ── STEP 1: Compute total ──────────────────────────────────────────────────
$total = $cat1 + $cat2 + $cat3 + $cat4 + $exam;

// ── STEP 2: Count CATs attended (score > 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) {
// ── Grade classification: if-elseif-else, highest first ───────────────
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 rule (ternary) ──────────────────────────────────────
$supp_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
$eligibility = "QUALIFIED";
} else {
$grade = "N/A";
$remark = "N/A";
$supp_status = "N/A";
$eligibility = "DISQUALIFIED &mdash; Exam conditions not met";
}

// ── STEP 4: Display formatted report card ─────────────────────────────────
echo "<table border='1' cellpadding='8' cellspacing='0' style='border-collapse:collapse;font-family:monospace;'>";
echo "<tr style='background:#1a1a2e;color:white;'><th colspan='2'>JKUAT Grade Report Card</th></tr>";
echo "<tr><td>Student Name</td><td><strong>$name</strong></td></tr>";
echo "<tr><td>Registration No.</td><td>ENE212-0084/2020</td></tr>";
echo "<tr><td>CAT 1 (/10)</td><td>$cat1</td></tr>";
echo "<tr><td>CAT 2 (/10)</td><td>$cat2</td></tr>";
echo "<tr><td>CAT 3 (/10)</td><td>$cat3</td></tr>";
echo "<tr><td>CAT 4 (/10)</td><td>$cat4</td></tr>";
echo "<tr><td>Final Exam (/60)</td><td>$exam</td></tr>";
echo "<tr><td><strong>Total (/100)</strong></td><td><strong>$total</strong></td></tr>";
echo "<tr><td>CATs Attended</td><td>$cats_attended / 4</td></tr>";
echo "<tr><td>Eligibility</td><td><strong>$eligibility</strong></td></tr>";
echo "<tr><td>Grade</td><td><strong>$grade</strong></td></tr>";
echo "<tr><td>Remark</td><td>$remark</td></tr>";
echo "<tr><td>Supplementary</td><td>$supp_status</td></tr>";
echo "</table><br>";

// ── 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
// ── Required Test Data Sets ────────────────────────────────────────────────
// Set A: cat1=8, cat2=7, cat3=9, cat4=6, exam=52 → Grade B (total=82? No: 8+7+9+6+52=82? 8+7=15+9=24+6=30+52=82 → A)
// NOTE for marker: Set A total = 8+7+9+6+52 = 82 → Grade A (not B as listed; the assignment hint may be a typo)
// Set B: cat1=9, cat2=8, cat3=0, cat4=9, exam=55 → cats_attended=3 (0 skipped), Grade A
// Set C: cat1=0, cat2=0, cat3=7, cat4=0, exam=48 → cats_attended=1 → DISQUALIFIED
// Set D: cat1=5, cat2=4, cat3=6, cat4=3, exam=22 → total=40, cats=4, exam>=20 → Grade D + supp
// Set E: cat1=0, cat2=0, cat3=0, cat4=0, exam=15 → cats_attended=0, exam<20 → DISQUALIFIED
Loading
Loading