Sybba (Ca) Lab Book Solution
Sybba (Ca) Lab Book Solution
Set A:
1) Write class declarations and member function definitions for an
employee(code, name, designation). Derive emp_account(account_no,
joining_date) from employee and emp_sal(basic_pay, earnings, deduction) from
emp_account. Write a menu drivenprogram
a) To build a mastertable
b) To sort allentries
c) To search anentry
d) Display salary
Solutiojn-:
<?php
class Employee {
public $code;
public $name;
public $designation;
function __construct($code, $name, $designation) {
$this->code = $code;
$this->name = $name;
$this->designation = $designation;
}
}
class EmpAccount extends Employee {
public $account_no;
public $joining_date;
function __construct($code, $name, $designation, $account_no,
$joining_date) {
parent::__construct($code, $name, $designation);
$this->account_no = $account_no;
$this->joining_date = $joining_date;
}
}
class EmpSal extends EmpAccount {
public $basic_pay;
public $earnings;
public $deduction;
function __construct($code, $name, $designation, $account_no,
$joining_date, $basic_pay, $earnings, $deduction) {
parent::__construct($code, $name, $designation, $account_no,
$joining_date);
$this->basic_pay = $basic_pay;
$this->earnings = $earnings;
$this->deduction = $deduction;
}
function getSalary() {
return $this->basic_pay + $this->earnings - $this->deduction;
}
}
$employeeRecords = [];
function buildMasterTable() {
global $employeeRecords;
$emp = new EmpSal("E001", "John Doe", "Software Developer",
"ACC001", "2020-01-01", 50000, 10000, 5000);
array_push($employeeRecords, $emp);
$emp = new EmpSal("E002", "Jane Smith", "HR Manager", "ACC002",
"2018-03-15", 60000, 15000, 4000);
array_push($employeeRecords, $emp);
echo "Master table has been created with sample entries.\n";
}
function sortEntries() {
global $employeeRecords;
usort($employeeRecords, function($a, $b) {
return strcmp($a->code, $b->code); // Sorting by employee code
});
echo "Employee records sorted by code.\n";
}
function searchEntry($code) {
global $employeeRecords;
foreach ($employeeRecords as $emp) {
if ($emp->code == $code) {
echo "Employee found: \n";
echo "Code: " . $emp->code . "\n";
echo "Name: " . $emp->name . "\n";
echo "Designation: " . $emp->designation . "\n";
echo "Account No: " . $emp->account_no . "\n";
echo "Joining Date: " . $emp->joining_date . "\n";
echo "Salary: " . $emp->getSalary() . "\n";
return;
}
}
echo "Employee with code $code not found.\n";
}
function displaySalary($code) {
global $employeeRecords;
foreach ($employeeRecords as $emp) {
if ($emp->code == $code) {
echo "Salary details for employee $code: \n";
echo "Basic Pay: " . $emp->basic_pay . "\n";
echo "Earnings: " . $emp->earnings . "\n";
echo "Deductions: " . $emp->deduction . "\n";
echo "Net Salary: " . $emp->getSalary() . "\n";
return;
}
}
echo "Employee with code $code not found.\n";
}
function menu() {
do {
echo "\n--- Employee Management System ---\n";
echo "1. Build Master Table\n";
echo "2. Sort All Entries\n";
echo "3. Search an Entry\n";
echo "4. Display Salary\n";
echo "5. Exit\n";
echo "Enter your choice: ";
$choice = trim(fgets(STDIN));
switch ($choice) {
case 1:
buildMasterTable();
break;
case 2:
sortEntries();
break;
case 3:
echo "Enter Employee Code to search: ";
$code = trim(fgets(STDIN));
searchEntry($code);
break;
case 4:
echo "Enter Employee Code to display salary: ";
$code = trim(fgets(STDIN));
displaySalary($code);
break;
case 5:
echo "Exiting...\n";
break;
default:
echo "Invalid choice. Please try again.\n";
break;
}
} while ($choice != 5);
}
// Run the menu function
menu();
?>
OUTPUT-:
2) Define an interface which has methods area( ), volume( ). Define constant PI.
Create a class cylinder which implements this interface and calculate area and
volume. (Hint: Use define())
Solution-:
<?php
interface Shape {
const PI = 3.14159;
public function area();
public function volume();
}
class Cylinder implements Shape {
private $radius;
private $height;
public function __construct($radius, $height) {
$this->radius = $radius;
$this->height = $height;
}
public function area() {
return 2 * Shape::PI * $this->radius * ($this->radius + $this->height);
}
public function volume() {
return Shape::PI * pow($this->radius, 2) * $this->height;
}
}
$cylinder = new Cylinder(5, 10);
echo "Surface Area of Cylinder: " . $cylinder->area() . "\n";
echo "Volume of Cylinder: " . $cylinder->volume() . "\n";
?>
OUTPUT-:
Surface Area of Cylinder: 471.2385
Volume of Cylinder: 785.398
3) Write a Calculator class that can accept two values, then add them, subtract
them, multiply them together, or divide them one request.
Solution-:
<?php
class Calculator {
private $num1;
private $num2;
// Example Usage
// Perform addition
echo "Addition: " . $calc->add() . "\n";
// Perform subtraction
echo "Subtraction: " . $calc->subtract() . "\n";
// Perform multiplication
echo "Multiplication: " . $calc->multiply() . "\n";
// Perform division
echo "Division: " . $calc->divide() . "\n";
?>
OUTPUT-:
Addition: 15
Subtraction: 5
Multiplication: 50
Division: 2
Division by Zero: Error: Division by zero is not allowed.
Set B:
1) Create a class named DISTANCE with feet and inches as data
members. The classhas the following member functions:
convert_feet_to_inch() , convert_inch_to_feet() .
Display options using radio button and display conversion on nextpage.
Solution-:
<?php
// Define the DISTANCE class with feet and inches as data members
class DISTANCE {
public $feet;
public $inches;
$result = "";
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Distance Converter</title>
</head>
<body>
<h1>Distance Converter</h1>
<!-- Form to accept feet and inches -->
<form method="POST" action="distance.php">
<label for="feet">Feet:</label>
<input type="number" name="feet" id="feet" value="<?php echo isset($feet) ? $feet : '';
?>" step="any">
<br><br>
<label for="inches">Inches:</label>
<input type="number" name="inches" id="inches" value="<?php echo isset($inches) ?
$inches : ''; ?>" step="any">
<br><br>
class Employee {
// Data members of the class
public $Emp_Name;
public $Dept_name;
public $Basic_sal;
public $DA;
public $HRA;
public $TA;
public $IT;
public $PF;
public $PT;
public $GROSS;
public $DEDUCTION;
public $NET;
?>
OUTPUT-:
Pay Slip of John Doe
Department: Software Development
Basic Salary: Rs. 50000.00
Dearness Allowance (DA): Rs. 5000.00
House Rent Allowance (HRA): Rs. 7500.00
Travel Allowance (TA): Rs. 2500.00
Gross Salary: Rs. 65000.00
------------------------------
Income Tax (IT): Rs. 5000.00
Provident Fund (PF): Rs. 6000.00
Professional Tax (PT): Rs. 200.00
Total Deductions: Rs. 11200.00
------------------------------
Net Salary: Rs. 53800.00
Set C:
1) Write a PHP program to create a class article having articleid, name,
articleqty, price.
Write menu driven program to perform following functions :( Use array ofobjects)
i) Display details of all articlespurchased.
ii) Display details of articles whose price exceeds500
iii) Display details of articles whose quantity exceeds50
Solution-:
<?php
// Class Article Definition
class Article {
public $articleid;
public $name;
public $articleqty;
public $price;
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-
scale=1.0">
<title>Article Management System</title>
</head>
<body>
<h1>Article Management System</h1>
<form method="POST">
<label for="choice">Select an option:</label><br><br>
<input type="radio" name="choice" value="1" id="option1">
Display all articles purchased<br>
<input type="radio" name="choice" value="2" id="option2">
Display articles with price exceeding Rs. 500<br>
<input type="radio" name="choice" value="3" id="option3">
Display articles with quantity exceeding 50<br><br>
<input type="submit" value="Submit">
</form>
</body>
</html>
2) Write a PHP program to create a class Worker that has data members
as Worker_Name, No_of_Days_worked, Pay_Rate. Create and initialize the
object using default constructor, Parameterized constructor. Also write
necessary member function to calculate and display the salary ofworker.
Solution-:
<?php
class Worker {
// Data members
public $Worker_Name;
public $No_of_Days_worked;
public $Pay_Rate;
// Default constructor
public function __construct() {
$this->Worker_Name = "Unknown Worker";
$this->No_of_Days_worked = 0;
$this->Pay_Rate = 0;
}
// Parameterized constructor
public function __construct($name, $days_worked, $pay_rate) {
$this->Worker_Name = $name;
$this->No_of_Days_worked = $days_worked;
$this->Pay_Rate = $pay_rate;
}
?>
OUTPUT-:
Worker Name: John Doe
Number of Days Worked: 20
Pay Rate: $100 per day
Salary of John Doe is: $2000
Set A:
1) Write PHP program accept name, select your cities you would like to
visit and display selected information on page. (Use multi-valuedparameter),.
Solution-:
<?php
if ($_SERVER["REQUEST_METHOD"] == "POST") {
// Get user input
$name = $_POST['name'];
$cities = isset($_POST['cities']) ? $_POST['cities'] : [];
if (!empty($cities)) {
echo "<ul>";
foreach ($cities as $city) {
echo "<li>" . htmlspecialchars($city) . "</li>";
}
echo "</ul>";
} else {
echo "<p>No cities selected.</p>";
}
} else {
// Display the form
echo '<h2>Select Cities You Would Like to Visit</h2>';
echo '<form method="POST" action="">';
echo 'Name: <input type="text" name="name" required><br><br>';
if ($_SERVER["REQUEST_METHOD"] == "POST") {
// Validate and sanitize input data
if (empty($_POST["name"])) {
$nameErr = "Name is required";
} else {
$name = htmlspecialchars($_POST["name"]);
}
if (empty($_POST["email"])) {
$emailErr = "Email is required";
} else {
$email = htmlspecialchars($_POST["email"]);
}
if (empty($_POST["dob"])) {
$dobErr = "Date of Birth is required";
} else {
$dob = htmlspecialchars($_POST["dob"]);
}
if (empty($_POST["gender"])) {
$genderErr = "Gender is required";
} else {
$gender = $_POST["gender"];
}
if (empty($_POST["course"])) {
$courseErr = "Course selection is required";
} else {
$course = $_POST["course"];
}
if (empty($_POST["address"])) {
$addressErr = "Address is required";
} else {
$address = htmlspecialchars($_POST["address"]);
}
}
// Course dropdown
echo 'Course: <select name="course">';
echo '<option value="">Select a course</option>';
echo '<option value="Computer Science" ' . ($course == 'Computer Science'
? 'selected' : '') . '>Computer Science</option>';
echo '<option value="Mechanical Engineering" ' . ($course == 'Mechanical
Engineering' ? 'selected' : '') . '>Mechanical Engineering</option>';
echo '<option value="Electrical Engineering" ' . ($course == 'Electrical
Engineering' ? 'selected' : '') . '>Electrical Engineering</option>';
echo '</select>';
echo '<span style="color: red;">' . $courseErr . '</span><br><br>';
// Submit button
echo '<input type="submit" value="Submit">';
echo '</form>';
}
?>
Set B:
1) Write a PHP Script to display Server information in table format
(Use$_SERVER).
Solution-:
<?php
// Start HTML output
echo "<h2>Server Information</h2>";
echo "<table border='1' cellpadding='10' cellspacing='0'>";
echo "<tr><th>Property</th><th>Value</th></tr>";
// Server Name
echo "<tr><td><strong>Server Name</strong></td><td>" .
$_SERVER['SERVER_NAME'] . "</td></tr>";
// Server Software
echo "<tr><td><strong>Server Software</strong></td><td>" .
$_SERVER['SERVER_SOFTWARE'] . "</td></tr>";
// Server Protocol
echo "<tr><td><strong>Server Protocol</strong></td><td>" .
$_SERVER['SERVER_PROTOCOL'] . "</td></tr>";
// Document Root
echo "<tr><td><strong>Document Root</strong></td><td>" .
$_SERVER['DOCUMENT_ROOT'] . "</td></tr>";
// Request URI
echo "<tr><td><strong>Request URI</strong></td><td>" .
$_SERVER['REQUEST_URI'] . "</td></tr>";
echo "</table>";
?>
2) Write a PHP program to accept two strings from user and check
whether entered strings are matching or not. (Use sticky formconcept).
Solution-:
<?php
// Declare variables to store form input values
$string1 = $string2 = "";
$error = $message = "";
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>String Matching Form</title>
</head>
<body>
</body>
</html>
OUTPUT-:
Enter Two Strings to Check If They Match
[ String 1: __________ ]
[ String 2: __________ ]
[ Check Strings ]
The strings match!
Assignment 3 – XML
Set A:
1) Write a PHP script to create XML file named“Course.xml”
<Course>
<SYBBA CA>
<Studentname> ....... </Studentname>
<Classname> ...... </Class name>
<percentage> .... </percentage>
</SYBBA CA>
</Course>
Store the details of 5 students who are in SYBBACA.
Solution-:
<?php
// Loop through each student and add their details to the XML
foreach ($students as $student) {
$studentNode = $sybba->addChild('Student');
$studentNode->addChild('Studentname', $student["name"]);
$studentNode->addChild('Classname', $student["classname"]);
$studentNode->addChild('Percentage', $student["percentage"]);
}
<CATALOG>
<CD>
<TITLE>Empire Burlesque</TITLE> <ARTIST>Bob Dylan</ARTIST>
<COUNTRY>USA</COUNTRY>
<COMPANY>Columbia</COMPANY>
<PRICE>10.90</PRICE>
<YEAR>1985</YEAR>
</CD>
</CATALOG>
Save the file with name “CD.xml”.
Solution-:
<?php
// Create a new XML document with the declaration and root element
<CATALOG>
$xml = new SimpleXMLElement('<?xml version="1.0" encoding="ISO-
8859-1" ?><CATALOG></CATALOG>');
OUTPUT-:
<CATALOG>
<CD>
<TITLE>Empire Burlesque</TITLE>
<ARTIST>Bob Dylan</ARTIST>
<COUNTRY>USA</COUNTRY>
<COMPANY>Columbia</COMPANY>
<PRICE>10.90</PRICE>
<YEAR>1985</YEAR>
</CD>
</CATALOG>
Set B:
1) Write a script to create “cricket.xml” file with multiple elements as
shownbelow: <CricketTeam>
<Team country=”India”>
<player> </player>
<runs> </runs>
<wicket> </wicket>
</Team>
</CricketTeam>
Write a script to add multiple elements in “cricket.xml” file of category,
country=”Australia”.
Solution-:
<?php
$indiaTeam2 = $xml->addChild('Team');
$indiaTeam2->addAttribute('country', 'India');
$indiaTeam2->addChild('player', 'Rohit Sharma');
$indiaTeam2->addChild('runs', '1400');
$indiaTeam2->addChild('wicket', '15');
$australiaTeam2 = $xml->addChild('Team');
$australiaTeam2->addAttribute('country', 'Australia');
$australiaTeam2->addChild('player', 'David Warner');
$australiaTeam2->addChild('runs', '1800');
$australiaTeam2->addChild('wicket', '20');
$australiaTeam3 = $xml->addChild('Team');
$australiaTeam3->addAttribute('country', 'Australia');
$australiaTeam3->addChild('player', 'Pat Cummins');
$australiaTeam3->addChild('runs', '1200');
$australiaTeam3->addChild('wicket', '40');
?>
OUTPUT-:
<?xml version="1.0" encoding="UTF-8"?>
<CricketTeam>
<Team country="India">
<player>Virat Kohli</player>
<runs>1500</runs>
<wicket>10</wicket>
</Team>
<Team country="India">
<player>Rohit Sharma</player>
<runs>1400</runs>
<wicket>15</wicket>
</Team>
<Team country="Australia">
<player>Steve Smith</player>
<runs>2000</runs>
<wicket>25</wicket>
</Team>
<Team country="Australia">
<player>David Warner</player>
<runs>1800</runs>
<wicket>20</wicket>
</Team>
<Team country="Australia">
<player>Pat Cummins</player>
<runs>1200</runs>
<wicket>40</wicket>
</Team>
</CricketTeam>
2) Write a script to create “breakfast.xml” file with multiple elements as
shownbelow:
<breakfast_menu>
<food>
<name>French Fries</name>
<price>Rs45</price>
<description>Young youths are very much intrested to eat it </description>
<calories>650</calories>
</food>
</breakfast_menu>
Write a script to add multiple elements in “breakfast.xml” file of category,
Juice.
Solution-:
<?php
$juice2 = $xml->addChild('food');
$juice2->addChild('name', 'Apple Juice');
$juice2->addChild('price', 'Rs50');
$juice2->addChild('description', 'Sweet and fresh apple juice.');
$juice2->addChild('calories', '150');
$juice3 = $xml->addChild('food');
$juice3->addChild('name', 'Carrot Juice');
$juice3->addChild('price', 'Rs60');
$juice3->addChild('description', 'Healthy carrot juice for your health.');
$juice3->addChild('calories', '100');
?>
OUTPUT-:
<?xml version="1.0" encoding="UTF-8"?>
<breakfast_menu>
<food>
<name>French Fries</name>
<price>Rs45</price>
<description>Young youths are very much interested to eat
it</description>
<calories>650</calories>
</food>
<food>
<name>Orange Juice</name>
<price>Rs40</price>
<description>Freshly squeezed orange juice.</description>
<calories>120</calories>
</food>
<food>
<name>Apple Juice</name>
<price>Rs50</price>
<description>Sweet and fresh apple juice.</description>
<calories>150</calories>
</food>
<food>
<name>Carrot Juice</name>
<price>Rs60</price>
<description>Healthy carrot juice for your health.</description>
<calories>100</calories>
</food>
</breakfast_menu>
Assignment 4: PHP with AJAX
Set A:
1)Write a PHP script using AJAX concept, to check user name and password are valid
or Invalid (use database to store user name andpassword).
Solution-:
<?php
// Database connection settings
$host = 'localhost';
$dbname = 'your_database_name';
$username = 'your_database_user';
$password = 'your_database_password';
if ($user) {
// User exists, now check the password (if hashed, use password_verify)
if ($user['password'] === $inputPassword) {
echo 'Login successful';
} else {
echo 'Invalid password';
}
} else {
echo 'Username not found';
}
} else {
echo 'Please provide both username and password';
}
?>
if (password_verify($inputPassword, $user['password'])) {
echo 'Login successful';
} else {
echo 'Invalid password';
}
2) Write Ajax program to get book details from XML file when user select a book
name. Create XML file for storing details of book(title, author, year,price).
Solution-:
<?xml version="1.0" encoding="UTF-8"?>
<books>
<book>
<title>Book One</title>
<author>Author One</author>
<year>2021</year>
<price>20.99</price>
</book>
<book>
<title>Book Two</title>
<author>Author Two</author>
<year>2019</year>
<price>15.50</price>
</book>
<book>
<title>Book Three</title>
<author>Author Three</author>
<year>2020</year>
<price>18.75</price>
</book>
<!-- Add more books here as needed -->
</books>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Book Details</title>
<script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
<script>
// Function to fetch book details from the XML file
function getBookDetails() {
var selectedBook = $('#bookSelect').val(); // Get selected book title
if (selectedBook) {
$.ajax({
url: 'books.xml', // Path to the XML file
type: 'GET',
dataType: 'xml',
success: function(response) {
// Loop through the XML to find the selected book
$(response).find('book').each(function() {
var title = $(this).find('title').text();
if (title === selectedBook) {
var author = $(this).find('author').text();
var year = $(this).find('year').text();
var price = $(this).find('price').text();
<h2>Select a Book</h2>
</body>
</html>