Upload
sreenupes
View
230
Download
1
Embed Size (px)
Citation preview
8/15/2019 C# Lab Manual for BCA.doc
1/34
PES University
DEPARTMENT OF BCA
.NET LABORATORY MANUAL
SUBCODE: UC14BC254
SEMESTER: ! SEMESTER
PREPARED BY:
P.SREENIVAS, ASST.PROFESSOR
DEPARTMENT OF MCA, PESIT
8/15/2019 C# Lab Manual for BCA.doc
2/34
.Net Laboratory
Subject Code: UC14BC254 Hrs / week: 02
Total Hours: 26 (1C)
Sl.
No.
Exercises Time
1. Getting Started with Technology and writing basic programs like Hello
world in C#.
2 Hours
2. Programs in C# to demonstrate the usage o Classes ! "bects. 2 Hours
$. %rite a Program in C# to demonstrate the usage o &ecision making'
(ooping ! branching.
2 Hours
). %rite Programs to demonstrate the concepts o *rrays' +ultidimensional
*rrays and ,agged *rrays
2 Hours
-. %rite Programs in C# to demonstrate boing and /nboing 2 Hours
0. %rite Programs in C# to demonstrate Single (eel nheritance' +ultileel
nheritance
2 Hours
3. %rite a program to &emonstrate interaces in C#. 2 Hours
4. %rite programs in C# to demonstrate "perator oerloading' 5irtual and
"erride 6eywords' *bstract class and *bstract +ethods
2 Hours
7. %rite a program to illustrate the use o dierent properties in C#
89:C*PS/(*T":;.%rite a Program in C# to demonstrate the usage o ndeers
2 Hours
1*T":S8:umerical ormatting' Console " o both number !
strings;
2 Hours
11. /sing Try' Catch and ?inally blocks write a program in C# to demonstrate
9ception Handling
2 Hours
12. %rite a program in C# to demonstrate o Simple delegates without
eents.
%rite a Program in C# to demonstrate &elegates with 9ents
2 Hours
1$. ?inal Test 2 Hours
8/15/2019 C# Lab Manual for BCA.doc
3/34
"ettin# st$rte% &it' C(
1. Write a Program in C# to display a simple message “Hello World”
using System;
class demonstrate{
public static void ain!"
{
Console.Writeine!“Hello $orld”";
Console.%eadine!";
&
&
8/15/2019 C# Lab Manual for BCA.doc
4/34
'. Write a Program in C# to demonstrate t(e usage o) Classes * +b,ects.
using System;
namespace Console-pplication1
{ class Program
{
static void ain!string/ args"
{ Car car; car 0 ne$ Car!%ed";
Console.Writeine!car.2escribe!"";
car 0 ne$ Car!3reen";
Console.Writeine!car.2escribe!"";
Console.%eadine!"; & &
class Car
{
private string color; public Car!string color"
{ t(is.color 0 color; &
public string 2escribe!"
{ return 4(is car is 5 Color; &
public string Color
{
get { return color; &
set { color 0 value; & &
&&
8/15/2019 C# Lab Manual for BCA.doc
5/34
6. Write a Program in C# to demonstrate t(e usage o) 2ecision ma7ing8 looping *
branc(ing.
De)isi*n M$+in#
using System;
public class 9:ample
{
static void ain!"{ int a 0 8 b 0 ';
int result 0 a < b;
i) !result 00 '"
{ Console.Writeine!%esult is '"; &
i) !result 00 6" { Console.Writeine!%esult is 6"; &
Console.%eadine!";
&&
S&it)' St$te,ent
using System;
class Program{ static void ain!" { int : 0 6;
s$itc( !:" { case 1=
Console.Writeine!: is e>ual to 1"; brea7;
case '= Console.Writeine!: is e>ual to '"; brea7;
case 6= goto de)ault;
de)ault= Console.Writeine!: is e>ual to neit(er 1 nor '"; brea7; &&&
&
8/15/2019 C# Lab Manual for BCA.doc
6/34
using System;
namespace 9:amples1
{
class Program
{
static void ain!string/ args"
{
int num8 i8result;
Console.Write!9nter a number?t";
num 0 Convert.4o@nt6'!Console.%eadine!"";
)or !i 0 1; i A0 1B; i55"
{
result 0 num i;
Console.Writeine!{B& : {1& 0 {'&8 num8 i8result";
&
Console.%eadine!";
&
&
&
--D*/'i0e L**
using System;
class Program
{
static void ain!"
{
int/ ids 0 ne$ int/ { D8 E8 F8 1B &;
int sum 0 B;
int i 0 B;
do
{
sum 50 idsi/;
i55;
& $(ile !i A G";
System.Console.Writeine!sum";
8/15/2019 C# Lab Manual for BCA.doc
7/34
Console.%eadine!";
&
&
8/15/2019 C# Lab Manual for BCA.doc
8/34
{b8 )&8
&;
Console.Writeine!arrayB8 B/";
Console.Writeine!arrayB8 1/";
Console.Writeine!array18 B/";
Console.Writeine!array18 1/";
&
&
..............................
using System;
public class @nit-rray
{ static void ain!"
{ int/ array 0 ne$ int/;
arrayB/ 0 1;
array1/ 0 ';
array'/ 0 6;
array6/ 0 G;
arrayG/ 0 ;
)or !int i 0 B; i A array.engt(; i55"
{ Console.Writeine!arrayi/";
& &&
8/15/2019 C# Lab Manual for BCA.doc
9/34
int sum 0 B;
int// : 0 ne$ int6//;
:B/0ne$ int6/;
:B/0ne$ intG/;
:B/0ne$ int/;
)or !int ,0B; ,A0:.engt(; ,55"
{ )or !int 70B; 7A0:,/.engt(; 755"
{
Console.Writeine !enter t(e5!751"5elements o) t(e 5
!,51"5ro$";
: ,8 7/ 0 int.Parse !Console.%eadine!"";
&
&
)or !int ,0B; ,A0:.engt(; ,55"
{
Console.Writeine !";
)or !int 70B; 7A0:,/.engt(; 755"
{
Console.Write !:,87/5?t";
& &
Console.Writeine !";
)or !int ,0B; ,A0:.engt(; ,55"
{
)or !int 70B; 7A0:,/.engt(; 755"
{ sum 50 : ,8 7/;
& & Console.Writeine !4(e sum o) all t(e elements are {B&8sum";
Console.%eadine!"; &
&
. Write a Program in C# to demonstrate Ko:ing and Lnbo:ing
using System;using System.Collections.3eneric;using System.in>;using System.4e:t;
namespace bo:ing{
8/15/2019 C# Lab Manual for BCA.doc
10/34
public struct Point { public int :8 y; public Point!int a8 int b" { : 0 a; y 0 b; & public void display!" { Console.Writeine!:0{B&8 y0{1&8 :8 y"; & & class Program { static void ain!string/ args" { Point p1 0 ne$ Point!'8 6"; Console.Writeine!2@SP-I@MI@M3 N+% S4%LC4 4IP9S"; p1.display!";
ob,ect o1 0 p1; Console.Writeine!o1.3et4ype!""; Point p' 0 !Point"o1; p'.display!";
Console.Writeine!2@SP-I@MI@M3 N+% P%@@4@O9 4IP9S";Console.Writeine!bo:ing";
@nt : 0 1B;ob,ect o 0 !ob,ect":;
8/15/2019 C# Lab Manual for BCA.doc
11/34
public void display1!"{System.Console.Writeine!good noon";&&class test{public static void ain!"{b :0ne$ b!";:.display!";:.display1!";&&M30tiLeve0 n'erit$n)eusing System;namespace @n(erit{class S(ape{public S(ape!"{Console.Writeine!Constructor o) base class S(ape";&&class Polygon = S(ape{public Polygon!"{Console.Writeine!Constructor o) Kase class Polygon";&&class Quad = Polygon{public Quad!"{Console.Writeine!Contructor o) class Quad";&public static void ain!string/ args"{Quad ob,0 ne$ Quad!"; &&&
E.2emonstrate an inter)ace $it( a C# program.
using System;
inter)acecylinder
{
void area !";
&
class test=cylinder
{
void area!"
8/15/2019 C# Lab Manual for BCA.doc
12/34
{
double pi06.1G';
Console.Writeine !9nter t(e radius and (eig(t";
double r8(8res;
r 0 double.Parse !Console.%eadine !"";
( 0 double.Parse !Console.%eadine !"";
res 0 !' pi r (" 5 !' pi r r";
Console.Writeine !-rea is5res"; &
&
class ainClass
{
public static void ain !string/ args"
{
cylinder c0ne$ test!"
c.area!";Console.%eadine!";
&
&
F. Write a program in C# to demonstrate +perator overloading.using System;using System.Collections.3eneric;using System.in>;using System.4e:t; class Comple: {
public int real; public int imaginary; int a8 b8 c;
public Comple:!int real8 int imaginary" { t(is.real 0 real; t(is.imaginary 0 imaginary; & public Comple:!int :8 int y8 int R"
8/15/2019 C# Lab Manual for BCA.doc
13/34
{ a 0 :; b 0 y; c 0 R; &
public static Comple: operator 5!Comple: c18 Comple: c'" { return ne$ Comple:!c1.real 5 c'.real8 c1.imaginary 5 c'.imaginary"; &
public override string 4oString!" { return !String.Normat!{B& 5 {1&i8 real8 imaginary""; & public static Comple: operator 55!Comple: op1" { op1.a55; op1.b55; op1.c55; return op1; & public void S(o$4(e%esult!" { Console.Writeine!a 5 8 5 b 5 8 5 c"; Console.%eadine!"; &
public static void ain!" { Comple: num1 0 ne$ Comple:!'8 6"; Comple: num' 0 ne$ Comple:!68 G";
Comple: c6 0 ne$ Comple:!1B8 'B8 6B"; c655;
8/15/2019 C# Lab Manual for BCA.doc
14/34
using System.in>;using System.4e:t;
namespace O@%4L-{ class s(apes { public virtual void points!" { Console.Writeine!-ll t(e geo Jgures"; &
& class (e:agon = s(apes { public override void points!" { Console.Writeine!(e:a (as D points"; & & class circle = s(apes { public override void points!" { Console.Writeine!circle (as B points"; & & class Program { static void ain!string/ args"
{
s(apes /s0{ ne$ s(apes!"8 ne$ (e:agon!"8ne$ circle!"&;
)or!int i0B;iAs.engt(;i55" { si/.points!"; & Console.%eadine!"; & &&
F c" Write a program to demonstrate abstract class and abstract met(ods in C#.
using System;
abstract class service{ public abstract void detail!"; &class employee=service{
8/15/2019 C# Lab Manual for BCA.doc
15/34
String name; int number8 salary;
public override void detail!" {
Console.Writeine!9nter employee name "; name0Console.%eadine!"; Console.Writeine !9nter employee number"; number0int.Parse!Console.%eadine!""; Console.Writeine !9nter employee salary";
salary0int.Parse!Console.%eadine!""; Console.Writeine !Mame?tnumber?tsalary"; Console.Writeine !name 5 ?t 5 number 5 ?t 5 salary"; &
&&class test{ public static void ain!"
{ employee b 0 ne$ employee!";
b.detail!";&&
. Write a program to illustrate t(e use o) diTerent properties in
C#!9MC-PSL-4@+M".
using System;
class Person
{
private string myMame 0M
8/15/2019 C# Lab Manual for BCA.doc
16/34
{
myMame 0 value;
&
&
public int -ge
{
get
{
return my-ge;
&
set
{
my-ge 0 value;
&
&
static int count0B;
public static int Count
{
get
{
count55;
return count;
&
&
public override string 4oString!"
{
return Mame 0 5 Mame 5 8 -ge 0 5 -ge;
&
public static void ain!"
{
Console.Writeine !LS@M3 394 -M2 S94 P%+P9%4@9S";
Person person 0 ne$ Person!";
Console.Writeine!Person details {B&8 person";
person.Mame 0 oe;
person.-ge 0 ;
8/15/2019 C# Lab Manual for BCA.doc
17/34
Console.Writeine!Person details {B&8 person";
person.-ge 50 1;
Console.Writeine!Person details {B&8 person";
Console.Writeine !";
Console.Writeine !LS@M3 S4-4@C P%+P9%4@9S";
Console.Writeine!Person.Count";
Console.Writeine!Person.Count";
Console.Writeine!Person.Count";
&
&
. Write a program in C# to demonstrate @M29U9%S.
using System;using System.Collections;
class yClass
{ private string /data 0 ne$ string/;
public string t(is int inde:/
{
get
{
return datainde:/;
&
set
{
datainde:/ 0 value;
& &
8/15/2019 C# Lab Manual for BCA.doc
18/34
&
class yClient
{
public static void ain!"
{
yClass mc 0 ne$ yClass!";
mcB/ 0 %a,es(;
mc1/ 0 -61'D;
mc'/ 0 Sne(adara;
mc6/ 0 Kang;
mcG/ 0 umbai;
Console.Writeine!{B&8{1&8{'&8{6&8{G&8mcB/8mc1/8mc'/8mc6/8mcG/";
& &
1B. Write a program to implement C+MS+9 @
8/15/2019 C# Lab Manual for BCA.doc
19/34
{
Console.Writeine!{B=B.BB&8 1";
Console.Writeine!{B=#.##&8 B.'6G";
Console.Writeine!{B=#####&8 1'6G.DE";
Console.Writeine!{B=!B#" ### ## ##&8 '6G''";
Console.Writeine!{B=##&8 B.'6G";
&
&
V...........................................
8/15/2019 C# Lab Manual for BCA.doc
20/34
11. Lsing 4ry8 Catc( and Ninally bloc7s $rite a program in C# to demonstrate error(andling.
using System;
namespace e:ception(andling
{
class ainClass
{
public static void ain !string/ args"
{
intnumbers 0 ne$ int'/;
try
{
numbersB/ 0 '6;
numbers1/ 0 6';
numbers'/ 0 G';
)oreac(!int i in numbers"
Console.Writeine!i"; &
catc(!@nde:+ut+)%ange9:ception e:"
{
Console.Writeine!-n inde: $as out o) rangeX?n5 e:.essage";
&
catc(!9:ception e:"
{
8/15/2019 C# Lab Manual for BCA.doc
21/34
Console.Writeine!Some sort o) error occured= 5 e:.essage";
&
Jnally
{
Console.Writeine!@tYs t(e end o) our try bloc7.";
&
Console.%eadine!";
&
&&
1'. Write a program to demonstrate o) Simple delegates $it(out events.using System;
namespace delegatespgm
{
public delegate void met(ods!";
class test
{
public void strconcat!"
{
Console.Writeine!enter t(e string1andstring'";
string s10Console.%eadine!";
string s'0Console.%eadine!";
string s6 0 s1 5 s'; Console.Writeine !S4%@M3 C+MC-4 @S 5 s65?n";
&
public void strreplace!"
{
string s6 0 Oisual C# 9:press;
s60s6.%eplace!C#8 Kasic";
Console.Writeine !S4%@M3 %9P-C9 @S 5 s65?n";
&
public void strlo$er!"
{
string s6 0 pesit bangalore;
s60s6.4oo$er!";
Console.Writeine !S4%@M3 +W9% @S 5 s65?n";
&
&
8/15/2019 C# Lab Manual for BCA.doc
22/34
class ainClass
{
public static void ain !string/ args"
{
test t1 0 ne$ test !";
met(ods m0ne$ met(ods!t1.strconcat";
m50t1.strreplace;
m50t1.strlo$er;
m!"; & &&
Pr*#r$, *n EventsDe0e#$tes.
using System;
namespace yevents
{
public class eventtestclass {
private int nvalue;
public delegate void delvalue!";
public event delvalue c(anged;
protected virtual void callevent!"{
i)!c(angedX0null" c(anged!";
else
Console.Writeine!9vent Jred but no (andler";
&
public eventtestclass!int n"
{ setvalue!n"; &
public void setvalue!int nv"
{
i)!nvalueX0nv"
{
nvalue0nv;
callevent!";
&&
&
class program
{ static void ain!"
{
eventtestclass etc0ne$ eventtestclass!6";
etc.setvalue!";
etc.setvalue!";
etc.setvalue!6";
Console.%eadine!";
&&
8/15/2019 C# Lab Manual for BCA.doc
23/34
&
nstr3)ti*ns t* &*r+ &it' C(.NET
&ownload the 5isual studio 2
8/15/2019 C# Lab Manual for BCA.doc
24/34
"nce the S" image is created' mounded the &isk. This S" image auto run will launch theinstallation. This the irst screen o the installation.
Click on the install +icrosot 5isual Studio 2
8/15/2019 C# Lab Manual for BCA.doc
25/34
Click :et.
8/15/2019 C# Lab Manual for BCA.doc
26/34
*ccept the license terms and click net.
8/15/2019 C# Lab Manual for BCA.doc
27/34
Select any o the check bo' this will enable CustomiDe button at the bottom let corner.
Select the customiDe button to select what components you want to install. you select
both the check boes all the components will be selected automatically.
you want you can speciy the path where this should be installed' but am leaing as it is.*nd click installE
8/15/2019 C# Lab Manual for BCA.doc
28/34
@e patient E this will take looooooong timeEbecause this will install a looooooong list o
itemsE
*ter installing +icrosot .:et ramework E.it will ask or a restartE.&onFt get panic pleasego ahead and allow it restart the system. *ter restarting the system' installation will startautomatically. "nce all the components were installed' you will get inish page.
ThatFs it. Click inish button. ou are ready to eplore the mush waited and ully loaded
http://geekswithblogs.net/Ramaraju/archive/2009/05/24/.Nethttp://geekswithblogs.net/Ramaraju/archive/2009/05/24/.Net
8/15/2019 C# Lab Manual for BCA.doc
29/34
isual Studio 2
8/15/2019 C# Lab Manual for BCA.doc
30/34
An*t'er &$y t* &*r+ &it' C(M*n*%eve0*
&eelopment Getting Started
tFs relatiely easy to get started working on +ono&eelop' but this page aims to make it
easier to get o the ground.
youFre on "S B you can also try IddsglFs one step
installJ httpsJ==github.com=ddsgl=monodeelopbuild
Setting up an 9nironment
+ono&eelop doesnFt reKuire the ery latest ersion o +ono' and we speciically depend onGT6L eatures no later that GT6L 2.4. That said' using a recent release o +ono and Gtk#
is likely to improe reliability and perormance.
It is strongly recommended to use a ac!aged release o" #ono or your distribution'
as it is ery easy to mi up conlicting +ono ersions i installing rom source. you mustinstall +ono rom source' set up a Parallel +ono 9nironment.
+ake sure you hae git installed' and check +ono&eelop out rom GitHub.
@uilding and nstalling
"pen a terminal in the topleel +ono&eelop directory' and run
.=conigure proileMcore
t may ail because o missing dependenciesN install them' and rerun the command.
There are a number o other proiles that can be usedN the command
.=conigure help
eplains how they are used.
:et' use
make
to build +ono&eelop.
Since youFre working on the deelopment ersion' itFs best not to install itN instead' you canuse
https://github.com/dvdsgl/monodevelop-buildhttp://www.mono-project.com/Parallel_Mono_Environmentshttps://github.com/mono/monodevelophttps://github.com/dvdsgl/monodevelop-buildhttp://www.mono-project.com/Parallel_Mono_Environmentshttps://github.com/mono/monodevelop
8/15/2019 C# Lab Manual for BCA.doc
31/34
make run
to run it without installing it.
t is a good idea to keep separate copies or using and deeloping.
you do install +ono&eelop' it is best to run the current ersion uninstalled' to make sureit works' beore installing it.
%orking on +ono&eelop
@eore hacking on +ono&eelop' donFt be araid to ask Kuestions on #monodeelop>C or +ono&eelop mailing list. People will be able to gie you pointer about where to start
and how best to approach the problems your are trying to sole. There are also numbero *rticles on the +ono&eelop architecture and on implementing addins.
The +ono&eelop solution can be opened rom +ono&eelop' and builds can be preormed
orm within +ono&eelop. ndeed' some parts o the build 8such as Stetic code generation;must be perormed within +&. Howeer' the modiied +ono&eelop must be run rom a
terminal with
make run
ou should ollow our contribution rules' or code style and licensing.
*ter you hae made your changes' commit them with a descriptie message and open apull reKuest on GitHub.
Troubleshooting the @uild
the +ono&eelop build ails' there are a number o possible ies.
the build commands ailed' try a clean rebuildJ
make cleanN make
and i any that are in conlict' or delete any that hae changed unnecessarily.
the build system ailed' check or changed iles and rerun the conigure script.
*& t* e6e)3te $ r*#r$, in C(.NET eit'er in!is3$0 St3%i*
Or M*n*Deve0*
http://mono-project.com/IRChttp://mono-project.com/IRChttp://lists.ximian.com/mailman/listinfo/monodevelop-listhttp://www.monodevelop.com/developers/articles/http://www.monodevelop.com/developers/#contributehttp://mono-project.com/IRChttp://mono-project.com/IRChttp://lists.ximian.com/mailman/listinfo/monodevelop-listhttp://www.monodevelop.com/developers/articles/http://www.monodevelop.com/developers/#contribute
8/15/2019 C# Lab Manual for BCA.doc
32/34
!is3$0 St3%i*1. +pen Oisual Studio i.e. Clic7 StartZProgramsZicroso)t OisualStudio 'B1BZicroso)t Oisual Studio 'B1B.'. Oisual Studio @nter)ace $ill open up and you $ill get a start page.6. Clic7 on Me$ Pro,ect.G. Nrom 4emplate Pane8 select Oisual C#8 select console application. Oisual studio $ill create a console application pro,ect $it( de)aultname li7e consoleapplication1D. Clic7 o7 a)ter you give name o) t(e console application !i) you$ant to c(ange )rom de)ault".
E. 4ype in t(e program in t(e de)ault editor $(at you get
F. Save t(e program.. 4o run t(e program Jrst you need to compile it. 4o compile t(eprogram8 select build menu and clic7 on build solution. @) t(ere areno errors8 you $ill get a message build success)ul.1B. @) compilation is success)ul8 you can e:ecute t(e program byclic7ing debug menu8 Start 2ebugging or Start $it(out 2ebugging.
M*n*Deve0*
8/15/2019 C# Lab Manual for BCA.doc
33/34
Open Ubuntu software center and type mono in the search box,now choose MonoDevelop package in the
list and Install.
In erminal,
sudo apt-get install monodevelop
!fter the installation start MonoDevelop,you should see an aesthetic programming environment.
o start programming select
"ile #$ %ew #$ "ile..
&elect '( and general
)uild the program*press "+
un the program*ctrl-"
8/15/2019 C# Lab Manual for BCA.doc
34/34
NOTE:his is very important the console in MonoDevelop /.0 does not read input.his is a bug in mono
and has already been reported.o execute programs with ead1ine statements please follow the
following steps*
!fter installing MonoDevelop
'reate the program using 2im editor,
vim Demo1.cs
• %ow install a package called mono#mcs,which is a c( compiler,
sudo apt-get install mono-mcs
• 'ompiling the program and generating .exe file,
mcs Demo1.cs
• 3xecuting the program,
mono Demo1.exe