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
175 changes: 105 additions & 70 deletions starter/lab3_task1.php
Original file line number Diff line number Diff line change
@@ -1,70 +1,105 @@
<?php
/**
* 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]
* @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

// TODO: Exercise A — your code here


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


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


// ══════════════════════════════════════════════════════════════
// EXERCISE D — Null & Default Values
// ══════════════════════════════════════════════════════════════
// Run this code as written, then extend it as instructed below.

$username = null;
$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;
echo "Config: $result<br>";

// TODO: Add one more chained ?? example of your own and explain it in your report.
<?php
/**
* ICS 2371 — Lab 3: Control Structures I
* Task 1: Simple if and if-else — Warm-Up Exercises [5 marks]
*
* @author Johnray Mwendwa
* @student ENE212-0070/2022
* @lab Lab 3 of 14
* @unit ICS 2371
* @date 06/04/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
$temperature = 39.2;
echo "Test temperature: $temperature \n";

if ($temperature >= 36.1 && $temperature <= 37.5) {
echo "Result:Normal \n";
}
if ($temperature > 37.5) {
echo "Result:Fever \n";
}
if ($temperature < 36.1) {
echo "Result:Hypothermia Warning \n";
}
//line break
echo "\n";


// ══════════════════════════════════════════════════════════════
// 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;
if ($number % 2 == 0) {
echo "$number is EVEN";
} else {
echo "$number is ODD";
}

//fizzbuzz type of problem
if ($number % 3 == 0) {
echo "$number is divisible by 3";
}
if ($number % 5 == 0) {
echo "$number is divisible by 5";
}
if ($number % 3 == 0 && $number % 5 == 0) {
echo "$number is divisible by both 3 and 5";
}


// ══════════════════════════════════════════════════════════════
// 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: boolean true — because == does type juggling and considers 10 and "10" equal
var_dump($x === $y); // B: boolean false — because === checks for both value and type, and 10 (integer) is not the same type as "10" (string)
var_dump($x == $z); // C: boolean true — because == does type juggling and considers 10 and 10.0 equal (both are numeric and have the same value)
var_dump($x === $z); // D: boolean false — because === checks for both value and type, and 10 (integer) is not the same type as 10.0 (float)
var_dump($y === $z); // E: boolean false — because === checks for both value and type, and "10" (string) is not the same type as 10.0 (float)
var_dump($x <=> $y); // F: int(0) — because the spaceship operator <=> returns 0 when the two operands are equal in value (after type juggling)

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

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


$config_val = null;
$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_val = null;
$result2 = $custom_val ?? $env_val ?? $default;
echo "Custom: $result2<br>";

175 changes: 109 additions & 66 deletions starter/lab3_task2.php
Original file line number Diff line number Diff line change
@@ -1,66 +1,109 @@
<?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 [Your Full Name]
* @student [Your Reg Number, e.g. SCT212-XXXX/2024]
* @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

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

// ── STEP 1: Compute total ─────────────────────────────────────────────────
// TODO: compute $total


// ── STEP 2: Count CATs attended ───────────────────────────────────────────
// TODO: compute $cats_attended (each CAT > 0 counts as attended)


// ── 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
<?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 Johnray Mwendwa
* @student ENE212-0070/2022
* @lab Lab 3 of 14
* @unit ICS 2371
* @date 06/04/2026
*/

// ── Test Data Set A (change values to run other test sets) ─────────────────
$name = "Johnray Mwendwa";
$cat1 = 0; // out of 10
$cat2 = 0; // out of 10
$cat3 = 0; // out of 10
$cat4 = 0; // out of 10
$exam = 15; // 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"

// ── 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
if ($cats_attended >= 3 && $exam >= 20) {
// eligible — now determine grade
if ($total >= 70) {
$grade = "A (Distinction)";
} elseif ($total >= 65) {
$grade = "B+ (Credit Upper)";
} elseif ($total >= 60) {
$grade = "B (Credit Lower)";
} elseif ($total >= 55) {
$grade = "C+ (Pass Upper)";
} elseif ($total >= 50) {
$grade = "C (Pass Lower)";
} elseif ($total >= 40) {
$grade = "D (Marginal Pass)";
} else {
$grade = "E (Fail)";
}
} else {
// not eligible
$grade = "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
echo "<h2>Report Card</h2>";
echo "<p>Student Name: $name</p>";
echo "<p>CAT 1: $cat1</p>";
echo "<p>CAT 2: $cat2</p>";
echo "<p>CAT 3: $cat3</p>";
echo "<p>CAT 4: $cat4</p>";
echo "<p>Exam: $exam</p>";
echo "<p>Total: $total</p>";
echo "<p>CATs Attended: $cats_attended</p>";
echo "<p>Eligibility Status: " . ($cats_attended >= 3 && $exam >= 20 ? "Eligible" : "DISQUALIFIED") . "</p>";
echo "<p>Grade: $grade</p>";
echo "<p>Remark: " . ($grade == "E (Fail)" ? "Needs Improvement" : "Well Done") . "</p>";
echo "<p>Supplementary Status: " . ($grade == "D (Marginal Pass)" ? "Eligible for Supplementary Exam" : "Not eligible for supplementary") . "</p>";


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