You are on page 1of 30

B.M.

S COLLEGE OF ENGINEERING
(Autonomous College under VTU)
Bull Temple Road, Bangalore-560019

DEPARTMENT OF COMPUTER APPLICATIONS

Lab report on
Windows Application Development
with C#.NET (18MCA5PCWP)

Submitted to fulfill the requirements of


5th Semester MCA

Submitted by:
N NEHA (1BM19MCA28)

Under the Guidance of


Assistant Professor
Dr Ch Ram Mohan Reddy
1
B.M.S COLLEGE OF ENGINEERING
(Autonomous College under VTU)
Bull Temple Road, Bangalore-560019

DEPARTMENT OF COMPUTER APPLICATIONS

CERTIFICATE
This is to certify that N NEHA(1BM19MCA28) has satisfactorily Windows
Application Development with C#.NET (18MCA5PCWP) completed the
requirements Laboratory Report prescribed by 4th semester MCA course,
BMS College of Engineering during the year 2020-2021.

Signature of the Internal Guide


TABLE OF CONTENTS
S.NO

1. Programs to demonstrate Boxing and Unboxing.

2. Program to demonstrate the sum of all the elements present in a jagged array of
3 inner arrays.

3. Programs to demonstrate Classes and Objects

4. Programs to illustrate the use of different properties in C#

5. Programs to demonstrate Exploring Structs.

6. Programs to demonstrate Exception Handling.

7. Program to demonstrate inheritance covering the concepts of Sealed


Classes, Sealed Methods, Extension Methods
8. Programs to demonstrate Abstract Classes and Interfaces.

9. Programs to demonstrate Polymorphism covering the concepts of


Overloading, Overriding, Virtual and Override keywords

10. Programs to demonstrate on Operator Overloading

11. Programs to demonstrate Delegates and Event Handlers.

12. Programs to demonstrate Language Integrated Query (LINQ).

Program1:Programs to demonstrate Boxing and Unboxing


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace P1_Lab1
{
class Program
{
static void Main(string[] args)
{

int num = 23;


object obj = num;
num=100;
Console.WriteLine("Boxing in c# by converting int to object");
Console.WriteLine("Value of num is : " + num);
Console.WriteLine("Value of object is : " + obj);
Console.WriteLine("UnBoxing in c# by converting object to int");
obj=num;
int l=(int) obj;
Console.WriteLine("Value of ob object is : " + obj);
Console.WriteLine("Value of l is : " + l);

}
}

}
OUTPUT:

PROGRAM2:Program to demonstrate the sum of all the elements present in a jagged


array of 3 inner arrays
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Program_Lapprogram2
{
class Program
{
static void Main(string[] args)
{
int r = 3,sum=0;
Console.WriteLine("Sum of all the elements present in a jagged array of 3 inner arrays");
//r=Convert.ToInt32(Console.ReadLine());
int[][] arr = new int[r][];
for (int i = 0; i < r; i++)
{
Console.WriteLine("How many number of elements");
int m = Convert.ToInt32(Console.ReadLine());
arr[i] = new int[m];
Console.WriteLine("Enter" + m + "elements");
for (int j = 0; j < m; j++)
{

arr[i][j] = int.Parse(Console.ReadLine());
sum = sum + arr[i][j];
}
}
Console.WriteLine("The sum of elements in a jagged array of 3 inner arrays are " +
sum);

}
}
}

OUTPUT:
Program 3: Programs to demonstrate Classes and Objects.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Program_Labprogram3
{
class Program
{
int insuranceId, carnum;
String carname, color;
void getCarDetails()
{
Console.WriteLine("Enter the insuranceId,car number,color,name");
insuranceId = Convert.ToInt32(Console.ReadLine());
carnum = Convert.ToInt32(Console.ReadLine());
color = Console.ReadLine();
carname = Console.ReadLine();
}
void Display()
{
Console.WriteLine("Car Details");
Console.WriteLine(" ");
Console.WriteLine("Car InsuranceID: " + insuranceId);
Console.WriteLine("Car Number: "+carnum);
Console.WriteLine("Car Color: " + color);
Console.WriteLine("Car Name: "+carname);

}
static void Main(string[] args)
{
Program p = new Program();
Console.WriteLine("Classes and objects");
p.getCarDetails();
p.Display();
}
}
}

