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
69 changes: 43 additions & 26 deletions starter/lab3_task1.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,58 +3,68 @@
* 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 Declan Munene
* @student ENE212-0061/2023
* @lab Lab 3 of 14
* @unit ICS 2371
* @date [Date completed]
* @date April 7, 2026
*/

// ══════════════════════════════════════════════════════════════
// 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
$temperature = 39.2;

// TODO: Exercise A — your code here
// Using separate if statements only (no else/elseif)
if ($temperature >= 36.1 && $temperature <= 37.5) {
echo "Temperature ($temperature): Normal<br>";
}

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

if ($temperature < 36.1) {
echo "Temperature ($temperature): Hypothermia Warning<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
$number = 47;

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

// TODO: Exercise B — your code here
// 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>";


// ══════════════════════════════════════════════════════════════
// 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).
echo "<h3>Exercise C Results:</h3>";
var_dump($x == $y); // A: bool(true) - Equality (values match after type juggling)
var_dump($x === $y); // B: bool(false) - Identity (int vs string)
var_dump($x == $z); // C: bool(true) - Equality (values match)
var_dump($x === $z); // D: bool(false) - Identity (int vs float)
var_dump($y === $z); // E: bool(false) - Identity (string vs float)
var_dump($x <=> $y); // F: int(0) - Spaceship operator returns 0 if values are equal


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

$username = null;
$display = $username ?? "Guest";
Expand All @@ -67,4 +77,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: Checking for a user profile picture
$user_upload = null;
$gravatar_url = null;
$placeholder = "default_avatar.png";

$final_image = $user_upload ?? $gravatar_url ?? $placeholder;
echo "Profile Image: $final_image<br>";
?>
139 changes: 93 additions & 46 deletions starter/lab3_task2.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,64 +3,111 @@
* 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.
*
* @author [Your Full Name]
* @student [Your Reg Number, e.g. SCT212-XXXX/2024]
* @author Declan Munene
* @student ENE212-0061/2023
* @lab Lab 3 of 14
* @unit ICS 2371
* @date [Date completed]
* @date April 7, 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

// ── 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"
// ── Test Data Set A (Update these values for Sets B, C, D, and E) ──────────
$name = "Declan Munene";
$cat1 = 8;
$cat2 = 7;
$cat3 = 9;
$cat4 = 6;
$exam = 52;

// ── 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
$eligible = false;
$grade = "N/A";
$desc = "N/A";
$supp_status = "N/A";

if ($cats_attended >= 3 && $exam >= 20) {
$eligible = true;

// Grade classification
if ($total >= 70) {
$grade = "A";
$desc = "Distinction";
} elseif ($total >= 65) {
$grade = "B+";
$desc = "Credit Upper";
} elseif ($total >= 60) {
$grade = "B";
$desc = "Credit Lower";
} elseif ($total >= 55) {
$grade = "C+";
$desc = "Pass Upper";
} elseif ($total >= 50) {
$grade = "C";
$desc = "Pass Lower";
} elseif ($total >= 40) {
$grade = "D";
$desc = "Marginal Pass";
} else {
$grade = "E";
$desc = "Fail";
}

// Supplementary ternary rule
$supp_status = ($grade == "D") ? "Eligible for Supplementary Exam" : "Not eligible for supplementary";

} else {
$eligible = false;
$desc = "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
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>JKUAT Student Report Card</title>
<style>
body { font-family: Arial, sans-serif; margin: 20px; line-height: 1.6; }
.report-card { border: 2px solid #2c3e50; padding: 20px; width: 500px; border-radius: 8px; }
h2 { color: #2c3e50; border-bottom: 2px solid #2c3e50; padding-bottom: 10px; }
.row { display: flex; justify-content: space-between; margin-bottom: 8px; border-bottom: 1px solid #eee; }
.label { font-weight: bold; }
.status-box { margin-top: 15px; padding: 10px; border-radius: 4px; font-weight: bold; text-align: center; }
.eligible { background-color: #d4edda; color: #155724; }
.disqualified { background-color: #f8d7da; color: #721c24; }
</style>
</head>
<body>

<div class="report-card">
<h2>Academic Report Card</h2>
<div class="row"><span class="label">Student Name:</span> <span><?php echo $name; ?></span></div>
<div class="row"><span class="label">CAT Scores:</span> <span><?php echo "$cat1, $cat2, $cat3, $cat4"; ?></span></div>
<div class="row"><span class="label">Exam Score:</span> <span><?php echo $exam; ?> / 60</span></div>
<div class="row"><span class="label">CATs Attended:</span> <span><?php echo $cats_attended; ?> / 4</span></div>
<div class="row"><span class="label">Total Marks:</span> <span><?php echo $total; ?> / 100</span></div>

<?php if ($eligible): ?>
<div class="status-box eligible">
Grade: <?php echo "$grade ($desc)"; ?><br>
<?php echo $supp_status; ?>
</div>
<?php else: ?>
<div class="status-box disqualified">
<?php echo $desc; ?>
</div>
<?php endif; ?>
</div>

// ── 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
</body>
</html>
Loading
Loading