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
123 changes: 123 additions & 0 deletions lab3_task1.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
<?php
/**
* ICS 2371 — Lab 3: Control Structures I
* Task 1: Temperature Alert 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 [Kevin Mutua]
* @student [ENE212-0078/2023]
* @lab Lab 3 of 14
* @unit ICS 2371
* @date [1 April 2026]
*/
// ══════════════════════════════════════════════════════════════
// 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
// ══════════════════════════════════════════════════════════════

$number = 47;

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

// 2. Divisibility by 3
if ($number % 3 == 0) {
echo "$number is divisible by 3<br>";
}else {
echo "$number is not divisible by 3<br>";
}

// 3. Divisibility by 5
if ($number % 5 == 0) {
echo "$number is divisible by 5<br>";
}else {
echo "$number is not divisible by 5<br>";
}

// 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>";
}



// ==========================================
// 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";
echo "Welcome, $display<br>";

// Part 2: Chained Null Coalescing
$config_val = null;
$env_val = null;
$default = "system_default";
$result = $config_val ?? $env_val ?? $default;
echo "Config: $result<br>";

// 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>";

?>

78 changes: 78 additions & 0 deletions lab3_task2.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
<?php
/**
* 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 [Kevin Mutua]
* @student [ENE212-0078/2023]
* @lab Lab 3 of 14
* @unit ICS 2371
* @date [1 April 2026]
*/

/* Task 2 — Grade Classification System */

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

// 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++;

$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";
}

// 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";

} 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.";
}

// 5. Final Output (SESE Principle)
echo "<h2>Student Result Card</h2>";
echo $output_message;
?>
84 changes: 84 additions & 0 deletions lab3_task3.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
<?php
/**
* ICS 2371 — Lab 3: Control Structures I
* Task 3: switch-case and match Expression [6 marks]
*
* @author [Kevin Mutua]
* @student [ENE212-0078/2023]
* @lab Lab 3 of 14
* @unit ICS 2371
* @date [1 April 2026]
*/

// ==========================================
// Exercise A — Day of Week Classifier
// ==========================================
$day = 3; // Change this to test 1-7

echo "<h3>Exercise A: Day Classifier</h3>";

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<br>";
}

echo "<hr>";

// ==========================================
// Exercise B — HTTP Status Code Explainer
// ==========================================
$status_code = 404;

echo "<h3>Exercise B: HTTP Explainer (Switch)</h3>";

switch ($status_code) {
case 200: echo "200: OK"; break;
case 301: echo "301: Moved Permanently"; break;
case 400: echo "400: Bad Request"; break;
case 401: echo "401: Unauthorized"; break;
case 403: echo "403: Forbidden"; break;
case 404: echo "404: Not Found"; break;
case 500: echo "500: Internal Server Error"; break;
default: echo "Unknown Status Code"; break;
}

echo "<hr>";

// ==========================================
// Exercise C — PHP 8 match Rewrite
// ==========================================
echo "<h3>Exercise C: HTTP Explainer (Match)</h3>";

// match is an expression, so we can assign its result to a variable
$explanation = match ($status_code) {
200 => "200: OK",
301 => "301: Moved Permanently",
400 => "400: Bad Request",
401 => "401: Unauthorized",
403 => "403: Forbidden",
404 => "404: Not Found",
500 => "500: Internal Server Error",
default => "Unknown Status Code",
};

echo $explanation;
?>
63 changes: 63 additions & 0 deletions lab3_task4.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
<?php
/**
* ICS 2371 — Lab 3: Control Structures I
* Task 4: Nested Conditions — Loan Eligibility Checker [6 marks]
*
* IMPORTANT: You must complete pseudocode AND flowchart in your PDF
* report BEFORE writing any code below.
*
* @author [Kevin Mutua]
* @student [ENE212-0078/2023]
* @lab Lab 3 of 14
* @unit ICS 2371
* @date [1 April 2026]
*/

/* Task 4 — HELB Loan Eligibility Checker */

// TEST DATA SETS (Update these to test A, B, C, D, and E)
$enrolled = true;
$gpa = 2.0;
$annual_income = 50000;
$previous_loan = true;

$final_status = "";

// 1. OUTER CHECK — Enrollment
if ($enrolled === true) {

// 2. INNER CHECK 1 — GPA
if ($gpa >= 2.0) {

// 3. INNER CHECK 2 — Income
if ($annual_income < 100000) {
$loan_amount = "Full loan";
} elseif ($annual_income < 250000) {
$loan_amount = "Partial 75%";
} elseif ($annual_income < 500000) {
$loan_amount = "Partial 50%";
} else {
$loan_amount = "Not eligible — Income above limit";
}

// 4. TERNARY — Renewal vs New (only if eligible for some loan)
// We check if the word "Not" is in the loan_amount string
if (strpos($loan_amount, "Not") === false) {
$app_type = ($previous_loan) ? "Renewal application" : "New application";
$final_status = "$loan_amount | $app_type";
} else {
$final_status = $loan_amount;
}

} else {
$final_status = "Not eligible — GPA below minimum";
}

} else {
$final_status = "Not eligible — must be an active student";
}

// 5. Output (SESE Principle)
echo "<h2>HELB Eligibility Result</h2>";
echo "Status: " . $final_status;
?>
Loading