Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 19

Name:Vivekkumar Yadav ROLL NO:: B 79

Class::TYBSc( Computer Science) Batch::F

Assignment 4:Object Oriented Programming


----------------------------------------------------------------------------------------------------------------------------------
Set A
Q: 1) 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()
----------------------------------------------------------------------------------------------------------------------------------
<?php
Interface Shape
{
function area();
function volume();
}

class cylinder implements shape


{
var $r,$h,$area,$volume;
const PI=3.14;
function cylinder($r,$h)
{
$this->r=$r;
$this->h=$h;
}
function area()
{
$this->area=2*(cylinder::PI)*$this->r*($this->r+$this->h);
echo"\nArea of cylinder is :".$this->area;
}
function volume()
{
$this->volume=(cylinder::PI)*$this->r*$this->r*$this->h;
echo"\nVolume of cylinder is : ".$this->volume;
}
}
$obj=new cylinder(3,4);
$obj->area();
$obj->volume();
?>
/
****************************************OUTPUT**************************************************
[Akshay@localhost Ass4]$ php ex4sa1.php

Area of cylinder is :131.88


Volume of cylinder is : 113.04

/
Name:Vivekkumar Yadav ROLL NO:: B 79
Class::TYBSc( Computer Science) Batch::F

Assignment 4:Object Oriented Programming


----------------------------------------------------------------------------------------------------------------------------------
SetB
Q:1) Derive a class square from class Rectangle. Create one more class circle.
Create an interface with only one method called 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 square,
circle and rectangle and display the area
----------------------------------------------------------------------------------------------------------------------------------
<?php
interface Shape
{
function area();
}
class Rectangle implements Shape
{
var $l,$b,$area;
function Rectangle($l,$b)
{
$this->l=$l;
$this->b=$b;
}
function area()
{
$this->area=$this->l*$this->b;
echo"\n Area of Rectangle is : $this->area";
}
}
class Square extends Rectangle implements Shape
{
var $s;

function Square($s)
{
$this->s=$s;
}
function area()
{
$this->area=$this->s*$this->s;
echo"\n Area of square is : $this->area";
}
}
class Circle implements Shape
{
var $r,$area;
const PI=3.14;
function Circle($r)
{
$this->r=$r;
}
function area()
{
$this->area=(Circle::PI)*$this->r*$this->r;
echo"\nArea of Circle is : $this->area";
}
}

$obj1=new Rectangle(5,4);
$obj1->area();
$obj2=new Square(4);
$obj2->area();
$obj3=new Circle(5);
$obj3->area();

?>

/
*********************************OUTPUT******************************
[Akshay@localhost Ass4]$ php ex4sb1.php

Area of Rectangle is : 20
Area of square is : 16
Area of Circle is : 78.5
/
Name:Vivekkumar Yadav ROLL NO:: B 79
Class::TYBSc( Computer Science) Batch::F

Assignment 4:Object Oriented Programming


----------------------------------------------------------------------------------------------------------------------------------
SETB
Q:2) Create a class account(accno,cust_name). Derive two classes from account as
saving_acc(balance, min_amount) and current_acc(balance,min_amount). Display a menu
a) Saving Account
b) Current Account
For each of this display a menu with the following options.
1. Create account
2. Deposit
3. Withdrawal
----------------------------------------------------------------------------------------------------------------------------------
<?php
class account
{
var $ano,$cname;
function account($an,$name)
{
$this->ano=$an;
$this->cname=$name;
}
}

class saving_account extends account


