0% found this document useful (0 votes)
21 views15 pages

Java Script Oops Notes

Uploaded by

priyashruti846
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
21 views15 pages

Java Script Oops Notes

Uploaded by

priyashruti846
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Java script oops

# → private
_ → protected
Q.1 :- create a class having following constructor and method .
a) The default constructor should accept employee id , name , and basic salary .
b) A method called calculate to increase the salary by 10% .
c) A method called output to display employee id , name , old salary , and new salary .
<script>
class employee
{
constructor()
{
[Link]=[Link]("enter a id")
[Link]=[Link]("enter a name")
[Link]=parseInt([Link]("enter the salary"))
}
calculate()
{
[Link]=[Link]+[Link]*.10
}
output()
{
[Link]("employee id="+[Link]+"<br>")
[Link]("employee name="+[Link]+"<br>")
[Link]("old salary="+[Link]+"<br>")
[Link]("new salary="+[Link]+"<br>")
}
}
ob=new employee()
[Link]()
[Link]()
</script>

Notes :- Javascript do not supports constructor overloading and method overloading .


➢ Parameterised constructor
Ex.1 :-
<script>
class employee
{
constructor (id,nm,sal)

{ [Link]=id
[Link]=nm
[Link]=sal
}
calculate()
{
[Link]=[Link]+[Link]*.10
}
output()
{
[Link]("employee id="+[Link]+"<br>")
[Link]("employee name="+[Link]+"<br>")
[Link]("old salary="+[Link]+"<br>")
[Link]("new salary="+[Link]+"<br>")
}
}
ob1=new
employee("e001","amit",4000)
[Link]() [Link]()
</script>

User friendly
<script> class
employee
{
constructor (id,nm,sal)

{ [Link]=id
[Link]=nm
[Link]=sal
}
calculate()
{
[Link]=[Link]+[Link]*.10
}
output()
{
[Link]("employee id="+[Link]+"<br>")
[Link]("employee name="+[Link]+"<br>")
[Link]("old salary="+[Link]+"<br>")
[Link]("new salary="+[Link]+"<br>")
}
}
[Link]=[Link]("enter a id")
[Link]=[Link]("enter a name")
[Link]=parseInt([Link]("enter the salary"))

ob1=new
employee(eid,enm,esal)
[Link]() [Link]()
</script>
EX2 :- <script>
class employee

