Anda di halaman 1dari 43

CS2312-OBJECT ORIENTED PROGRAMMING LABORATORY

CS 2312 OBJECT ORIENTED PROGRAMMING LABORATORY


Aim: To develop object-oriented programming skills using C++ and Java.
1. Function overloading, default arguments in C++
2. imple class design in C++, namespaces, objects creations
!. Class design in C++ using d"namic memor" allocation, destructor, cop"
constructor
#. $perator overloading, friend functions
%. $verloading assignment operator, t"pe conversions
&. 'n(eritance, run-time pol"morp(ism
). Template design in C++
*. '+$, T(ro,ing and Catc(ing e-ceptions
.. /rogram development using T0
11. imple class designs in Java ,it( Javadoc
11. 2esigning /ackages ,it( Javadoc comments
12. 'nterfaces and 'n(eritance in Java
1!. 3-ceptions (andling in Java
1#. Java '+$
1%. 2esign of multi-t(readed programs in Java
DEPARTMENT OF ELECTRICAL AND ELECTRONICS ENGINEERING,REC 1
CS2312-OBJECT ORIENTED PROGRAMMING LABORATORY
DETAILED SYLLABUS
1. Strin !"n!#t$n#ti"n %&in '(n#mi! m$m"r( #))"!#ti"n !"n!$*t

Aim
To implement t(e string concatenation function b" using d"namic
memor" allocation concept.
O+,$!ti-$
To concatenate t,o or more strings into one string b" allocating memor"
to objects at t(e time of t(eir construction.
E.$r!i&$&
1. Create class T4'56 ,it( t,o constructors. T(e first is an empt"
constructor, ,(ic( allo,s declaring an arra" of strings. T(e second
constructor initiali7es t(e lengt( of t(e strings, and allocates necessar"
space for t(e string to be stored and creates t(e string itself.
2. Create a member function to concatenate t,o strings.
!. 3stimate t(e combined lengt( of t(e strings to be joined and allocates
memor" for t(e combined string using ne, operator and t(en creates
t(e same using t(e string functions strcp"89 and strcat89.
#. 2ispla" t(e concatenated string.
2. Im*)$m$nt#ti"n "/ #rit0m$ti! "*$r#ti"n& "n !"m*)$. n%m+$r& %&in
!"n&tr%!t"r "-$r)"#'in
Aim
To implement arit(metic operations on comple- numbers using
constructor overloading
O+,$!ti-$
To represent comple- numbers and to perform arit(metic operations on
comple- numbers using overloaded constructors in a class.
DEPARTMENT OF ELECTRICAL AND ELECTRONICS ENGINEERING,REC 2
CS2312-OBJECT ORIENTED PROGRAMMING LABORATORY
E.$r!i&$&
1. Create class C$:/03; ,it( t(ree constructor to perform constructor
overloading. T(e first constructor takes no arguments ,(ic( is used to
create objects ,(ic( are not initiali7ed.t(e second takes one argument
,(ic( is used to create objects and initiali7e t(em and t(e t(ird takes
t,o arguments ,(ic( is also used to create objects and initiali7e t(em to
specific values.
2. 2eclare friend function.
!. $verload arit(metic operators +,-,<,+ to perform arit(metic operations on
t(e comple- numbers.
#. 2ispla" t(e results of eac( arit(metic operations.
3. T" r$#' # -#)%$ "/ 'i&t#n!$ /r"m "n$ "+,$!t #n' #'' 1it0 # -#)%$ in #n"t0$r
"+,$!t %&in /ri$n' /%n!ti"n
Aim
To read a value of distance from one object and add ,it( a value in
anot(er object using friend function.
O+,$!ti-$
To create t,o classes and store t(e values of distance and to read t(e
value from one class and add ,it( a value in anot(er object using friend
function.
E.$r!i&$&
1. Create t,o classes => and =C and store t(e value of distances.
2. 2eclare friend function.
!. 4ead t(e value from t(e classes.
#. /erform addition to add one object of => ,it( anot(er object of =C.
%. 2ispla" t(e result of addition.
DEPARTMENT OF ELECTRICAL AND ELECTRONICS ENGINEERING,REC 3
CS2312-OBJECT ORIENTED PROGRAMMING LABORATORY
2. Im*)$m$nt#ti"n "/ 3 #n' 4 "*$r#t"r "-$r)"#'in #n' im*)$m$nt#ti"n "/
#''iti"n "*$r#ti"n "/ "!t#) "+,$!t 1it0 int$$r %&in "*$r#t"r "-$r)"#'in
Aim
To implement + and ? operator overloading and to implement addition
operation of octal object ,it( integer using operator overloading.
O+,$!ti-$
8=9 To displa" t(e number of da"s bet,een t,o valid dates and t(e date
after a number of da"s from a valid date b" overloading t(e operators +
and -.
8>9To represent octal numbers and to add an octal object ,it( integer b"
overloading operator @+A.
E.$r!i&$&
5A6
1. Create a class called 2=T3 and define t,o member functions get-data
and displa"-result.
2. =ccept t,o valid dates in t(e form of dd+mm+"""" using get-data.
!. $verload operators + and ? to displa" t(e number of da"s bet,een t,o
valid dates using displa"-result.
#. 4epeat step ! to displa" t(e date after a number of da"s from a valid
date using displa"-result.
5B6
1. Create class $CT=0 for representing octal numbers.
2. Create a constructor to implement $CT=0 (B- ,(ere - is an integer.
!. $verload operator @+A to perform t(e integer addition ,it( an $CT=0
object like int "B (+k 8,(ere ( is an $CT=0 object and k is an integer9.
#. 2ispla" t(e resultant integer value ".
DEPARTMENT OF ELECTRICAL AND ELECTRONICS ENGINEERING,REC 2
CS2312-OBJECT ORIENTED PROGRAMMING LABORATORY
7. Im*)$m$nt#ti"n "/ #''iti"n #n' &%+tr#!ti"n "/ t1" *")(n"mi#) "+,$!t&
%&in "*$r#t"r "-$r)"#'in
Aim
To implement addition and subtraction operations of t,o pol"nomials
and displa" using CC operator overloading.
O+,$!ti-$
To add and subtract t,o /$0D5$:'=0 objects and to displa" results b"
overloading t(e operator CC.
E.$r!i&$&
1. Create a class called /$0D5$:'=0 ,it( constructors to create
pol"nomial objects and to initiali7e ,it( specific values.
2. Create member functions to perform addition and subtraction of t,o
pol"nomials.
!. $verload operator CC to displa" t(e results of addition and subtraction
operations on t,o pol"nomials.
#. 2ispla" t(e results.
8. M#n#in +#n9 #!!"%nt %&in in0$rit#n!$ !"n!$*t
Aim
To manage t(e account information of t(e customer using in(eritance
concept.
O+,$!ti-$
To maintain and update t(e customer account specific information using
in(eritance concept.
E.$r!i&$&
1. Create a class ,it( t(e follo,ing member variables. Customer name,
account number and account t"pe.
DEPARTMENT OF ELECTRICAL AND ELECTRONICS ENGINEERING,REC 7
CS2312-OBJECT ORIENTED PROGRAMMING LABORATORY
2. Create t(e derived classes ,it( follo,ing member variables.
for current account information
>alance, 2eposit and ,it(dra,al amount
for savings account information
>alance and 2eposit amount
!. Erite a member function to get t(e 2eposit and ,it(dra,al amount and
to update t(e balance information for current account.
#. Erite a member function to get t(e 2eposit amount and to update t(e
balance information for saving account.
%. Erite a member function to 2ispla" t(e balance information for
respective account t"pe.
:. T" !"m*%t$ t0$ #r$# "/ tri#n)$ #n' r$!t#n)$ %&in in0$rit#n!$ #n'
-irt%#) /%n!ti"n
Aim
To implement derived class and virtual function concepts.
O+,$!ti-$
To calculate t(e area of triangle and rectangle using derived classes and
displa" t(e result using virtual function.

E.$r!i&$&
1. Create a base class F=/3.
2. 2erive t,o sub classes T4'=5603 and 43CT=5603 from t(e base
class F=/3.
!. 2efine member functions getGdata89 and displa"Garea89.
#. Find out t(e area of triangle and rectangle and displa" t(e result using
displa"Garea89.
&. :ake displa"Garea89 as a virtual function.
DEPARTMENT OF ELECTRICAL AND ELECTRONICS ENGINEERING,REC 8
CS2312-OBJECT ORIENTED PROGRAMMING LABORATORY
;.T$m*)#t$&
Aim:
To ,rite a c++ program for s,apping t,o values using function templates.
A)"rit0m:
1. 2eclare a template CclassTH.
2. tart t(e main program.
!. 2eclare t(e integer variables i1 and i2,float variables f1 and f2,c(ar variables c1
and
c2.
#. 4ead t(e t,o integers i1 and i2.
%. /rint i1 and i2 before s,apping.
&. Call t(e function s,ap8i1,i29.
). print i1 and i2 after s,apping.
*. 4ead t(e t,o floats f1 and f2.
.. /rint f1 and f2 before s,apping.
11. Call t(e function s,ap8f1,f29.
11. /rint f1 and f2 after s,apping.
12. 4ead t(e t,o c(aracters c1 and c2.
1!. /rint c1 and c2 before s,apping.
1#. Call t(e function s,ap8c1,c29.
1%. /rint t(e values of c1 and c2 after s,apping.
1&. top t(e program.
A)"rit0m /"r S1#*5T <#,T<+6
1. Create a template variable t.
2. =ssign tBa,aBb and bBt.
!. 4eturn.
DEPARTMENT OF ELECTRICAL AND ELECTRONICS ENGINEERING,REC :
CS2312-OBJECT ORIENTED PROGRAMMING LABORATORY
=.>ritin &im*)$ *r"r#m& in J#-#
Aim
To generate random numbers using simple Java program
O+,$!ti-$
To generate random numbers using built in function random89.
E.$r!i&$&
1. Create a class called rand ,it( variable declaration and includes (eader
file mat(.
2. 6enerate random numbers using built in function random89.
!. 2ispla" t(e result.
1?. U&$ "/ Int$r/#!$& in J#-#
Aim
To calculate area of rectangle and circle using interfaces.
O+,$!ti-$
To create t,o classes and store t(e values of distance and to read t(e
values from one class and add ,it( a value in anot(er object using
friend function
E.$r!i&$&
1. Create t,o classes => and =C and store t(e value of distances.
2. 2eclare friend function.
!. 4ead t(e value from t(e classes.
#. /erform addition to one object of => ,it( anot(er object of =C.
%. 2ispla" t(e result of addition.
DEPARTMENT OF ELECTRICAL AND ELECTRONICS ENGINEERING,REC ;
CS2312-OBJECT ORIENTED PROGRAMMING LABORATORY
11. D$&inin P#!9#$& 1it0 J#-#'"! !"mm$nt&
=imI
To ,rite t(e java application for simple /ackage creation.
- 2eveloping user defined packages in Java.
=lgorit(mI
1. Create a package b"
a. 2ecalring a package statement as t(e first stmt of t(e java application.
i. /ackage packagenameJ
2. Create a class named imple.
!. 'nside t(e main function do
a. 2eclare a tring $bject and assign it a statement.
i. tring sBK/ackage =pplication ampleKJ
b. /rint t(e string s using t(e prinln statement.
i. "stem.out.println8L$utput is L+s9J
c. Close t(e main and terminate t(e class.
12. E@CEPTION AANDLING 4 PRE4DEFINED E@CEPTIONS
=imI
To create a java application for 3-ception Fandling :ec(anism.
- Fandling pre-defined e-ceptions
=lgorit(mI
1. Create a class named 2ef3-ception
2. 'nside t(e main function
!. 'nside a tr" block do
a. 2eclare an integer arra" of si7e !
i. 'nt 'MNBO2PJ
b. =ssing a value to t(e 11
t(
position of t(e arra"
i. 'nt 'M11NBO21PJ
ii. Close t(e tr" block.
iii. Create t(e corresponding catc( block to catc( t(e e-ception
5egative=rra"i7e3-ception
a. catc(85egative=rra"i7e3-ception n9OP
2. 'nside t(e catc( block print t(e e-ception caug(t using t(e
println statements.
a. "stem.out.println 8Lgenerated e-ceptionK+n9J
iv. Close t(e main function and terminate t(e class.
DEPARTMENT OF ELECTRICAL AND ELECTRONICS ENGINEERING,REC =
CS2312-OBJECT ORIENTED PROGRAMMING LABORATORY
13. USER4DEFINED E@CEPTIONS
=imI
To ,rite a java application for (andling user-defined e-ceptions.
=lgorit(mI
1. 'mport t(e package java.ioJ
2. Create t(e class:"ec-ception e-tending t(e 3-ception class.
!. 'nside t(e class do
a. 2eclare a private variable @aA of integer t"pe.
b. 'nside t(e constructor getting a single argument do
i. =ssign t(e obtained argument to aJ
ii. Close t(e constructor.
c. 2eclare and defne t(e totring met(od ,(ic( returns t(e variable a.
#. close t(e met(od and t(e class.
%. Create anot(er class Qser2ef3-ception
a. 'nside t( class do
i. 2eclare a public integer variable - .
ii. 2eclare anot(er variable as final of integer t"pe @kA.
iii. 2eclare and define a met(od get'nt89 and do
1. 'nside t(e tr" block
2. Create >uffered4eader object dis . To accept input
numbers from t(e user.
!. 2eclare a string object line.
#. Qsing a ,(ile loop c(eck ,(et(er 8lineBdis.read0ine899R
Bnull.
%. if t(e condition is true do
a. Qsing parse'nt met(od convert t(e obtained string
input to number and store in a variable -.
b. Qsing if statement c(eck ,(et(er input in @-A is eSual
o %.
c. 'f true /rint Lcongrats-generated e-ceptionK
d. $t(er,ise Ltr" againK.
e. Close t(e tr" block.
f. Create a catc( block for catc(ing t(e generated
e-ception.
g. Create anot(er catc( block for catc(ing t(e
5umberFormat3-ception. do
i. /rint t(e e-ceptions.
(. Create a catc( block for catc(ing t(e ioe-ception.
i. Close t(e get'nt met(od
&. create t(e main function do
a. create t(e object for t(e Qser2ef3-ception class.
b. Call t(e get'nt met(od ,it( (elp of t(e created object.
DEPARTMENT OF ELECTRICAL AND ELECTRONICS ENGINEERING,REC 1?
CS2312-OBJECT ORIENTED PROGRAMMING LABORATORY
12. MULTITAREADING
Aim:
To create a java application for implementing Multithreading.
Algorithm:
1. Create a class intThread1 implementing the predefined interface Runnable.
2. nside the class do
a. !eclare a string s.
". Create a instance of Thread class.
i. Thread t#
$. !eclare and define the constructor intThread%&tring st' and do
i. Assign st to s.
ii. Create a object for the Thread Class.
iii. (ass the &tring st and its o)n object as arguments for thread
constructor for creating ne) child thread
1. t*ne) Thread%this+, Test Thread,'#
iv. (rint the object of the Thread class.
1. &-stem.out.println%./e) thread,+0t'#
v. &tart the thread b-
1. t.start%'#
vi. close the constructor.
1. Create the run method do
a. Create a tr- bloc2 and do
i. Create a for loop starting from *1 and ending at 3*4 do
1. print the variable .
a. s-stem.out.println%.s,0'#
2. create the sleep method 5)hich suspends the thread for
144milliseconds.
a. Thread.sleep%144'#
". close the tr- bloc2.
$. declare a catch bloc2 for capturing the e6ception created b- sleep
method.
a. Catch%nteruppted76ception e'89
1. print the &tatement )hile e6citing the child thread.
a. &-stem.out.println%st0.76iting child thread,'#
:. Create another class inthread 2
a. Create the main function and do
b. Create t)o threads m1 and m2
i. ntThread m1*ne) intThread%.one,'#
ii. ntThread m2*ne) intThread%.t)o,'#
iii. (rint the statements .Thread is alive, for chec2ing the thread is aliveb-
using
1. &-stem.out.println%.Thread m1 is alive .0m1.t.isAlive%''#
2. do the same for second thread.
iv. nside the tr- bloc2 do
1. print the statement .;aiting for threads to finish,.
2. declare m1.t.join < m2.t.join method to )ait forever the thread
to die.
". close the tr- bloc2.
$. declare the catch bloc2 to catch the e6ception.
DEPARTMENT OF ELECTRICAL AND ELECTRONICS ENGINEERING,REC 11
CS2312-OBJECT ORIENTED PROGRAMMING LABORATORY
v. (rint the statements .Thread is alive, for rechec2ing the status of the
threads using
1. &-stem.out.println%.Thread m1 is alive .0m1.t.isAlive%''#
2. &-stem.out.println%.Thread m1 is alive .0m2.t.isAlive%''#
vi. (rint the string .main thread is e6citing,.
c. Close the main and terminate the class.
DEPARTMENT OF ELECTRICAL AND ELECTRONICS ENGINEERING,REC 12
CS2312-OBJECT ORIENTED PROGRAMMING LABORATORY
Pr. N".1
STRING CONCATENATION
AIM:
To implement t(e string concatenation function b" using d"namic memor"
allocation concept.
REBUIREMENTS:
Fard,are I /entium 'T /C
oft,are I Turbo C++
PROGRAM:
+<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
/r.5o.1 2ateI
T4'56 C$5C=T35=T'$5
5ame I
4eg 5o. I
$bjective I To concatenate t,o or more strings into one string b" allocating
memor" to objects at t(e time of t(eir construction.
<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<+
UincludeCiostream.(H
UincludeCstring.(H
UincludeCconio.(H
class string
O
c(ar <nameJ
int lengt(J
publicI
string89
O
lengt(B1J
nameBne, c(arMlengt(+1NJ
P
DEPARTMENT OF ELECTRICAL AND ELECTRONICS ENGINEERING,REC 13
CS2312-OBJECT ORIENTED PROGRAMMING LABORATORY
string8c(ar<s9
O
lengt(Bstrlen8s9J
nameBne, c(arMlengt(+1NJ
strcp"8name,s9J
P
void displa"8void9
O
coutCCnameCCVWnVJ
P
void join8string Xa,string Xb9J
PJ
void stringIIjoin8string Xa,string Xb9
O
lengt(Ba.lengt(+b.lengt(J
delete nameJ
nameBne, c(arMlengt(+1NJ
strcp"8name,a.name9J
strcat8name,b.name9J
PJ
void main89
O
clrscr89J
string name1 8V$bject V9J
string name2 8V$riented V9J
string name! 8V/rogramming 0abV9J
string s1,s2J
s1.join8name1,name29J
s2.join8s1,name!9J
name1.displa"89J
DEPARTMENT OF ELECTRICAL AND ELECTRONICS ENGINEERING,REC 12
CS2312-OBJECT ORIENTED PROGRAMMING LABORATORY
name2.displa"89J
name!.displa"89J
s1.displa"89J
s2.displa"89J
getc(89J
P
OUTPUT:
$bject
$riented
/rogramming 0ab
$bject $riented
$bject $riented /rogramming 0ab
DEPARTMENT OF ELECTRICAL AND ELECTRONICS ENGINEERING,REC 17
CS2312-OBJECT ORIENTED PROGRAMMING LABORATORY
Pr. N".2
ARITAMETIC OPERATIONS ON COMPLE@ NUMBERS
AIM:
To implement arit(metic operations on comple- numbers using constructor
overloading
REBUIREMENTS:
Fard,are I /entium 'T /C
oft,are I Turbo C++
PROGRAM:
+<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
/r.5o.2 2ateI

=4'TF:3T'C $/34=T'$5 $5 C$:/03; 5Q:>34
5ame I
4eg.5o. I
$bjective I To implement arit(metic operation on comple- numbers using
constructor over loading.
<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<+
UincludeCiostream.(H
UincludeCmat(.(H
UincludeCconio.(H
class comple-
O
float -,"J
publicI
comple-89OP
comple-8float 79
O
-B"B7J
P
DEPARTMENT OF ELECTRICAL AND ELECTRONICS ENGINEERING,REC 18
CS2312-OBJECT ORIENTED PROGRAMMING LABORATORY
comple-8float real,float img9
O
-BrealJ
"BimgJ
P
friend comple- operator+8comple-,comple-9J
friend comple- operator-8comple-,comple-9J
friend comple- operator<8comple-,comple-9J
friend comple- operator+8comple-,comple-9J
friend void s(o,8comple-9J
PJ
comple- operator+ 8comple- c1,comple- c29
O
comple- c!J
c!.-Bc1.-+c2.-J
c!."Bc1."+c2."J
return c!J
P
comple- operator- 8comple- c1,comple- c29
O
comple- c!J
c!.-Bc1.--c2.-J
c!."Bc1."-c2."J
return c!J
P
comple- operator< 8comple- c1,comple- c29
O
comple- c!J
c!.-B8c1.-<c2.-9-8c1."<c2."9J
c!."B8c1."<c2."9+8c1.-<c2.-9J
return c!J
P
DEPARTMENT OF ELECTRICAL AND ELECTRONICS ENGINEERING,REC 1:
CS2312-OBJECT ORIENTED PROGRAMMING LABORATORY
comple- operator+ 8comple- c1,comple- c29
O
comple- c!J
float aJ
aB88c2.-<c2.-9+8c2."<c2."99J
c!.-B88c1.-<c2.-9+8c1."<c2."99+aJ
c!."B88c1."+c2.-9-8c1."<c2."99+aJ
return c!J
P
void s(o,8comple- c9
O
if8c."C19
coutCCc.-CCV-jVCCfabs8c."9CCendlJ
else
coutCCc.-CCV+jVCCfabs8c."9CCendlJ
P
void main89
O
comple- =81,29J
comple- >8!,19J
comple- C,2,3,FJ
clrscr89J
CB=+>J
2B=->J
3B=<>J
FB=+>J
coutCCVComple- 5oI1 B VJ
s(o,8=9J
coutCCVComple- 5oI2 B VJ
s(o,8>9J
coutCCVum B VJ
s(o,8C9J
DEPARTMENT OF ELECTRICAL AND ELECTRONICS ENGINEERING,REC 1;
CS2312-OBJECT ORIENTED PROGRAMMING LABORATORY
coutCCV2ifference B VJ
s(o,829J
coutCCV/roduct B VJ
s(o,839J
coutCCVYuotient B VJ
s(o,8F9J
getc(89J
P
OUTPUT:
Comple- 5oI1 B 1+j2
Comple- 5oI2 B !+j1
um B #+j!
2ifference B -2+j1
/roduct B 1+j%
Yuotient B 1.%-j1.1!!!!!
DEPARTMENT OF ELECTRICAL AND ELECTRONICS ENGINEERING,REC 1=
CS2312-OBJECT ORIENTED PROGRAMMING LABORATORY
Pr. N".3
ADDITION OF DISTANCES
AIM:
To read a value of distance from one object and add ,it( a value in anot(er
object using friend function
REBUIREMENTS:
Fard,are I /entium 'T /C
oft,are I Turbo C++
PROGRAM:
+<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
/r.5o.! 2ateI
$>J3CT =22'T'$5 Q'56 F4'352 FQ5CT'$5
5ame I
4eg.5o. I
$bjective I To create t,o classes and to store t(e value of distance and to read
value from one class and add ,it( a value in anot(er object using
friend function.
<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<+
UincludeCiostream.(H
UincludeCconio.(H
UincludeCmat(.(H
class =CJ
class =>
O
float dJ
publicI
=>89
O
coutCCV3nter t(e first distance8in feet9IVJ
DEPARTMENT OF ELECTRICAL AND ELECTRONICS ENGINEERING,REC 2?
CS2312-OBJECT ORIENTED PROGRAMMING LABORATORY
cinHHdJ
P
friend void add8=>,=C9J
PJ
class =C
O
float dJ
publicI
=C89
O
coutCCV3nter t(e second distance8in inc(es9IVJ
cinHHdJ
P
friend void add8=> a,=C b9
O
float totalJ
b.dBb.d+12J
totalBa.d+b.dJ
coutCCVTotal 2istanceIVCCtotalCCVfeetVJ
P
PJ
void main89
O
clrscr89J
=> =J
=C >J
add8=,>9J
getc(89J
P
DEPARTMENT OF ELECTRICAL AND ELECTRONICS ENGINEERING,REC 21
CS2312-OBJECT ORIENTED PROGRAMMING LABORATORY
OUTPUT:
3nter t(e first distance8in feet9I12
3nter t(e second distance8in inc(es9I!1
Total 2istanceI1#.%feet
Pr. N".2
OPERATOR OCERLOADING AND
ADDITION OF OCTAL >ITA INTEGER
AIM:
To implement + and ? operator overloading and to implement addition
operation of octal object ,it( integer using operator overloading.
REBUIREMENTS:
Fard,are I /entium 'T /C
oft,are I Turbo C++
PROGRAM:
+<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
/r.5o.#= 2ateI
':/03:35T=T'$5 $F $/34=T$4 $T340$=2'56
5ame I
4eg.5o. I
$bjective I To displa" t(e number of da"s bet,een t,o valid dates and to
displa" t(e date after a number of da"s from a valid date b"
overloading t(e operator Z+Z and Z-Z.
<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<+
UincludeCiostream.(H
UincludeCconio.(H
class date
O
int da",date,mont(,"earJ
publicI
date89
O
DEPARTMENT OF ELECTRICAL AND ELECTRONICS ENGINEERING,REC 22
CS2312-OBJECT ORIENTED PROGRAMMING LABORATORY
da"B1J
mont(B1J
"earB1J
P
date8int d, int m, int "9
O
da"BdJ
mont(BmJ
"earB"J
P
void getdata89
O
coutCCVdd mm """"VCCendlJ
cinHHda"HHmont(HH"earJ
P
void displa"result89
O
coutCCda"CCVIVCCmont(CCVIVCC"earCCendlJ
P
int isleap"ear89
O
if88"ear[#BB1XX"ear[111RB19 \\ 8"ear[#11BB199
return 1J
else
return 1J
P
int t(ismont(ma-89
O
int mM12NBM!1,2*,!1,!1,!1,!1,!1,!1,!1,!1,!1,!1NJ
if8mont(BB2XXisleap"ear899
DEPARTMENT OF ELECTRICAL AND ELECTRONICS ENGINEERING,REC 23
CS2312-OBJECT ORIENTED PROGRAMMING LABORATORY
return 2.J
else
return mMmont(-1NJ
P
void operator+89
O
++da"J
if8da"Ht(ismont(ma-899
O
da"B1J
mont(++J
P
if8mont(H129
O
mont(B1J
"ear++J
P
P
void operator-89
O
int i,d1,d2,dd,m1,m2,md,msB1,"1,"2,"d,"s,tda"sJ
coutCCV3nter date 1IVJ
cinHHd1HHm1HH"1J
coutCCV3nter date 2IVJ
cinHHd2HHm2HH"2J
int mM12NBO!1,2*,!1,!1,!1,!1,!1,!1,!1,!1,!1,!1PJ
if8"2C"19
O
coutCCV'nvalid dateVJ
P
else
O
"dB"2-"1J
DEPARTMENT OF ELECTRICAL AND ELECTRONICS ENGINEERING,REC 22
CS2312-OBJECT ORIENTED PROGRAMMING LABORATORY
P
if8m2Cm19
O
m1Bm1+12J
"1--J
mdBm2-m1J
P
else
O
mdBm2-m1J
P
if8d2Cd19
O
d1Bd1+mMm1-1NJ
m1--J
ddBd2-d1J
P
else
O
ddBd2-d1J
P
for8iBm1JiCm2Ji++9
O
msBms+mMiNJ
P
if88"2[#11BB19\\8"2[111RB19XX8"2[1BB199
O
"sB!&&<"dJ
P
else
"sB!&%<"dJ
tda"sBdd+ms+"sJ
coutCCV2'FF3435C3BVCCtda"sJ
P
DEPARTMENT OF ELECTRICAL AND ELECTRONICS ENGINEERING,REC 27
CS2312-OBJECT ORIENTED PROGRAMMING LABORATORY
PJ
DEPARTMENT OF ELECTRICAL AND ELECTRONICS ENGINEERING,REC 28
CS2312-OBJECT ORIENTED PROGRAMMING LABORATORY
void increda"8date Xd9
O
int i,noJ
coutCCV2=T3VJ
d.displa"result89J
coutCCV3nter t(e no of da"s after ,(ic( it s(ould be displa"edVJ
cinHHnoJ
for8iB1JiCnoJi++9
O
+dJ
P
coutCCV$n 'ncrement it becomesVJ
d.displa"result89J
coutCCendlJ
P
void diff89
O
date dJ
-dJ
P
void main89
O
clrscr89J
date -J
-.getdata89J
increda"8-9J
diff89J
getc(89J
P
DEPARTMENT OF ELECTRICAL AND ELECTRONICS ENGINEERING,REC 2:
CS2312-OBJECT ORIENTED PROGRAMMING LABORATORY
OUTPUT:
dd mm """"
) . 211)
2=T3)I.I211)
3nter t(e no of da"s after ,(ic( it s(ould be displa"ed%
$n 'ncrement it becomes12I.I211)
3nter date 1I% 11 211)
3nter date 2I% 11 211)
2'FF3435C3B!1
+<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
/r.5o.#> 2ateI
':/03:35T=T'$5 $F $/34=T$4 $T340$=2'56
5ame I
4eg.5o. I
$bjective I To represent octal number and to add an object ,it( integer b"
overloading t(e operator Z+Z.
<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<+
UincludeCiostream.(H
UincludeCconio.(H
class octal
O
int -,",(,kJ
publicI
octal89
O
(B-J
P
void getdata89J
void displa"89J
void operator+89J
PJ
DEPARTMENT OF ELECTRICAL AND ELECTRONICS ENGINEERING,REC 2;
CS2312-OBJECT ORIENTED PROGRAMMING LABORATORY
void octalIIgetdata89
O
coutCCV 3nter t(e $ctal 5o B VJ
cinHHoctHH(J
coutCCV 3nter t(e 'nteger Talue B VJ
cinHHkJ
P
void octalIIdispla"89
O
coutCCV $ctal Talue B VCC(CCendlJ
coutCCV 'nteger Talue B VCCkCCendlJ
coutCCV um B VCC"CCendlJ
P
void octalIIoperator+89
O
" B (+kJ
P
void main89
O
clrscr89J
octal sJ
s.getdata89J
+sJ
s.displa"89J
getc(89J
P
OUTPUT:
3nter t(e $ctal 5o B &&
3nter t(e 'nteger Talue B !
$ctal Talue B %#
'nteger Talue B !
um B %)
DEPARTMENT OF ELECTRICAL AND ELECTRONICS ENGINEERING,REC 2=
CS2312-OBJECT ORIENTED PROGRAMMING LABORATORY
Pr. N".7
ADDITION AND SUBTRACTION OF T>O POLYNOMIALS
AIM:
To implement addition and subtraction operations of t,o pol"nomials and
displa" using CC operator overloading.
REBUIREMENTS:
Fard,are I /entium 'T /C
oft,are I Turbo C++
PROGRAM:
<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
/r.5o.% 2ateI
/$0D5$:'5=0 $>J3CT Q'56 $/34=T$4 $T340$=2'56
5ame I
4eg.5o. I
$bjective I To add and subtract t,o pol"nomial of objects and to displa" t(e
result b" over loading.
<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<+
UincludeCiostream.(H
UincludeCconio.(H
class pol"
O
publicI
int a,b,c,dJ
pol"89
OP
pol" 8int ,, int -, int ", int 79
O
aB,J
bB-J
cB"J
DEPARTMENT OF ELECTRICAL AND ELECTRONICS ENGINEERING,REC 3?
CS2312-OBJECT ORIENTED PROGRAMMING LABORATORY
dB7J
P
friend pol" add8pol",pol"9J
friend pol" sub8pol",pol"9J
friend void operator CC8pol" 71,pol" 729
O
coutCC71.aCCV-]!+VCC71.bCCV-]2+VCC71.cCCV-+VCC71.dJ
P
PJ
pol" add8pol" S1, pol" S29
O
pol" rJ
r.aBS1.a+S2.aJ
r.bBS1.b+S2.bJ
r.cBS1.c+S2.cJ
r.dBS1.d+S2.dJ
return rJ
P
pol" sub 8pol" S1, pol" S29
O
pol" sJ
s.aBS1.a+S2.aJ
s.bBS1.b+S2.bJ
s.cBS1.c+S2.cJ
s.dBS1.d+S2.dJ
return sJ
P
void main 89
O
pol" p!,p1J
pol" p281,1,1,19J
DEPARTMENT OF ELECTRICAL AND ELECTRONICS ENGINEERING,REC 31
CS2312-OBJECT ORIENTED PROGRAMMING LABORATORY
int a,b,c,dJ
clrscr 89J
coutCCVWnF'4T /$0D5$:'5=0I VJ
p2CCp2J
coutCCVWn3nter t(e co-efficient of cubic pol"nomialIVJ
coutCCVWn-]!VJ
cinHHp1.aJ
coutCCVWn-]2VJ
cinHHp1.bJ
coutCCV-VJ
cinHHp1.cJ
coutCCVconstantVJ
cinHHp1.dJ
coutCCVWnWnsumWnVJ
p!Badd8p1,p29J
p!CCp!J
coutCCVWn2ifference WnVJ
p!Bsub8p1,p!9J
p!CCp!J
getc(89J
P
OUTPUT:
F'4T /$0D5$:'5=0I 1-]!+1-]2+1-+1
3nter t(e co-efficient of cubic pol"nomialI
-]! *
-]2 .
- *
constant &
sum
.-]!+11-]2+.-+)
2ifference
1)-]!+1.-]2+1)-+1!
DEPARTMENT OF ELECTRICAL AND ELECTRONICS ENGINEERING,REC 32
CS2312-OBJECT ORIENTED PROGRAMMING LABORATORY
Pr. N".8
BAND ACCOUNT MAINTENANCE
AIM:
To manage t(e account information of t(e customer using in(eritance concept.
REBUIREMENTS:
Fard,are I /entium 'T /C
oft,are I Turbo C++
PROGRAM:
+<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
/r.5o.) 2ateI
:=5=6'56 >=5^ =CC$Q5T Q'56 '5F34'T=5C3
5ame I
4eg.5o. I
$bjective I To maintain and update customer account specific information
using in(eritance.
<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<+
UincludeCiostream.(H
UincludeCconio.(H
class bank
O
publicI
c(ar<cnameJ
long int acnoJ
void displa"89
O
coutCCcnameJ
coutCCacnoJ
DEPARTMENT OF ELECTRICAL AND ELECTRONICS ENGINEERING,REC 33
CS2312-OBJECT ORIENTED PROGRAMMING LABORATORY
P
PJ
class savings I public bank
O
publicI
int ,dra,, dep, balJ
void saves89
O
coutCCV3nter t(e amount to be depositedB4s.VJ
cinHHdepJ
bal+BdepJ
coutCCV3nter t(e amount to be ,it(dra,nB4s.VJ
cinHH,dra,J
bal-B,dra,J
coutCCVDour =+c >alance isB4s.VCCbalCCendlJ
P
PJ
class current I public savings
O
publicI
void info89
O
coutCCV0ast amount ,itdra,nB4s.VCC,dra,CCendlJ
coutCCV0ast amount depositedB4s.VCCdepCCendlJ
coutCCVDour =+c >alance isB4s.VCCbalCCendlJ
P
PJ
DEPARTMENT OF ELECTRICAL AND ELECTRONICS ENGINEERING,REC 32
CS2312-OBJECT ORIENTED PROGRAMMING LABORATORY
void main89
O
int c(J
current acJ
clrscr89J
coutCCV3nter customer nameIVJ
cinHHac.cnameJ
coutCCendlCCV3nter "our =+c no.IVJ
cinHHac.acnoJ
coutCCendlJ
,(ile819
O
coutCCV3nter t(e =+c t"pe,1.avings 2.Current !.3-itWnVJ
cinHHc(J
s,itc(8c(9
O
case 1I
ac.saves89J
breakJ
case 2I
ac.info89J
breakJ
case !I
breakJ
defaultI
coutCCV'nvalid C(oiceVJ
breakJ
P
if 8c(BB!9
breakJ
P
getc(89J
P
DEPARTMENT OF ELECTRICAL AND ELECTRONICS ENGINEERING,REC 37
CS2312-OBJECT ORIENTED PROGRAMMING LABORATORY
OUTPUT:
3nter customer nameI ;;;
3nter "our =+c no.I&&&
3nter t(e =+c t"peI 1.avings 2.Current !.3-it
1
3nter t(e amount to be depositedB4s.%111
3nter t(e amount to be ,it(dra,nB4s.%11
Dour =+c >alance isB4s.#%11
3nter t(e =+c t"peI 1.avings 2.Current !.3-it
2
0ast amount ,it(dra,nB4s.%11
0ast amount depositedB4s.%111
Dour =+c >alance isB4s.#%11
3nter t(e =+c t"peI 1.avings 2.Current !.3-it
!
DEPARTMENT OF ELECTRICAL AND ELECTRONICS ENGINEERING,REC 38
CS2312-OBJECT ORIENTED PROGRAMMING LABORATORY
Pr. N".:
AREA OF TRIANGLE AND RECTANGLE
AIM:
To calculate t(e area of triangle and rectangle using derived classes and
displa" t(e result using virtual function
REBUIREMENTS:
Fard,are I /entium 'T /C
oft,are I Turbo C++
PROGRAM:
+<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
/r.5o.& 2ateI
=43= $F T4'=5603 =52 43CT=5603 Q'56 T'4TQ=0 FQ5CT'$5
5ame I
4eg.5o. I
$bjective I To calculate t(e area of a triangle and rectangle using derived
classes and displa" t(e result using virtual functions
<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<+
UincludeCiostream.(H
UincludeCconio.(H
class s(ape
O
publicI
virtual void getdata89
OP
virtual void displa"89
OP
PJ
class triangleIpublic s(ape
O
DEPARTMENT OF ELECTRICAL AND ELECTRONICS ENGINEERING,REC 3:
CS2312-OBJECT ORIENTED PROGRAMMING LABORATORY
int (,bsJ
float atJ
publicI
void getdata89
O
coutCCV3nter t(e (eig(t and base of triangle B VJ
cinHH(HHbsJ
P
areatri89
O
atB88bs<(9+2.19J
return 8at9J
P
void displa"89
O
coutCCV=rea of Triangle B VCCatCCV S.QnitsVCCendlJ
P
PJ
class rectangleIpublic s(ape
O
int l,b,arJ
publicI
void getdata89
O
coutCCV3nter t(e lengt( and breadt( of rectangle B VJ
cinHHlHHbJ
P
arearec89
O
arB8l<b9J
return8ar9J
P
void displa"89
DEPARTMENT OF ELECTRICAL AND ELECTRONICS ENGINEERING,REC 3;
CS2312-OBJECT ORIENTED PROGRAMMING LABORATORY
O
coutCCV=rea of 4ectangle B VCCarCCV S.QnitsVCCendlJ
P
PJ
void main89
O
clrscr89J
s(ape sJ
triangle tJ
rectangle rJ
s(ape <bptrJ
coutCCVFor TriangleVCCendlJ
bptrBXtJ
bptr-Hgetdata89J
t.areatri89J
bptr-Hdispla"89J
coutCCVFor 4ectangleVCCendlJ
bptrBXrJ
bptr-Hgetdata89J
r.arearec89J
bptr-Hdispla"89J
getc(89J
P
OUTPUT:
For Triangle
3nter t(e (eig(t and base of triangle B # &
=rea of Triangle B 12 S.Qnits
For 4ectangle
3nter t(e lengt( and breadt( of rectangle B ) *
=rea of 4ectangle B %& S.Qnits
DEPARTMENT OF ELECTRICAL AND ELECTRONICS ENGINEERING,REC 3=
CS2312-OBJECT ORIENTED PROGRAMMING LABORATORY
Pr. N".;
RANDOM NUMBERS
AIM:
To generate random numbers using simple Java program
REBUIREMENTS:
Fard,are I /entium 'T /C
oft,are I J2^1.!
PROGRAM:
+<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
/r.5o.* 2ateI
4=52$: 5Q:>34
5ame I
4eg.5o. I
$bjective I To generate random numbers using built in function random89
<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<+
class rand
O
public static void main8tring argsMN9
O
double raJ
int ' J
"stem.out.println8V4andom 5umbers areIV9J
for 8iB 1JiCB11Ji++9
O
raB:at(.random89J
"stem.out.println8VWtV+ra9J
P
P
P
DEPARTMENT OF ELECTRICAL AND ELECTRONICS ENGINEERING,REC 2?
CS2312-OBJECT ORIENTED PROGRAMMING LABORATORY
COMPILATION AND E@ECUTION:
2IWjavaprgHjavac rand.java
2IWjavaprgHjava rand
OUTPUT:
4andom 5umbers areI
1.)1)**112.)1)!*#*
1.112)2!1!#&&!%1!.&
1.1.**1!*2.1!1!**%!
1.21%21.1#.!))!&*1*
1.#.#&##&2#*1&2.%1
1.)2!&*)&)2.%.1))#
1.1&!&22!2##&*#%)*#
1.!11!&!111!2&2*!&
1..*&))!1&#!!1%)2
1.***1#121..*!1.!
DEPARTMENT OF ELECTRICAL AND ELECTRONICS ENGINEERING,REC 21
CS2312-OBJECT ORIENTED PROGRAMMING LABORATORY
Pr. N".=
AREA OF RECTANGLE AND CIRCLE
AIM:
To calculate area of rectangle and circle using interfaces.
REBUIREMENTS:
Fard,are I /entium 'T /C
oft,are I J2^1.!
PROGRAM:
+<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
/r.5o.* 2ateI
=43= $F 43CT=5603 =52 C'4C03
5ame I
4eg.5o. I
$bjective I To create t,o classes and to implement interfaces.
<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<+
interface =rea
O
final static float piB!.1#FJ
float compute8float -,float "9J
P
class 4ectangle implements =rea
O
public float compute8float -,float "9
O
return8-<"9J
P
P
DEPARTMENT OF ELECTRICAL AND ELECTRONICS ENGINEERING,REC 22
CS2312-OBJECT ORIENTED PROGRAMMING LABORATORY
class Circle implements =rea
O
public float compute8float -, float "9
O
return8pi<-<-9J
P
P
class 'nterfaceTest
O
public static void main8tring argsMN9
O
4ectangle rectBne, 4ectangle89J
Circle cirBne, Circle89J
=rea arJ
arBrectJ
"stem.out.println8V=rea of 4ectangleBV+ar.compute811,2199J
arBcirJ
"stem.out.println8V=rea of CircleBV+ar.compute811,199J
P
P
COMPILATION AND E@ECUTION:
2IWjavaprgHjavac 'nterfaceTest.java
2IWjavaprgHjava 'nterfaceTest
OUTPUT:
=rea of 4ectangleB211.1
=rea of CircleB!1#.1

DEPARTMENT OF ELECTRICAL AND ELECTRONICS ENGINEERING,REC 23

Anda mungkin juga menyukai