OUTPUT:
PROGRAM4:Programs to illustrate the use of different properties in C#

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Program_Labprogram4
{
class Program
{
public String carname;
private int insuranceId=0;
private static int count=0;
public string Car
{
get
{
return carname;
}
set
{
carname = value;

}
}
public override string ToString()
{

return ("Car Name: " + carname);


}
public Program(int PropVal)
{
insuranceId =Convert.ToInt32( PropVal);

}
public int Insurance
{
get
{
return insuranceId;
}

}
public Program()
{
count++;
}
public static int Carnum
{
get
{
return count;
}
set
{
count = value;
}
}
private string[] names = new string[2];
public string this[int i]
{
get
{
return names[i];
}
set
{
names[i] = value;
}
}
static void Main(string[] args)
{
Console.WriteLine("Different properties in C#");
Console.WriteLine(" ");
Program p = new Program();
p.carname = "FORD";
Console.WriteLine("GETTER AND SETTER METHODS");
Console.WriteLine(p);
Console.WriteLine(" ");
Console.WriteLine("ReadOnly Property");
Program p1 = new Program(1234);
Console.WriteLine("Car InsuranceId: " + p1.insuranceId);
Console.WriteLine(" ");
Console.WriteLine("Static Property");
Program p2 = new Program();
Program p3 = new Program();
Console.WriteLine("Number of Objects of Program class using Static property:{0}" +
Program.Carnum);
Console.WriteLine(" ");
Console.WriteLine("Indexer Property");
Program p4 = new Program();
p4[0] = "Rakshitha";
p4[1] = "MCA,BMSCE";
for (int i = 0; i < 2; i++)
{
Console.WriteLine(p4[i]);
}
}
}
}
OUTPUT:

5. Programs to demonstrate Exploring Structs


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace PROGRAM_LAB5
{
struct area
{
public int l;
public int b;
public area(int l,int b)
{

this.l = l;
this.b = b;

}
public int display()
{
return l * b;
}

};
class Program
{
static void Main(string[] args)
{
area m = new area();
Console.WriteLine("Enter breadth and length of rectangle");
int a = Convert.ToInt32(Console.ReadLine());
int b = Convert.ToInt32(Console.ReadLine());
m.l = a;
m.b = b;
Console.WriteLine("Area of rectangle is {0}", m.display());

}
}
}
6. Programs to demonstrate Exception Handling
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace PROGRAM_LAB6
{
class Program
{
int n = 0,a=3,c;
int[] j ={1,2};
Program()
{
}
public void divide()
{
try
{
c = a / n;
c = j[2];

}
catch (DivideByZeroException e)
{
Console.WriteLine("Cant divide by Zero"+e);
}
catch (IndexOutOfRangeException e1)
{
Console.WriteLine("Index out of range"+e1);
}

}
static void Main(string[] args)
{
Program p = new Program();
p.divide();
}
}
}

USERDEFINED EXCEPTION:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

public class NumberIsZeroException : Exception


{
public NumberIsZeroException(string message) : base(message) { }
}

public class Program