{ input
()
{
[Link]=[Link]("Enter the id")
[Link]=[Link]("Enter the name")
[Link]=[Link]("Enter the city")
}
accept()
{
[Link]=parseInt([Link]("Enter the basic salary"))
}
calculate()
{
if([Link]>=10000)
{
[Link]=[Link]*.05;
[Link]=[Link]*.10;
}
else
{
[Link]=[Link]*.10;
[Link]=[Link]*.20;
}
[Link]=[Link]+[Link]+[Link]
}
display()
{
if([Link]>15000)
{
[Link]=[Link]*.05;
}
else
{
[Link]=[Link]*.02;
}
[Link]("Employee Id="+[Link]+"<br>")
[Link]("Employee name="+[Link]+"<br>")
[Link]("city="+[Link]+"<br>") [Link]("basic
salary="+[Link]+"<br>") [Link]("TA="+[Link]+"<br>")
[Link]("DA="+[Link]+"<br>")
[Link]("gross="+[Link]+"<br>")
[Link]("tax="+[Link]+"<br>") [Link]("employee net
payment="+([Link])+"<br>")
}
}
ob=new employee()
[Link]()
[Link]()
[Link]()
[Link]() </script>

➢ INHERITANCE :-
Single level inheritance :-

<!-- html code -->


<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Single Level Inheritance</title>
</head>
<body>

<form name="f">
<div>
<div>
Enter First No:
<input type="text" name="t1">
</div>
<div>
Enter Second No:
<input type="text" name="t2">
</div>
<div>
Result:
<input type="text" name="t3">
</div>
<div>
<input type="button" value="Sum" onclick="a()">
<input type="button" value="Sub" onclick="b()">
</div>
</div>
</form>

<!-- java script -->


<script>
class base
{
input(a,b)

{ this._a
=a
this._b=b
} output()
{ var z
z=this._a+this._b
return(z)
}
}
class child extends base

{ displa
y()
{ var
z
z=this._a-
this._b
return(z)
}
}
function a()
{
ob=new child()
[Link](parseInt([Link]),parseInt([Link]))
[Link]=[Link]()
}
function b()
{
ob=new child()
[Link](parseInt([Link]),parseInt([Link]))
[Link]=[Link]()
}
</script>
</body>
</html>

super :- super is the reserve word that is used to hold the address of base class .

➢ It is used to run the constructor and the method of base class .


➢ super always used in inheritance

Ex.1 :- calling base class constructor through child class .


◘ Without
html <script>
class base
{
constructor(x)

{ this._x
=x
}
display()
{
return(this._x*this._x)
}
}
class child extends base
{
constructor(a,b)
{
super(a)
this._y=b
}
disp()
{
return(this._x+this._y)
}
}
n1=parseInt([Link]("Enter a no.= "))
n2=parseInt([Link]("Enter a no.=
")) ob=new child(n1,n2) k=[Link]()
alert("Square= "+k) k=[Link]() alert("sum=
"+k)
</script>

◘ With html
<!-- html code -->
<!DOCTYPE html>
<body>
<form name="f">
<table border="1">
<tr>
<th>Enter First No </th>
<th><input type="text" name="t1"></th>
</tr>
<tr>
<th>Enter second No </th>
<th><input type="text" name="t2"></th>
</tr>
<tr><th> Result</th>
<th><input type="text" name="t3"></th>
</tr>
<th><input type="button" value="Square" onclick="a()"></th>
<th><input type="button" value="Sum" onclick="b()"></th>
</tr>
</table>
</form>

<!—java script -->

<script>
class base{
constructor(x)
{
this._x=x
}
display()
{
return(this._x*this._x)
}
}
class child extends base
{
constructor(a,b)
{
super(a)
this._y=b
}
disp()
{
return(this._x+this._y)
}
}
function a()
{
ob=new child(parseInt([Link]),parseInt([Link]))
[Link]=[Link]()
}
function b()
{
ob=new child(parseInt([Link]),parseInt([Link]))
[Link]=[Link]()
}
</script>
</body>
</html>

Q.1 :-
(i).create a class called product having following constructor and method.
a) The parametrised constructor should accept product id and product name.
b) A method called calculate to make discount on product cost by 20% .
(ii).create a class called my product that inherits product class and executes the
constructor of product class my product constructor accept three formal parameter for
product id , product name ,and product cost.

a) A method called display to print product id ,name ,cost and selling cost.

<script>
class product
{
constructor(id,nm)
{
this._id=id
this._nm=nm
}
calculate()
{
this._ncost=this._pcost-this._pcost*.20;
}
}
class myproduct extends product
{
constructor(id,nm,pcost)
{
super(id,nm)
this._pcost=pcost
}
display()
{

[Link]("product id="+this._id+"<br>")
[Link]("product name="+this._nm+"<br>")
[Link]("old cost="+this._pcost+"<br>")
[Link]("new cost="+this._ncost+"<br>")
}
}
id=[Link]("enter a id=")
nm=[Link]("enter a name.=")
cost=parseInt([Link]("enter a product cost="))
ob=new
myproduct(id,nm,cost)
[Link]()
[Link]()
</script>

◘ With array of object :-


<script>
class product
{
constructor(id,nm)
{
this._id=id
this._nm=nm
}
calculate()
{
this._ncost=this._pcost-this._pcost*.20;
}
}
class myproduct extends product
{
constructor(id,nm,pcost)
{
super(id,nm)
this._pcost=pcost
}
display()
{

[Link]("product id="+this._id+"<br>")
[Link]("product name="+this._nm+"<br>")
[Link]("old cost="+this._pcost+"<br>")
[Link]("new cost="+this._ncost+"<br>")
}
}
ob=[]
for(i=0;i<1;i++)
{
ob[i]=new myproduct("1","amit",50)
ob[i].calculate()
}
for(i=0;i<1;i++)
{
ob[i].display()
}
</script>

◘ Multilevel :-
<script> class
student{
constructor()
{
this._sid=[Link]("enter a id")
this._snm=[Link]("enter a name")
}
accept()
{
this._cfee=parseInt([Link]("enter the course fee"))
}
display()
{
[Link]("student id="+this._sid+"<br>")
[Link]("student name="+this._snm+"<br>")
[Link]("student name="+this._cfee+"<br>")
}
}
class studentchild extends student
{
constructor()

{ supe
r()
this._cnm=[Link]("enter the course name")

}
input()
{
this._i1=parseInt([Link]("enter the first installment"))
this._i2=parseInt([Link]("enter the second installment"))
}
calculate()
{
this._totins=this._i1+this._i2
this._bal=this._cfee-this._totins
}
}
class studentchild1 extends studentchild
{
constructor()

{ supe
r()
this._city=[Link]("enter the city")
}
output()
{
[Link]("city="+this._city+"<br>")
[Link]("course fee="+this._cfee+"<br>")
[Link]("total installment="+this._totins+"<br>")
[Link]("balance="+this._bal+"<br>")
}
}
ob=new studentchild1()
[Link]()
[Link]()
[Link]()
[Link]()
[Link]()
</script>

◘ hierarchical inheritance :-
• Without html :-
<script>
class product
{
constructor(pid,pnm)
{
this._pid=pid
this._pnm=pnm
}
display ()
{
[Link]("product id= "+this._pid+"<br>")
[Link]("name= "+this._pnm+"<br>")
}
}
class costumer extends product
{
constructor(cid,pid,pnm)
{
super(pid,pnm)
this._cid=cid
}
display()
{
[Link]()
[Link]("customer id= "+this._cid+"<br>")
}
}
class employee extends product
{
constructor(eid,esal,pid,pnm)
{
super(pid,pnm)
this._eid=eid
this._esal=esal }
output()
{
[Link]("employee id= "+this._eid+"<br>")
[Link]("salary= "+this._esal+"<br>")
}
}
ob= new costumer("c001","poo1","lux")
[Link]()
ob= new
employee("e001",1000,"poo2","margo")
[Link]() [Link]()
</script>

• With html :-
<!—Html code -->
<!DOCTYPE html>
<html lang="en">
<head>
<title>Customer</title>
</head>
<table border="1">
<body>
<form name="f">
<tr>
<th> customer id</th>
<th><input type="text" name=t1></th>
</tr>
<tr>
<th> Product id</th>
<th><input type="text" name=t2></th>
</tr>
<tr>
<th> employee id</th>
<th><input type="text" name=t5></th>
</tr>

<tr>
<th> employee salary</th>
<th><input type="text" name=t4></th>
</tr>
<tr>
<th>Product Name</th>
<th><input type="text" name=t3></th>
</tr>
<tr>
<th ><input type="button" value="click" onclick="a()"></th>
</tr>
</form>
</body>
</table>
</html>
<!—java script -->

<script>
class product
{
constructor(pid,pnm)
{
this._pid=pid
this._pnm=pnm
}
display ()
{
alert("product id= "+this._pid)
alert("name= "+this._pnm)
}
}
class customer extends product
{
constructor(cid,pid,pnm)
{
super(pid,pnm)
this._cid=cid
}
display()
{
[Link]()
alert("customer id= "+this._cid)
}
}
class employee extends product
{
constructor(eid,esal,pid,pnm)
{
super(pid,pnm)
this._eid=eid
this._esal=esal
}
output()
{
alert("employee id= "+this._eid)
alert("salary= "+this._esal)
}
}
function a()
{
ob=new
employee([Link],[Link],[Link],[Link])
[Link]() [Link]()
c=new customer([Link],[Link],[Link])
[Link]()
}
</script>

:- it is used to skip the running stage of loop .


Ex.1 :- <script>
for(i=0;i<=10;i++)
{
if(i%2==0)
continue //skip the loop
[Link](i+"<br>")
} </script>

:- it is used to stop running stage of loop.


Ex.1 :-
<script>
for(i=0;i<=10;i++)

{ if(i>
5)
break //stop the loop
[Link](i+"<br>")
}
</script>

:- it is same as while loop but it executes at list one time whether the
condition is true or not .
Ex.1 :-
<script>
i=1
do{
[Link](i+"<br>")
i++
}
while(i<=10)
</script>

You might also like