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
130 changes: 90 additions & 40 deletions starter/lab3_task1.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,68 +3,118 @@
* 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 [Amoke Ken Odhiambo]
* @student [ENE212-0220/2023]
* @lab Lab 3 of 14
* @unit ICS 2371
* @date [Date completed]
* @date [4rth April 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

// TODO: Exercise A — your code here
/* Exercise A — Temperature Alert System
*/

// Change value to test 36.8, 39.2, and 34.5
$temperature = 39.2;

// 1. Check for Normal range (inclusive: 36.1 to 37.5)
if ($temperature >= 36.1 && $temperature <= 37.5) {
echo "Normal";
}

// 2. Check for Fever
if ($temperature > 37.5) {
echo "Fever";
}

// 3. Check for Hypothermia Warning
if ($temperature < 36.1) {
echo "Hypothermia Warning";
}


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

// TODO: Exercise B — your code here
$number = 47;

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

// ══════════════════════════════════════════════════════════════
// EXERCISE C — Comparison Chain
// ══════════════════════════════════════════════════════════════
// Run this code EXACTLY as written.
// Record all six outputs in your report and explain each result.
// 2. Divisibility by 3
if ($number % 3 == 0) {
echo "$number is divisible by 3<br>";
}else {
echo "$number is not divisible by 3<br>";
}

$x = 10; $y = "10"; $z = 10.0;
// 3. Divisibility by 5
if ($number % 5 == 0) {
echo "$number is divisible by 5<br>";
}else {
echo "$number is not divisible by 5<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?
// 4. Divisibility by both
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<br>";
}

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


// ══════════════════════════════════════════════════════════════
// EXERCISE D — Null & Default Values
// ══════════════════════════════════════════════════════════════
// Run this code as written, then extend it as instructed below.
// ==========================================
// Exercise C — Comparison Chain
// ==========================================
$x = 10; // Integer
$y = "10"; // String
$z = 10.0; // Float (Decimal)

echo "<h3>Exercise C Results:</h3>";
echo "<pre>"; // <pre> makes var_dump output easier to read in a browser

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

echo "</pre>";


// ==========================================
// Exercise D — Null & Default Values
// ==========================================
echo "<h3>Exercise D Results:</h3>";

// Part 1: Basic Null Coalescing
$username = null;
$display = $username ?? "Guest";
$display = $username ?? "Guest";
echo "Welcome, $display<br>";

// Chained null coalescing
// Part 2: 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.
// Part 3: My Custom Chained Example
// This checks for a user-set theme, then a session theme, then defaults to 'Light'
$user_theme = null;
$session_theme = "Dark Mode";
$fallback_theme = "Light Mode";

$final_theme = $user_theme ?? $session_theme ?? $fallback_theme;

echo "Selected Theme: $final_theme<br>";

?>

101 changes: 57 additions & 44 deletions starter/lab3_task2.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,60 +7,73 @@
* 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 [Amoke Ken Odhiambo]
* @student [ENE212-0220/2023]
* @lab Lab 3 of 14
* @unit ICS 2371
* @date [Date completed]
* @date [4rth April 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
/* Task 2 — Grade Classification System */

// ── 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"
// TEST DATA SETS
// Set A: 8, 7, 9, 6, 52
// Set B: 9, 8, 0, 9, 55
// Set C: 0, 0, 7, 0, 48
// Set D: 5, 4, 6, 3, 22
// Set E: 0, 0, 0, 0, 15
$cat1 = 0; $cat2 = 0; $cat3 = 0; $cat4 = 0; $exam = 15;

// ── Supplementary Rule (implement using ternary) ──────────────────────────
// If grade is D: "Eligible for Supplementary Exam"
// Otherwise: "Not eligible for supplementary"
// 1. Calculate Attendance
$cats_attended = 0;
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
$total = $cat1 + $cat2 + $cat3 + $cat4 + $exam;
$output_message = "";

// 2. Eligibility Rule (Nested IF)
if ($cats_attended >= 3 && $exam >= 20) {

// 3. Grading Rules (if-elseif-else, highest first)
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)
// 4. Supplementary Rule (Ternary)
$supp_msg = ($grade == "D") ? "Eligible for Supplementary Exam" : "Not eligible for supplementary";

$output_message = "Total Score: $total<br>Grade: $grade ($desc)<br>Status: $supp_msg";

// ── STEP 3: Eligibility check (nested if) ─────────────────────────────────
// TODO: nested if — eligibility → grade classification → supplementary ternary
} else {
// Failure to meet conditions
$output_message = "<strong>DISQUALIFIED — Exam conditions not met</strong><br>";
$output_message .= "Reason: Attended $cats_attended CATs and Exam score was $exam.";
}


// ── 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
// 5. Final Output (SESE Principle)
echo "<h2>Student Result Card</h2>";
echo $output_message;
?>
Loading
Loading