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
36 changes: 33 additions & 3 deletions starter/lab3_task1.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@
* 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 [Tim Kiplimo]
* @student [ENE212-0063/2021]
* @lab Lab 3 of 14
* @unit ICS 2371
* @date [Date completed]
* @date [3/04/2026]
*/

// ══════════════════════════════════════════════════════════════
Expand All @@ -21,6 +21,19 @@
// Test with: 36.8, 39.2, 34.5 — screenshot each

// TODO: Exercise A — your code here
// --- Exercise A: Temperature Alert System ---
echo "<h3>Exercise A</h3>";
$temperature = 34.5; // Change this value to 36.8 and 34.5 for your screenshots

if ($temperature >= 36.1 && $temperature <= 37.5) {
echo "Normal<br>";
}
if ($temperature > 37.5) {
echo "Fever<br>";
}
if ($temperature < 36.1) {
echo "Hypothermia Warning<br>";
}


// ══════════════════════════════════════════════════════════════
Expand All @@ -31,7 +44,18 @@
// Also check divisibility by 3, by 5, and by both 3 and 5 — one line each

// TODO: Exercise B — your code here
echo "<h3>Exercise B</h3>";
$number = 47;

if ($number % 2 == 0) {
echo "$number is EVEN<br>";
} else {
echo "$number is ODD<br>";
}
// Additional 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<br>" : "$number is NOT divisible by both<br>";

// ══════════════════════════════════════════════════════════════
// EXERCISE C — Comparison Chain
Expand Down Expand Up @@ -68,3 +92,9 @@
echo "Config: $result<br>";

// TODO: Add one more chained ?? example of your own and explain it in your report.
$user_color = null;
$session_color = null;
$theme_default = "dark-mode";
$final_theme = $user_color ?? $session_color ?? $theme_default;
echo "Active Theme: $final_theme<br>";
?>
86 changes: 83 additions & 3 deletions starter/lab3_task2.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,11 @@
* 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 [Tim Kiplimo]
* @student [ENE212-0063/2021]
* @lab Lab 3 of 14
* @unit ICS 2371
* @date [Date completed]
* @date [3/04/2026]
*/

// ── Test Data Set A (change values to run other test sets) ─────────────────
Expand Down Expand Up @@ -64,3 +64,83 @@
// 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

<?php
/**
* Lab 3 - Task 2: Student Grading & Eligibility System
* Course: Structural Programming
*/

// --- TEST DATA SETS ---
// Uncomment the set you want to test (Shift + /)
// Set A: Expected Grade B
//$cat1 = 8; $cat2 = 7; $cat3 = 9; $cat4 = 6; $exam = 52;

// Set B: Expected Grade A (Check attendance)
// $cat1 = 9; $cat2 = 8; $cat3 = 0; $cat4 = 9; $exam = 55;

// Set C: Expected DISQUALIFIED
//$cat1 = 0; $cat2 = 0; $cat3 = 7; $cat4 = 0; $exam = 48;

// Set D: Expected Grade D + Supp Eligible
// $cat1 = 5; $cat2 = 4; $cat3 = 6; $cat4 = 3; $exam = 22;

// Set E: Expected DISQUALIFIED
$cat1 = 0; $cat2 = 0; $cat3 = 0; $cat4 = 0; $exam = 15;


// 1. Calculate Total Score
$total = $cat1 + $cat2 + $cat3 + $cat4 + $exam;

// 2. Determine CAT Attendance (Score > 0 counts as attended)
$cats_array = [$cat1, $cat2, $cat3, $cat4];
$cats_attended = 0;
foreach ($cats_array as $score) {
if ($score > 0) {
$cats_attended++;
}
}

echo "<h2>Student Results</h2>";
echo "Total Score: $total / 100<br>";
echo "CATs Attended: $cats_attended<br><br>";

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

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

// 5. Supplementary Rule (TERNARY)
$supp_status = ($grade === "D") ? "Eligible for Supplementary Exam" : "Not eligible for supplementary";