{
var $balance,$min_amount;

function saving_account($ano,$cname,$balance,$min_amount)
{
$this->account($ano,$cname);
$this->balance=$balance;
$this->min_amount=$min_amount;
}

function display()
{
echo"\n Account No : $this->ano";
echo"\n Account Type : Saving Account ";
echo"\n Customer Name : $this->cname";
echo"\n Account Balance : $this->balance";
}

function getbalance()
{
return $this->balance;
}

function withdraw($amount)
{
$bal=$this->getbalance();
$bal=$bal-$amount;

if($bal < $this->min_amount)


{
echo"Transaction could not be processed due to insufficient balance ";
}
else
{
$this->balance-=$amount;

echo"\n Your account has been debited with Rs $amount\n";


}
}

function deposit($amount)
{
$this->balance +=$amount;
}

}
class current_account extends account
{
var $balance,$min_amount;

function current_account($ano,$cname,$balance,$min_amount)
{
$this->account($ano,$cname);
$this->balance=$balance;
$this->min_amount=$min_amount;
}

function display()
{
echo"\n Account No : $this->ano";
echo"\n Account Type : Current Account ";
echo"\n Customer Name : $this->cname";
echo"\n Account Balance : $this->balance";
}

function getbalance()
{
return $this->balance;
}

function withdraw($amount)
{
$bal=$this->getbalance();
$bal=$bal-$amount;

if($bal < $this->min_amount)


{
echo"Transaction could not be processed due to insufficient balance ";
}
else
{
$this->balance -=$amount;

echo"\n Your account has been debited with Rs $amount\n";


}
}
function deposit($amount)
{
$this->balance +=$amount;
}
}

echo"\n Type (S) for Saving Account or (C) for Current account : ";
$account_type=fgets(STDIN);

if($account_type=="S\n" || $account_type=="s\n")
{
$class_name="saving_account";
}
else
{
$class_name="current_account";
}

do
{
echo"\n************************************MENU for $class_name ************************\
n";
echo"\n1.Create Account\n";
echo"\n2.Deposit Amount into Account\n";
echo"\n3.Withdrawl Amount from Account\n";
echo"\n4.Exit\n";
echo"\nEnter your choice: ";
$ch=fgets(STDIN);

switch($ch)
{
case 1 :
echo"\n Enter Account No of Customer : ";
$ano=fgets(STDIN);
echo"\n Enter Name of Customer : ";
$cname=fgets(STDIN);
echo"\n Enter Balance : ";
$balance=fgets(STDIN);
$s_acc=new $class_name($ano,$cname,$balance,500);
echo"\n--------------------------------------------------------------------------------\n";
$s_acc->display();
echo"\n--------------------------------------------------------------------------------\n";
break;

case 2 :
echo"\n Enter Deposit amount : ";
$amt=fgets(STDIN);
$s_acc->deposit($amt);
echo"\n You deposited RS $amt in your Account \n";
echo"\n Current Balance of Account is ". $s_acc->getbalance() ;
break;

case 3 :
echo"\n Enter Withdrawl amount : ";
$amt=fgets(STDIN);
$s_acc->withdraw($amt);
// echo"\n You have withdrawl Rs $amt from your account \n";
echo"\n Current Balance of Account is ".$s_acc->getbalance();

break;

case 4 : die("\n You Exited .............\n");

break;

}
}
while($ch!=4);
?>

