Student Copy Slips With Program Solution
Student Copy Slips With Program Solution
<?php
class demo
{
public $a,$b;
function show()
{
//echo "hi";
}
function sample()
{
}
}
$obj= new demo();
$obj->show();
print_r(get_class_methods("demo"));
print_r(get_declared_classes());
print_r(get_class_vars("demo"));
?>
Slip 3: Write a calculator class that can accept two values, then add, subtract,
multiply them or divide them on request.
For example:
$calc = new Calculator( 3,
4 ); echo $calc- >add(); II
Displays "7"
echo $calc- >multiply(); II Displays "12"
Calform.html
<!doctype html>
<html>
<head>
</head>
<body>
<form action="calculator.php" method=get>
Enter 1st value :
<input type="text" name=a> <br><br><br>
Enter 2nd value :
<input type="text" name=b> <br><br><br>
<input type = "submit">
</form>
</body>
</html>
Calculator.php
<?php
class Calculate
{
public $a;
public $b;
function __construct($a,$b)
{
$this->a=$a;
$this->b=$b;
}
public function add()
{
$c=$this->a+$this->b;
echo"Addition = $c<br>";
}
public function subtract()
{
$c=$this->a-$this->b;
echo"Subtract = $c<br>";
}
public function multiply()
{
$c=$this->a*$this->b;
echo"Multiplication = $c<br>";
}
public function div()
{
$c=$this->a/$this->b;
echo"Division = $c";
}
}
$x=$_GET['a'];
$y=$_GET['b'];
$calc=new Calculate($x,$y);
$calc->add();
$calc->subtract();
$calc->multiply();
$calc->div();
?>
Slip 4: Define a class Employee having private members - id, name, department,
salary. Define parameterized constructors. Create a subclass called "Manager"
with private member bonus. Create 3 objects of the Manager class and display the
details of the manager having the maximum total salary (salary+ bonus).
<?php
class Employee
{
private $eid,$ename,$edept,$sal;
function __construct($a,$b,$c,$d)
{
$this->eid=$a;
$this->ename=$b;
$this->edept=$c;
$this->sal=$d;
}
public function getdata()
{
return $this->sal;
}
public function display()
{
echo "employee id : ".$this->eid."</br>";
echo "employee name : ".$this->ename."</br>";
echo "employee department : ".$this->edept."</br>";
}
}
{
parent::display();
echo "maximum salary is : ".self::$total1;
}
}
$ob=new Manager(0,"ABC","",0,0);
$ob1=new Manager(1,"Akansha","hr",28000,2000);
$ob=$ob1->max($ob);
$ob2=new Manager(2,"Ramesh","Acct",30000,2500);
$ob=$ob2->max($ob);
$ob3=new Manager(3,"Riya","Tester",32000,1000);
$ob=$ob3->max($ob);
$ob->display();
?>
Slip5:
Create an abstract class Shape with methods area( ) and volume( ). Derive
three classes rectangle (length, breath), Circle(radius) and Cylinder(radius,
height), Calculate area and volume of all. (Use Method overriding).
<?php
define('pi',3.14);
define('four_thirds',4/3);
Slip6: Write a PHP script, which will return the following component of the
URL (Using response header)
http://www.college.com/Science/CS.php
List of Components: scheme,
host,
path
Expected output:
Scheme: http
Host: www.college.com
Path: /Science/CS.php
<?php
header("Content-type:text/plain");
$url = 'http://www.college.com/Science/CS.php';
$url=parse_url($url);
echo 'Scheme : '.$url['scheme']."\n";
echo 'Host : '.$url['host']."\n";
echo 'Path : '.$url['path']."\n";
//var_dump($http_response_header);
?>
$c=new Convert(4);
$c->kgtogm();
?>
Slip 8: Write a PHP Script to create class Shape and its sub-class Triangle,
Square, Circle and display area of selected shape (use concept of inheritance).
<?php
define('pi',3.14);
define('four_thirds',4/3);
define('one_twos',1/2);
class Shape
{
public $x = 0;
public $y = 0;
}
Slip9: Write a PHP program to create a Class Calculator which will accept
two values from user and pass as an argument through parameterized
constructor and do the following task:
a)Add
b)Subtract
c) Multiply
them together or divide them on request.
Calform.html
<!doctype html>
<html>
<head>
</head>
<body>
<form action="calculator.php" method=get>
Enter 1st value :
<input type="text" name=a> <br><br><br>
Enter 2nd value :
<input type="text" name=b> <br><br><br>
<input type = "submit">
</form>
</body>
</html>
Calculator.php
<?php
class Calculate
{
public $a;
public $b;
function __construct($a,$b)
{
$this->a=$a;
$this->b=$b;
}
public function add()
{
$c=$this->a+$this->b;
echo"Addition = $c<br>";
}
public function subtract()
{
$c=$this->a-$this->b;
echo"Subtract = $c<br>";
}
public function multiply()
{
$c=$this->a*$this->b;
echo"Multiplication = $c<br>";
}
public function div()
{
$c=$this->a/$this->b;
echo"Division = $c";
}
}
$x=$_GET['a'];
$y=$_GET['b'];
$calc=new Calculate($x,$y);
$calc->add();
$calc->subtract();
$calc->multiply();
$calc->div();
?>
function sample()
{
}
}
$obj= new demo();
$obj->show();
print_r(get_class_methods("demo"));
print_r(get_declared_classes());
print_r(get_class_vars("demo"));
?>
Slip 11 : Write a PHP program to create class circle having radius data
member and two member functions find_circumference() and find_area().
Display area and circumference depending on user's preference.
<html>
<head>
<title>PHP Program To find Area and Circumference of a Circle</title>
</head>
<body>
<form method="post">
<table border="0">
<tr>
<td><input type="text" name="num1" value="" placeholder="Enter the radius of a
circle"/></td>
</tr>
<tr>
<td> <input type="submit" name="submit" value="Submit"/></td>
</tr>
</table>
</form>
<?php
if(isset($_POST['submit']))
{
$r = $_POST['num1'];
$pi = 3.14;
$area = $pi * $r * $r;
echo "Area of a Circle is: ".$area;
echo "<br>";
$cir = 2*$pi*$r;
echo "Circumference of a circle is: " .$cir;
return 0;
}
?>
</body>
</html>
<?php
$fahr=$_GET[ 'fahrenheit' ];
if(! is_null($fahr))
{
$celsicus=($fahr-32)*5/9;
printf("%.2ff is %.2fc", $fahr, $celsicus);
}
?>
</body>
</html>
<html>
<body>
<form method="POST" action="nextslip13.php">
Employee ID. :<input type="text" name="sno" value="<?php
if(isset($_POST['sno']))echo $_POST['sno'];?>"><br><br>
Nextslip13.php
<?php
if(isset($_POST['submit']))
{
$sno=$_POST['sno'];
$sname=$_POST['sname'];
$spos=$_POST['spos'];
echo "Employee id is:$sno<br>";
echo "Employee name is:$sname<br>";
echo "Employee position is:$spos<br>";
}
?>
Slip14 : Write a PHP script to accept a string from user and then display the
accepted string in reverse order. (use concept of self processing form)
<html>
<body>
<form method="POST" action="<?php echo $_SERVER['PHP_SELF'] ?>">
Enter String : <input type="text" name="str1"><br><br>
<input type="submit" name="submit">
</form>
<?php
if(isset($_POST['submit']))
{
$str=$_POST['str1'];
$nstr=strrev($str);
echo"The reverse string is : ".$nstr;
}
?>
</body>
</html>
Slip15 : Write PHP program to select list of subjects from list box and displays
the selected subject information on next page. (Use sticky Multivalued
parameter).
<html>
<body>
<form action="multicheckslip15.php" method="post">
Subjects:<select name="tech[]" size=6 multiple>
<option value="php">PHP</option>
<option value="asp">ASP</option>
<option value="jsp">JSP</option>
<option value="c++"> C++</option>
<option value="angularjs">ANGULAR JS</option>
<option value="dotnet"> DOT NET</option>
</select>
<br>
<input type="submit" value="Display">
</form>
</body>
</html>
Multicheckslip15.php
<?php
$tech = $_POST['tech'];
if(is_array($tech)) {
foreach($tech as $v) echo $v."<br>";
}
else echo $tech."<br>";
?>
Slip16 : Write a PHP program to accept two string from user and check
whether entered strings are matching or not.(use sticky form concept)
<html>
<head>
</head>
<body>
<form method="POST" action="slip16.php">
<b>Enter String 1:
</b><input type="text" name="str1" value="<?php if(isset($_POST['str1'])) echo
$_POST['str1'];?>" /><br><br>
<b>Enter String 2 :
</b><input type="text" name="str2" value="<?php if(isset($_POST['str2'])) echo
$_POST['str2'];?>" /><br><br>
<input type="submit" name="submit" value="Submit">
</form>
<?php
if(isset($_POST['submit']))
{
$s1 = $_POST['str1'];
$s2 = $_POST['str2'];
if(strcmp($s1,$s2) == 0)
{
echo "Matching...";
}
else {
echo "String not matching...";
}
}
?>
</body>
</html>
Slip17 : Write a PHP Script to display Server information in table format (Use
$_SERVER).
<?php
echo"<table border=1>";
foreach($_SERVER as $k=>$v)
{
echo"<tr><td>".$k."</td><td>".$v."</td></tr>";
}
echo"</table>";
?>
Slip18 : Write a PHP program to create a simple distance calculator that
can accept distance in meters from user. Convert it into centimeter or
kilometer according to user preference.
(use radio buttons and Self Processing form)
<html>
<head>
<title>Distance Conversion</title>
<body>
<?php
if($_SERVER['REQUEST_METHOD']=='GET')
{
?>
<form action="<?php echo$_SERVER['PHP_SELF']?>" method="POST">
Enter Distance in meter :<input type=text name=t1><br><br>
<input type="radio" name="r1" value="1">Convert into Centimeter<br><br>
<input type="radio" name="r1" value="2">Convert into Kilometer<br><br>
<input type="submit" value="Convert"><br>
</form>
<?php
}
else if($_SERVER['REQUEST_METHOD']=='POST')
{
$dist=$_POST['t1'];
$op=$_POST['r1'];
switch($op)
{
case 1:
echo "Conversion from meter to centimeter is :";
$cm=$dist*100;
echo "$cm cm";
break;
case 2:
echo "Conversion from meter to Kilometer is :";
$km=$dist/1000;
echo "$km km";
break;
}
}
?>
</body>
</html>
Slip20 : Write a PHP script for the following: Design a form to accept a
number from the user. Perform the operations and show the results.
l) Fibonacci Series.
2) To find sum of the digits of that number.
(Use the concept of self processing page.)
<?php
extract($_REQUEST);
if(isset($check))
{
$num = 0;
$n1 = 0;
$n2 = 1;
echo "<h3>Fibonacci series for first $n numbers: </h3>";
echo "\n";
echo $n1.' '.$n2.' ';
while ($num < $n-2 )
{
$n3 = $n2 + $n1;
echo $n3.' ';
$n1 = $n2;
$n2 = $n3;
$num = $num + 1;
}
}
?>
<!DOCTYPE html>
<html>
<head>
<title>Form</title>
</head>
<body>
<form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
Enter Your Number :
<input type="text" name="n" required>
</form>
</body>
</html>
Slip30 : Derive a class Rectangle from class Square. Create one more class
Triangle. Create an interface with only one method called cal_area ().
Implement this interface in all the classes. Include appropriate data members
and constructors in all classes. Write a program to accept details of a Rectangle,
Square and Triangle and display the area.
<?php
$a ;
$z;
$r;
$s;
$w;
$l;
$res;
interface i1
{
public function cal_area();
}