// Output Results
echo "<strong>GRADE: $grade ($desc)</strong><br>";
echo "Status: $supp_status<br>";

} else {
// Failure of Eligibility Gate
echo "<strong style='color:red;'>DISQUALIFIED — Exam conditions not met</strong><br>";
}
?>
49 changes: 45 additions & 4 deletions starter/lab3_task3.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@
* 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 [Tim Kiplimo]
* @student [ENE212-0063/2021]
* @lab Lab 3 of 14
* @unit ICS 2371
* @date [Date completed]
* @date [3/04/2026]
*/

// ══════════════════════════════════════════════════════════════
Expand All @@ -18,6 +18,20 @@
// Group Saturday and Sunday under "Weekend".
// All weekdays print their name and "— Lecture day".
// Remember: break is NOT optional. Missing break = fall-through.
// --- Exercise A: Day of Week Classifier ---
echo "<h3>Exercise A: Day Classifier</h3>";
$day = 3; // Test with 1-7

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

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

Expand All @@ -40,6 +54,20 @@
$status_code = 404;

// TODO: switch-case for HTTP status
// --- Exercise B: HTTP Status Code (Switch) ---
echo "<h3>Exercise B: HTTP Switch</h3>";
$status_code = "200";

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


// ══════════════════════════════════════════════════════════════
Expand All @@ -50,7 +78,20 @@
// Observe the difference in syntax and behaviour.

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

// --- Exercise C: PHP 8 Match Rewrite ---
echo "<h3>Exercise C: HTTP Match Expression</h3>";
// Note: Match returns a value, so we assign it or echo it directly
$message = 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 "$message<br>";

// ══════════════════════════════════════════════════════════════
// EXERCISE D — Rewrite comparison
Expand Down
63 changes: 60 additions & 3 deletions starter/lab3_task4.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@
* IMPORTANT: You must complete pseudocode AND flowchart in your PDF
* report BEFORE writing any code below.
*
* @author [Your Full Name]
* @student [Your Reg Number, e.g. SCT212-XXXX/2024]
* @author [Tim Kiplimo]
* @student [ENE212-0063/2021]
* @lab Lab 3 of 14
* @unit ICS 2371
* @date [Date completed]
* @date [3/04/2026]
*/

// ── Problem: Student Loan Eligibility System ───────────────────────────────
Expand Down Expand Up @@ -57,3 +57,60 @@
// Set C: enrolled=false, gpa=3.5, income=60000, previous=true → Not enrolled
// Set D: enrolled=true, gpa=2.5, income=600000, previous=true → Income fail
// Set E: enrolled=true, gpa=2.0, income=50000, previous=true → Full | Renewal

// --- TEST DATA SETS ---
// Set A: Expected "Partial 75% | New"
//$enrolled = true; $gpa = 3.1; $income = 180000; $prev_loan = false;

// Set B: Expected "GPA fail"
//$enrolled = true; $gpa = 1.8; $income = 80000; $prev_loan = false;

// Set C: Expected "Not enrolled"
//$enrolled = false; $gpa = 3.5; $income = 60000; $prev_loan = true;

// Set D: Expected "Income fail"
// $enrolled = true; $gpa = 2.5; $income = 600000; $prev_loan = true;

// Set E: Expected "Full | Renewal"
$enrolled = true; $gpa = 2.0; $income = 50000; $prev_loan = true;


echo "<h2>HELB Loan Eligibility Results</h2>";

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

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

// INNER CHECK 2 — Household Income
if ($income < 100000) {
$loan_status = "Full loan";
$eligible = true;
} elseif ($income < 250000) {
$loan_status = "Partial 75%";
$eligible = true;
} elseif ($income < 500000) {
$loan_status = "Partial 50%";
$eligible = true;
} else {
$loan_status = "Not eligible — High income";
$eligible = false;
}

// TERNARY — Renewal vs New (Only if eligible)
if ($eligible) {
$app_type = ($prev_loan) ? "Renewal application" : "New application";
echo "<strong>Status: $loan_status | $app_type</strong>";
} else {
echo "<span style='color:orange;'>$loan_status</span>";
}

} else {
echo "<span style='color:red;'>Not eligible — GPA below minimum (GPA fail)</span>";
}

} else {
echo "<span style='color:red;'>Not eligible — must be an active student (Not enrolled)</span>";
}
?>