Skip to content
Closed
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
114 changes: 74 additions & 40 deletions starter/lab3_task1.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,68 +3,102 @@
* 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 [wayne naum ]
* @student [ENE 212-0085/2023]
* @lab Lab 3 of 14
* @unit ICS 2371
* @date [Date completed]
*/

// ══════════════════════════════════════════════════════════════
// ==========================================
// 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</h3>";

// TODO: Exercise A — your code here
// 1. Declare the variable (Starting with the first test value)
$temperature = 34.5;

// 2. Check if Normal (between 36.1 and 37.5 inclusive)
if ($temperature >= 36.1 && $temperature <= 37.5) {
echo "Normal<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
// 3. Check if Fever (greater than 37.5)
if ($temperature > 37.5) {
echo "Fever<br>";
}

// TODO: Exercise B — your code here
// 4. Check for Hypothermia (less than 36.1)
if ($temperature < 36.1) {
echo "Hypothermia Warning<br>";
}

echo "<hr>";
// ==========================================
// EXERCISE B — Even or Odd
// ==========================================
echo "<h3>Exercise B</h3>";

// ══════════════════════════════════════════════════════════════
// EXERCISE C — Comparison Chain
// ══════════════════════════════════════════════════════════════
// Run this code EXACTLY as written.
// Record all six outputs in your report and explain each result.
// 1. Declare the variable
$number = 47;

$x = 10; $y = "10"; $z = 10.0;
// 2. Check if Even or Odd using an if-else statement
if ($number % 2 == 0) {
echo "$number is EVEN<br>";
} else {
echo "$number is ODD<br>";
}

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?
// 3. Divisibility checks (using separate if statements)
if ($number % 3 == 0) {
echo "$number is divisible by 3<br>";
}
if ($number % 5 == 0) {
echo "$number is divisible by 5<br>";
}
if ($number % 3 == 0 && $number % 5 == 0) {
echo "$number is divisible by both 3 and 5<br>";
}

// Your explanation of each result goes in your PDF report (not here).
echo "<hr>";
// ==========================================
// EXERCISE C — Comparison Chain
// ==========================================
echo "<h3>Exercise C</h3>";

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

var_dump($x == $y); echo "<br>"; // A
var_dump($x === $y); echo "<br>"; // B
var_dump($x == $z); echo "<br>"; // C
var_dump($x === $z); echo "<br>"; // D
var_dump($y === $z); echo "<br>"; // E
var_dump($x <=> $y); echo "<br>"; // F

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

$username = null;
$display = $username ?? "Guest";
$display = $username ?? "Guest";
echo "Welcome, $display<br>";

// Chained null coalescing
$config_val = null;
$env_val = null;
$default = "system_default";
$result = $config_val ?? $env_val ?? $default;
$env_val = null;
$default = "system_default";
$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
echo "<br><b>Custom Example:</b><br>";
$user_theme_preference = null;
$system_dark_mode = null;
$fallback_theme = "light_mode";

// It checks preference first, then system, then falls back to default
$active_theme = $user_theme_preference ?? $system_dark_mode ?? $fallback_theme;
echo "Active Theme: $active_theme<br>";
102 changes: 58 additions & 44 deletions starter/lab3_task2.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,60 +7,74 @@
* 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 [wayne naum]
* @student [ENE212-0060/2023]
* @lab Lab 3 of 14
* @unit ICS 2371
* @date [Date completed]
*/

// ── 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
echo "<h3>Task 2: Grade Classification System</h3>";

// ── 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
// ==========================================
// 1. INPUTS (Currently set to Test Set A)
// ==========================================
$cat1 = 0;
$cat2 = 0;
$cat3 = 0;
$cat4 = 0;
$exam = 15;

// ── 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"
// ==========================================
// 2. SEQUENCE: Calculations
// ==========================================
$total = $cat1 + $cat2 + $cat3 + $cat4 + $exam;
$cats_attended = 0;

// ── Supplementary Rule (implement using ternary) ──────────────────────────
// If grade is D: "Eligible for Supplementary Exam"
// Otherwise: "Not eligible for supplementary"
if ($cat1 > 0) { $cats_attended++; }
if ($cat2 > 0) { $cats_attended++; }
if ($cat3 > 0) { $cats_attended++; }
if ($cat4 > 0) { $cats_attended++; }

// ── STEP 1: Compute total ─────────────────────────────────────────────────
// TODO: compute $total
echo "<strong>Scores:</strong> CATs Attended: $cats_attended/4 | Exam: $exam | Total Score: $total/100<br><br>";

// ==========================================
// 3. SELECTION: Eligibility & Grading
// ==========================================
if ($cats_attended >= 3 && $exam >= 20) {

// --- Grade Classification (if-elseif-else) ---
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";
}

// ── STEP 2: Count CATs attended ───────────────────────────────────────────
// TODO: compute $cats_attended (each CAT > 0 counts as attended)
echo "<strong>Status:</strong> Grade $grade ($desc)<br>";

// --- Supplementary Check (Ternary Operator) ---
$supp_status = ($grade === "D") ? "Eligible for Supplementary Exam" : "Not eligible for supplementary";
echo "<strong>Supplementary:</strong> $supp_status<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
} else {
// --- Disqualified Branch ---
echo "<strong>Status:</strong> <span style='color:red;'>DISQUALIFIED — Exam conditions not met</span><br>";
}
?>
110 changes: 65 additions & 45 deletions starter/lab3_task3.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,59 +3,79 @@
* 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 [wayne naum ]
* @student [ENE 212-0060/2023]
* @lab Lab 3 of 14
* @unit ICS 2371
* @date [Date completed]
*/