/*
*********************************OUTPUT********************************
[Akshay@localhost Ass4]$ php ex4sb2.php

Type (S) for Saving Account or (C) for Current account : s

************************************MENU for saving_account ************************

1.Create Account

2.Deposit Amount into Account

3.Withdrawl Amount from Account

4.Exit

Enter your choice: 1

Enter Account No of Customer : 1546458

Enter Name of Customer : Akshay

Enter Balance : 50000

--------------------------------------------------------------------------------

Account No : 1546458

Account Type : Saving Account


Customer Name : Akshay

Account Balance : 50000

--------------------------------------------------------------------------------

************************************MENU for saving_account ************************

1.Create Account

2.Deposit Amount into Account

3.Withdrawl Amount from Account


4.Exit

Enter your choice: 2

Enter Deposit amount : 25000

You deposited RS 25000


in your Account

Current Balance of Account is 75000


************************************MENU for saving_account ************************

1.Create Account

2.Deposit Amount into Account

3.Withdrawl Amount from Account

4.Exit

Enter your choice: 3

Enter Withdrawl amount : 15000

Your account has been debited with Rs 15000

Current Balance of Account is 60000


************************************MENU for saving_account ************************

1.Create Account

2.Deposit Amount into Account

3.Withdrawl Amount from Account

4.Exit

Enter your choice: 4

You Exited .............


[Akshay@localhost Ass4]$ php ex4sb2.php

Type (S) for Saving Account or (C) for Current account : c

************************************MENU for current_account ************************

1.Create Account

2.Deposit Amount into Account

3.Withdrawl Amount from Account

4.Exit

Enter your choice: 1


Enter Account No of Customer : 5464465

Enter Name of Customer : Tanaji

Enter Balance : 5000

--------------------------------------------------------------------------------

Account No : 5464465

Account Type : Current Account


Customer Name : Tanaji

Account Balance : 5000

--------------------------------------------------------------------------------

************************************MENU for current_account ************************

1.Create Account

2.Deposit Amount into Account

3.Withdrawl Amount from Account

4.Exit

Enter your choice: 3

Enter Withdrawl amount : 5000


Transaction could not be processed due to insufficient balance
Current Balance of Account is 5000

************************************MENU for current_account ************************

1.Create Account

2.Deposit Amount into Account

3.Withdrawl Amount from Account

4.Exit

Enter your choice: 2

Enter Deposit amount : 25000

You deposited RS 25000


in your Account

Current Balance of Account is 30000


************************************MENU for current_account ************************

1.Create Account

2.Deposit Amount into Account


3.Withdrawl Amount from Account

4.Exit

Enter your choice: 3

Enter Withdrawl amount : 10000

Your account has been debited with Rs 10000

Current Balance of Account is 20000


************************************MENU for current_account ************************

1.Create Account

2.Deposit Amount into Account

3.Withdrawl Amount from Account

4.Exit

Enter your choice: 4

You Exited .............


[Akshay@localhost Ass4]$
*/
Name:Vivekkumar Yadav ROLL NO:: B 79
Class::TYBSc( Computer Science) Batch::F

Assignment 4:Object Oriented Programming


----------------------------------------------------------------------------------------------------------------------------------
SetC
Q:1) Define an interface for stack operation. Implement this interface in a class
----------------------------------------------------------------------------------------------------------------------------------
<?php
interface stack_operations
{
function push($ele);
function pop();
//function display();
}

class Stack implements stack_operations