{
int a = 0;
public void display()
{
if (a == 0)
{
throw (new NumberIsZeroException("Zero value found"));
}
}
}
namespace PROGRAM_LAB6
{

public class Program1


{
static void main(string args)
{
Program p = new Program();
try
{
p.display();
}
catch (NumberIsZeroException e)
{
Console.WriteLine("Number divide By zero UserDefined Exception");
}
}

}
}
7.Program to demonstrate inheritance covering the concepts of Sealed Classes, Sealed
Methods, Extension Methods

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace RakshithaLabprogram7
{
class Program1
{
virtual public void add(int a,int b)
{
Console.WriteLine("ADDITION IS" + (a + b));
}
virtual public void sub(int a, int b)
{
Console.WriteLine("Subtraction is" + (a - b));
}
}
class Program2:Program1{
sealed override public void add(int a, int b)
{
Console.WriteLine("Multiplication is"+(a*b));
}
override public void sub(int a, int b)
{
Console.WriteLine("Subtraction is" + (a - b));
}

}
sealed class Program3:Program2{
/* override public void add(int a, int b)
{
Console.WriteLine("Subtraction is"+(a-b));
}
*/
override public void sub(int a, int b)
{
Console.WriteLine("Subtraction is" + (a - b));
}
}
class Program{
static void Main(string[] args)
{
Program1 p=new Program1();
p.add(10,4);
p=new Program2();
p.add(8,9);
p=new Program3();
p.sub(10, 8);
Console.ReadLine();
}
}

}
8. Programs to demonstrate Abstract Classes and Interfaces
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ProgramLab8
{
interface I1
{
void numbernegative(int a);

}
abstract class M1
{
abstract public void numberpositive(int b);
}
class P:M1,I1
{

override public void numberpositive(int b)


{
if(b>0){
Console.WriteLine("Number is positive");
}

}
public void numbernegative(int a)
{
if (a < 0)
{
Console.WriteLine("Number is negative");
}

}
/*class P2 : I1
{
public void numbernegative(int a)
{
if (a < 0)
{
Console.WriteLine("Number is negative");
}

}
}*/
class Program
{
static void Main(string[] args)
{
Console.WriteLine("ABSTRACT CLASSES AND INTERFACES");
int a, b;
Console.WriteLine();
Console.WriteLine("Enter 2 values");
a = Convert.ToInt32(Console.ReadLine());
b = Convert.ToInt32(Console.ReadLine());
P m = new P();
m.numberpositive(a);
m.numbernegative(b);
Console.ReadLine();

}
}
}
9. Programs to demonstrate Polymorphism covering the concepts of Overloading,
Overriding, Virtual and Override keywords
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ProgramLab9
{
class Perimeter
{

virtual public void display(){


Console.WriteLine("Base Class");
}
}
class Square:Perimeter{
public void findPerimeter(double a)
{
Console.WriteLine("Sqquare Perimeter is"+(a*a));
}
public override void display()
{
Console.WriteLine("Deriverd Class 1");
}
}
class Rectangle:Square{
public void findPerimeter(double a,double b)
{
double s=Convert.ToDouble(2*(a+b));
Console.WriteLine("Rectangle Perimeter is"+s);
}
public override void display()
{
Console.WriteLine("Deriverd Class 2");
}
}
class Triangle:Rectangle{
public void findPerimeter(double a,double b,double c)
{

Console.WriteLine("Triangle Perimeter is" + (a + b + c));


}
public override void display()
{
Console.WriteLine("Deriverd Class 2");
}

}
class Program{
static void Main(string[] args)
{

Square h = new Square();


h.display();
Rectangle g = new Rectangle();
g.display();
Triangle t=new Triangle();
t.findPerimeter(10,2);
t.findPerimeter(10,2,3);
t.findPerimeter(10);
t.display();
Console.ReadLine();
}
}}

10.Programs to demonstrate on Operator

Overloading

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Program_lab10
{
class Box
{
private double length;
private double breadth;
private double height;
public double getVolume()
{
return length * breadth * height;
}
public void setLength(double len)
{
length = len;
}
public void setBreadth(double bre)
{
breadth = bre;
}
public void setHeight(double hei)
{
height = hei;
}
// Overload + operator to add two Box objects.
public static Box operator +(Box b, Box c)
{
Box box = new Box();
box.length = b.length + c.length;
box.breadth = b.breadth + c.breadth;
box.height = b.height + c.height;
return box;
}
public static bool operator ==(Box lhs, Box rhs)
{
bool status = false;
if (lhs.length == rhs.length && lhs.height == rhs.height && lhs.breadth ==
rhs.breadth)
{
status = true;
}
return status;
}
public static bool operator !=(Box lhs, Box rhs)
{
bool status = false;
if (lhs.length != rhs.length || lhs.height != rhs.height || lhs.breadth != rhs.breadth)
{
status = true;
}
return status;
}
public static bool operator <(Box lhs, Box rhs)
{
bool status = false;
if (lhs.length < rhs.length && lhs.height < rhs.height && lhs.breadth < rhs.breadth)
{
status = true;
}
return status;
}
public static bool operator >(Box lhs, Box rhs)
{
bool status = false;
if (lhs.length > rhs.length && lhs.height >
rhs.height && lhs.breadth > rhs.breadth)
{
status = true;
}
return status;
}
public static bool operator <=(Box lhs, Box rhs)
{
bool status = false;
if (lhs.length <= rhs.length && lhs.height
<= rhs.height && lhs.breadth <= rhs.breadth)
{
status = true;
}
return status;
}
public static bool operator >=(Box lhs, Box rhs)
{
bool status = false;
if (lhs.length >= rhs.length && lhs.height
>= rhs.height && lhs.breadth >= rhs.breadth)
{
status = true;
}
return status;
}
public override string ToString()
{
return String.Format("({0}, {1}, {2})", length, breadth, height);
}
}
class Tester
{
static void Main(string[] args)
{
Box Box1 = new Box(); // Declare Box1 of type Bov
Box Box2 = new Box(); // Declare Box2 of type Box
Box Box3 = new Box(); // Declare Box3 of type Box
Box Box4 = new Box();
double volume = 0.0; // Store the volume of a box here
// box 1 specification
Box1.setLength(6.0);
Box1.setBreadth(7.0);
Box1.setHeight(5.0);
// box 2 specification
Box2.setLength(12.0);
Box2.setBreadth(13.0);
Box2.setHeight(10.0);
//displaying the Boxes using the overloaded ToString(): Console.WriteLine("Box
1: {0}", Box1.ToString()); Console.WriteLine("Box 2: {0}", Box2.ToString());
// volume of box 1
volume = Box1.getVolume();
Console.WriteLine("Volume of Box1 : {0}", volume);
// volume of box 2
volume = Box2.getVolume();
Console.WriteLine("Volume of Box2 : {0}", volume);
// Add two object as follows:
Box3 = Box1 + Box2;
Console.WriteLine("Box 3: {0}", Box3.ToString());
// volume of box 3
volume = Box3.getVolume();
Console.WriteLine("Volume of Box3 : {0}", volume);
//comparing the boxes
if (Box1 > Box2)
Console.WriteLine("Box1 is greater than Box2");
else
Console.WriteLine("Box1 is not greater than Box2");
if (Box1 < Box2)
Console.WriteLine("Box1 is less than Box2");
else
Console.WriteLine("Box1 is not less than Box2");
if (Box1 >= Box2)
Console.WriteLine("Box1 is greater or equal to Box2");
else
Console.WriteLine("Box1 is not greater or equal to Box2");
if (Box1 <= Box2)
Console.WriteLine("Box1 is less or equal to Box2");
else
Console.WriteLine("Box1 is not less or equal to Box2");
if (Box1 != Box2)
Console.WriteLine("Box1 is not equal to Box2");
else
Console.WriteLine("Box1 is not greater or equal to Box2");
Box4 = Box3;
if (Box3 == Box4)
Console.WriteLine("Box3 is equal to Box4");
else
Console.WriteLine("Box3 is not equal to Box4");
Console.ReadKey();
}

}
}

11.Programs to demonstrate Delegates and Event Handlers.


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Program_Lab11
{
public class Program
{
public delegate void DelegateEventHandler();
public static double previous_amount;
public static void Deposit()
{

Console.WriteLine("Enter the amount to grab the bonus");


previous_amount = Convert.ToDouble(Console.ReadLine());
Console.WriteLine("Current Bank Balance: " +
previous_amount);
}
public static void Reward()
{
if (previous_amount >= 5000)
{
double bonus = 500;
Console.WriteLine("Bonus Applied, Bank Balance: " +
(previous_amount + bonus));
}
else
{
Console.WriteLine("No Bonus Applied");
}
}
public static event DelegateEventHandler deh;
public static void Main(string[] args)
{
deh += new DelegateEventHandler(Deposit);
deh += new DelegateEventHandler(Reward);
deh.Invoke();
Console.ReadLine();
}
}
}

12. Programs to demonstrate Language Integrated Query (LINQ).

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApplicationLINQ
{
class Program
{
static void Main(string[] args)
{
try
{
Console.WriteLine("\n\tUsage of LINQ");
int a;
Console.Write("Enter the size of marks board : ");
a = int.Parse(Console.ReadLine());
int[] marks = new int[a];
Console.WriteLine("Enter the marks : ");
for (int i = 0; i < marks.Length; i++)
{
marks[i] = Convert.ToInt32(Console.ReadLine());
}
IEnumerable<int> marksQuery = from score in marks where score > 80 select score;
Console.Write("Marks above 80 ");
foreach (int i in marksQuery)
{
Console.Write(i + " ");
}
Console.WriteLine("\n\n***************************\n\n");
}
catch (Exception e)
{
Console.WriteLine(e.Message);
}
Console.ReadLine();
}
}
}

You might also like