// ══════════════════════════════════════════════════════════════
// ==========================================
// EXERCISE A — Day of Week Classifier
// ══════════════════════════════════════════════════════════════
// Given $day (integer 1–7, where 1=Monday):
// Use switch-case to print the day name.
// Group Saturday and Sunday under "Weekend".
// All weekdays print their name and "— Lecture day".
// Remember: break is NOT optional. Missing break = fall-through.
// ==========================================
echo "<h3>Exercise A — Day of Week Classifier</h3>";

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

// TODO: 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 number. Please enter 1-7.<br>";
break;
}

echo "<hr>";

// ══════════════════════════════════════════════════════════════
// ==========================================
// EXERCISE B — HTTP Status Code Explainer
// ══════════════════════════════════════════════════════════════
// Given $status_code, use switch-case to explain it:
// 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"

$status_code = 404;

// TODO: switch-case for HTTP status


// ══════════════════════════════════════════════════════════════
// 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.

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


// ══════════════════════════════════════════════════════════════
// 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?
// ==========================================
echo "<h3>Exercise B</h3>";

$status_code = 404;

switch ($status_code) {
case 200: echo "200: OK — The request succeeded.<br>"; break;
case 301: echo "301: Moved Permanently — The URL has been changed.<br>"; break;
case 400: echo "400: Bad Request — The server could not understand the request.<br>"; break;
case 401: echo "401: Unauthorized — Authentication is required.<br>"; break;
case 403: echo "403: Forbidden — You do not have permission to access this.<br>"; break;
case 404: echo "404: Not Found — The requested resource could not be found.<br>"; break;
case 500: echo "500: Internal Server Error — The server encountered a problem.<br>"; break;
default: echo "$status_code: Unknown Status Code.<br>"; break;
}

echo "<hr>";

// ==========================================
// EXERCISE C — PHP 8 match Rewrite
// ==========================================
echo "<h3>Exercise C</h3>";

$status_code_match = 404;

// The match expression assigns the result directly to the variable
$explanation = match ($status_code_match) {
200 => "200: OK — The request succeeded.<br>",
301 => "301: Moved Permanently — The URL has been changed.<br>",
400 => "400: Bad Request — The server could not understand the request.<br>",
401 => "401: Unauthorized — Authentication is required.<br>",
403 => "403: Forbidden — You do not have permission to access this.<br>",
404 => "404: Not Found — The requested resource could not be found.<br>",
500 => "500: Internal Server Error — The server encountered a problem.<br>",
default => "$status_code_match: Unknown Status Code.<br>",
};

echo $explanation;

echo "<hr>";

// The single closing tag for the whole file!
?>
Loading
Loading