{
var $stk;
function Stack()
{
$this->stk=array();
}
function push($ele)
{
$e=explode(',',$ele);
for($i=0;$i<count($e);$i++)
array_push($this->stk,$e[$i]);
}
function pop()
{

$ele=array_pop($this->stk);
return $ele;
}
function display()
{
echo"\nStack is :";
/*for($i=count($this->stk)-1;$i>=0;$i--)
echo"\n".$this->stk[$i];*/
print_r($this->stk);
}
}
do
{
echo"\n***************MENU*********************\n";
echo"\n1.Insert an element into stack \n";
echo"\n2.Delete an element from stack \n";
echo"\n3.Display the contents of stack\n";
echo"\n4.Exit\n";
echo"\nEnter your choice : ";
$ch=fgets(STDIN);
$obj=new Stack();

switch($ch)
{
case 1 :echo"\nEnter the element into stack : ";
$ele=fgets(STDIN);
$e=substr($ele,0,strlen($ele)-1);
// $e=explode(',',$ele);

$obj->push($e);
break;
case 2 : $ele=$obj->pop();
echo"\n The deleted element from a stack is $ele";
break;
case 3 :$obj->display();

break;
case 4 : die("\nYou exited..........");
break;

}
while($ch!=4);

?>

/*
********************************OUTPUT***********************************
[Akshay@localhost Ass4]$ php ex4sc1.php
/*
SetC1
*/

***************MENU*********************

1.Insert an element into stack

2.Delete an element from stack

3.Display the contents of stack

4.Exit

Enter your choice : 1

Enter the element into stack : 10

***************MENU*********************

1.Insert an element into stack

2.Delete an element from stack

3.Display the contents of stack

4.Exit

Enter your choice : 1

Enter the element into stack : 20


***************MENU*********************

1.Insert an element into stack

2.Delete an element from stack

3.Display the contents of stack

4.Exit

Enter your choice : 1

Enter the element into stack : 30

***************MENU*********************

1.Insert an element into stack

2.Delete an element from stack

3.Display the contents of stack

4.Exit
Enter your choice : 1

Enter the element into stack : 40

***************MENU*********************

1.Insert an element into stack

2.Delete an element from stack

3.Display the contents of stack

4.Exit

Enter your choice : 3

Stack is :Array
(
[0] => 10
[1] => 20
[2] => 30
[3] => 40
)

***************MENU*********************

1.Insert an element into stack

2.Delete an element from stack


3.Display the contents of stack

4.Exit

Enter your choice : 2

The deleted element from a stack is 40


***************MENU*********************

1.Insert an element into stack

2.Delete an element from stack

3.Display the contents of stack

4.Exit

Enter your choice : 3

Stack is :Array
(
[0] => 10
[1] => 20
[2] => 30
)

***************MENU*********************

1.Insert an element into stack

2.Delete an element from stack

3.Display the contents of stack

4.Exit

Enter your choice : 4

You exited.........
Name:Vivekkumar Yadav ROLL NO:: B 79
Class::TYBSc( Computer Science) Batch::F

Assignment 4:Object Oriented Programming


----------------------------------------------------------------------------------------------------------------------------------
Set C
Q:2) Write necessary class and member function definitions for a cricket player object. The
program should accept details from user (max :10) (player_code, name, runs, innings_played,
no_of_times_out). The program should contain following menu.
a) Enter details of players.
b) Display average runs of a single player.
c) Average runs of all players.
d) Display the list of players in sorted order as per runs(use function overloading)
----------------------------------------------------------------------------------------------------------------------------------
<?php
class player
{
var $pcode=array(),$innings=array(),$pname=array(),$runs=array(),$notout=array();
function player($c,$i,$n,$r,$no)
{
$this->pcode=explode(" ",$c);
$this->innings=explode(" ",$i);
$this->pname=explode(" ",$n);
$this->runs=explode(" ",$r);
$this->notout=explode(" ",$no);
}

function display()
{
for($i=0;$i<count($this->pcode);$i++)
{
echo"\nPlayer Name :". $this->pname[$i] ;
echo"\nPlayer Code :". $this->pcode[$i] ;
echo"\nInnings Played :". $this->innings[$i] ;
echo"\nTotal Runs :". $this->runs[$i] ;
echo"\nNumber of Times Not Out :". $this->notout[$i] ;
}
}

function sort_runs()
{
array_multisort($this->runs,$this->pcode,$this->innings,$this->pname,$this->notout);
$this->display();
}

function __call($method,$arguments)
{
if($method=="average")
{
if(count($arguments)==0)
{
$avg=array();
for($i=0;$i<count($this->pname);$i++)
{
$avg[$i]=$this->runs[$i]/$this->innings[$i];
}
return($avg);
}
if(count($arguments)==1)
{
for($i=0;$i<count($this->pname);$i++)
{
if($this->pcode[$i]==$arguments[0])
{
return($this->runs[$i]/$this->innings[$i]);
}
}
}
}
}
}

do
{
echo"\n********************************MENU************************************\n";
echo"\n1.Accept Details of Players \n";
echo"\n2.Display Average Runs of a Single Player \n";
echo"\n3.Display Average Runs of All Players \n";
echo"\n4.Display List Of players in Sorted Order \n ";
echo"\n5.Exit\n";
echo"\n Enter your choice : ";
$ch=fgets(STDIN);

switch($ch)
{
case 1 :
echo"\n Enter Player Code : ";
$c1=fgets(STDIN);
$c=substr($c1,0,strlen($c1)-1);

echo"\n Enter Player Name : ";


$c1=fgets(STDIN);
$name=substr($c1,0,strlen($c1)-1);

echo"\n Enter Runs of a Player : ";


$c1=fgets(STDIN);
$runs=substr($c1,0,strlen($c1)-1);

echo"\n Innings Played : ";


$c1=fgets(STDIN);
$inn=substr($c1,0,strlen($c1)-1);

echo"\n No of times Not Out : ";


$c1=fgets(STDIN);
$notout=substr($c1,0,strlen($c1)-1);

$obj=new player($c,$inn,$name,$runs,$notout);
echo"\n+++++++++++++++++++++++++++++Player Details are++++++++++++
+++++++++++++ \n";
$obj->display();
break;

case 2 :
echo"\nEnter Player Code : ";
$c1=fgets(STDIN);
$c=substr($c1,0,strlen($c1)-1);
echo"\n Average Runs of player of Player Code $c1 is " .$obj->average($c);
break;

case 3 :
echo"\n Average Runs of All players are \n";
$b=$obj->average();
print_r($b);

break;

case 4 :
echo"\nPlayer Details in sorted order is :\n";
$obj->sort_runs();
echo"\n----------------------------------------------------------------------\n";

break;
case 5 :die("\n You Exited-------------\n");
}
}while($ch!=5);

?>

OUTPUT-

Akshay@localhost Ass4]$ php ex4sc2.php

********************************MENU************************************

1.Accept Details of Players

2.Display Average Runs of a Single Player

3.Display Average Runs of All Players

4.Display List Of players in Sorted Order

5.Exit

Enter your choice : 1

Enter Player Code : 101

Enter Player Name : vivek

Enter Runs of a Player : 1200

Innings Played : 21

No of times Not Out : 8

+++++++++++++++++++++++++++++Player Details are+++++++++++++++++++++++++

Player Name :vivek


Player Code :101
Innings Played :21
Total Runs :1200
Number of Times Not Out :8
********************************MENU************************************

1.Accept Details of Players

2.Display Average Runs of a Single Player

3.Display Average Runs of All Players

4.Display List Of players in Sorted Order

5.Exit

Enter your choice : 1

Enter Player Code : 102

Enter Player Name : akshay

Enter Runs of a Player : 2509

Innings Played : 28

No of times Not Out : 22

+++++++++++++++++++++++++++++Player Details are+++++++++++++++++++++++++

Player Name :akshay


Player Code :102
Innings Played :28
Total Runs :2509
Number of Times Not Out :22
********************************MENU************************************

1.Accept Details of Players

2.Display Average Runs of a Single Player

3.Display Average Runs of All Players

4.Display List Of players in Sorted Order

5.Exit

Enter your choice : 3

Average Runs of All players are


Array
(
[0] => 89.607142857143
)

********************************MENU************************************

1.Accept Details of Players


2.Display Average Runs of a Single Player

3.Display Average Runs of All Players

4.Display List Of players in Sorted Order

5.Exit

Enter your choice : 2

Enter Player Code : 102

Average Runs of player of Player Code 102


is 89.607142857143
********************************MENU************************************

1.Accept Details of Players

2.Display Average Runs of a Single Player

3.Display Average Runs of All Players

4.Display List Of players in Sorted Order

5.Exit

Enter your choice : 4

Player Details in sorted order is :

Player Name :akshay


Player Code :102
Innings Played :28
Total Runs :2509
Number of Times Not Out :22
----------------------------------------------------------------------
Player Name :vivek
Player Code :101
Innings Played :21
Total Runs :1200
Number of Times Not Out :8

********************************MENU************************************

1.Accept Details of Players

2.Display Average Runs of a Single Player

3.Display Average Runs of All Players

4.Display List Of players in Sorted Order

5.Exit

Enter your choice : 5


You Exited-------------

You might also like