Download as pdf or txt
Download as pdf or txt
You are on page 1of 16

SRINIVAS INSTITUTE OF TECHNOLOGY

Merlapadavu, Mangalore
Department of MCA
( .NET LABORATORY – 16MCA57)
PART-A
LAB 1: Write a Program in C# to demonstrate Command line arguments processing for the
following.
a) To find the square root of a given number.
b) To find the sum & average of three numbers.

using System;
using System.IO;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Lab1
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("\n=====COMMAND LINE PROGRAM======");
if (args.Length == 0)
{
Console.WriteLine("No arguments submitted");
Console.ReadLine();
return;
}
Int32 x = 0, a = 0, b = 0, c = 0, sum = 0, avg = 0;
try
{
x = int.Parse(args[0].ToString());

Console.WriteLine("square root of a number :


{0}",+Math.Sqrt(x));
a = int.Parse(args[1].ToString());
b = int.Parse(args[2].ToString());
c = int.Parse(args[3].ToString());
sum = a + b + c;
avg = sum / 3;
Console.WriteLine("sum of three number :{0}", +sum);
Console.WriteLine("avg of three number :{0}", +avg);
Console.ReadLine();
return; }
catch
{
Console.WriteLine("Invalid input");
Console.ReadLine();
return;
}
}
}
}

OUTPUT:

LAB 2: Write a Program in C# to demonstrate the following


a) Boxing and Unboxing
b) Invalid Unboxing.

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

namespace Lab2
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("========Boxing & UnBoxing========");
List<object> mixedList = new List<object>();
mixedList.Add("\nFirst Group");
for (int j = 1; j < 5; j++)
{
mixedList.Add(j); //Boxing
}
mixedList.Add("\nSecond Group:");
for (int j = 6; j < 10; j++)
{
mixedList.Add(j); //Boxing
}

foreach (var item in mixedList)


{
Console.WriteLine(item);
}
var sum = 0;
for (var j = 1; j < 5; j++)
{
sum += (int)mixedList[j] * (int)mixedList[j]; //UnBoxing
}
Console.WriteLine("\nSquare sum of First Group is:" + sum);
sum = 0;
for (var j = 6; j < 10; j++)
{
sum += (int)mixedList[j] * (int)mixedList[j]; //UnBoxing
}
Console.WriteLine("\nSquare sum of Second Group is:" + sum);
Console.WriteLine("\n============================");
Console.WriteLine("ININVALID UNBOXING");
Console.WriteLine("\n============================");

float i = 123;
object o = i; // implicit boxing

try
{
int j = (int)o; // attempt to unbox

System.Console.WriteLine("Unboxing OK.");
}
catch (System.InvalidCastException e)
{
System.Console.WriteLine("{0} Error: Incorrect
unboxing.", e.Message);
}
Console.ReadKey();
} }}
OUTPUT:

LAB 3: Write a program in C# to add Two complex numbers using Operator overloading.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace LAB3
{
public struct Complex
{
public int real;
public int imaginary;

public Complex(int real, int imaginary)


{
this.real = real;
this.imaginary = imaginary;
}

public static Complex operator +(Complex c1, Complex c2)


{
return new Complex(c1.real + c2.real, c1.imaginary +
c2.imaginary);
}
public override string ToString()
{
return (String.Format("{0} + {1}i", real, imaginary));
}
}

class Program
{
static void Main()
{
Complex num1 = new Complex(4, 5);
Complex num2 = new Complex(2, 4);
Complex sum = num1 + num2;
Console.WriteLine("First Complex Number : {0}", num1);
Console.WriteLine("Second Complex Number : {0}", num2);
Console.WriteLine("The Sum of the Two Numbers : {0}", sum);
Console.ReadLine();
}
}
}

OUTPUT:
LAB 4: Write a Program in C# to find the sum of each row of given jagged array of 3 inner
arrays.

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

namespace lab4
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("=====JAGGED ARRAY=====");
int i = 0, j = 0, sum = 0, size1 = 0, size2 = 0, size3 = 0;
int[][] Jarray = new int[3][];
Console.WriteLine("Jagged array of three inner arrays is
created");
Console.WriteLine("-------------------");
Console.WriteLine("Enter the size for first inner array:");
size1 = int.Parse(Console.ReadLine());
Jarray[0] = new int[size1];
Console.WriteLine("-------------------");
Console.WriteLine("Enter the size for second inner array:");
size2 = int.Parse(Console.ReadLine());
Jarray[1] = new int[size2];
Console.WriteLine("-------------------");
Console.WriteLine("Enter the size for third inner array:");
size3 = int.Parse(Console.ReadLine());
Jarray[2] = new int[size3];
Console.WriteLine("-------------------");
Console.WriteLine("Enter the elements for first inner
array:");
i = 0;
for (j = 0; j < size1; j++)
{
Jarray[i][j] = int.Parse(Console.ReadLine());
sum = sum + Jarray[i][j];
}
Console.WriteLine("------------------");
Console.WriteLine("Enter the elements for second inner
array:");
i = 1;
for (j = 0; j < size2; j++)
{
Jarray[i][j] = int.Parse(Console.ReadLine());
sum = sum + Jarray[i][j];
}
Console.WriteLine("------------------");
Console.WriteLine("Enter the elements for third inner
array:");
i = 2;
for (j = 0; j < size3; j++)
{
Jarray[i][j] = int.Parse(Console.ReadLine());
sum = sum + Jarray[i][j];
}
Console.WriteLine("------------------");
Console.WriteLine("Sum of all the elements of the Jagged
array is:" + sum);
Console.WriteLine("======================================");
Console.ReadLine();

}
}
}

OUTPUT:
LAB 5: Write a Program in C# to demonstrate Array Out of Bound Exception using Try,
Catch and Finally blocks.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Lab5
{
class Program
{
static int GetInt(int[] array,int index)
{
try{
return array[index];
}
catch(System.IndexOutOfRangeException e)
{
Console.WriteLine("Array Exception Handling here");
System. Console.WriteLine(e.Message);
Console.WriteLine("----------------------------------");
throw new System.ArgumentOutOfRangeException("Index
parameter is out of range", e);

}
}
static void Main(string[] args)
{
Console.WriteLine("====EXCEPTION HANDLING===");
try
{
int[] arr = { 1, 2, 3, 4, 5}; //GIVE 6 VALUES FOR OUTPUT 2
int k = GetInt(arr, 5);
Console.WriteLine("The value of given index is:{0}", k);
Console.WriteLine("---------------------------------");
throw new System.FieldAccessException();
int value = 1 / int.Parse("0");
}
catch (System.DivideByZeroException e)
{
System.Console.WriteLine(e.Message);
}

catch (System.ArgumentOutOfRangeException e2)


{
Console.WriteLine(e2.Message);
}
catch (System.FieldAccessException e3)
{
Console.WriteLine("-----Field Access Exception-----");
Console.WriteLine(e3.Message);
Console.WriteLine("---------------------------------");
}
catch (Exception ex)
{
Console.WriteLine("Exception by superclass:Exception");
Console.WriteLine("---------------");
Console.WriteLine("Message={0}", ex.Message);
Console.WriteLine("Source={0}", ex.Source);
Console.WriteLine("StackTrace={0}", ex.StackTrace);
Console.WriteLine("TargetSite={0}", ex.TargetSite);
Console.WriteLine("---------------------------------");
}
finally{
Console.WriteLine("Always Executed");
Console.WriteLine("---------------------------------");
}
Console.ReadLine();
}
}
}

OUTPUT 1:
OUTPUT 2:

LAB 6: Write a Program to Demonstrate Use of Virtual and override key words in C# with a
simple program.

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

namespace Lab_Ex_13
{
class super
{
protected int x;
public super(int x)
{
this.x = x;
}
public virtual void display()
{
Console.WriteLine("\n Super x = " + x);
}
}
class sub : super
{
int y;
public sub(int x, int y)
: base(x)
{
this.y = y;
}
public override void display()
{
Console.WriteLine("\n Sub x = " + x); // OR base.display();
Console.WriteLine("\n Sub y = " + y);
}
}
class overridetest
{
public static void Main()
{
sub s = new sub(100, 200);
s.display();
Console.ReadLine();
}
}
}

OUTPUT:
LAB 7: Write a Program in C# to create and implement a Delegate for any two arithmetic
operations.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
delegate int numberChange(int a);

namespace @delegate
{
class Program
{
static int num = 20;
public static int Add(int p)
{
num += p;
return num;
}
public static int Subtract(int q)
{
num -= q;
return num;
}

public static int getnum()


{
return num;
}
public static void Main(string[] args)
{
Console.WriteLine("----------Calculator-----------");
Console.WriteLine("1.Addition");
Console.WriteLine("2.Subtraction");
Console.WriteLine("3.Exit");
numberChange nc1 = new numberChange(Add);
numberChange nc2 = new numberChange(Subtract);
while (true)
{
Console.WriteLine("Enter the choice:");
int ch = int.Parse(Console.ReadLine());

switch (ch)
{
case 1:
Console.WriteLine("Enter the number:");
int n = int.Parse(Console.ReadLine());
nc1(n);
Console.WriteLine("Value of num in Addition
:{0}", getnum());
break;
case 2:
Console.WriteLine("Enter the number:");
int m = int.Parse(Console.ReadLine());
nc2(m);
Console.WriteLine("Value of num in
Subtraction:{0}", getnum());
break;
case 3:
Environment.Exit(0);
break;
default:
Console.WriteLine("Enter the valid option");
break;
}
}
}
}
}

OUTPUT:
LAB 8: Write a Program in C# to demonstrate abstract class and abstract methods in C#.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace abstracts
{
abstract class Shape1
{
protected float R, L, B;
public abstract float Area();
public abstract float Circumference();
}
class Rectangle1 : Shape1
{
public void GetLB()
{
Console.Write("Enter Length:");
L = float.Parse(Console.ReadLine());
Console.Write("Enter Breadth:");
B = float.Parse(Console.ReadLine());
}
public override float Area()
{
return L * B;
}
public override float Circumference()
{
return 2 * (L + B);
}
class Circle1 : Shape1
{
public void GetRadius()
{
Console.Write("Enter the radius:");
R = float.Parse(Console.ReadLine());
}
public override float Area()
{
return 3.14F * R * R;
}
public override float Circumference()
{
return 2 * 3.14F * R;
}
}
class ClassAbstract
{
public static void Calculate(Shape1 s)
{
Console.WriteLine("Area:{0}", s.Area());
Console.WriteLine("Circumference:{0}",s.Circumference());
}
static void Main()
{
Console.WriteLine("===ABSTRACT CLASS AND METHODS====");
Console.WriteLine("\n---Rectangle---");
Rectangle1 r = new Rectangle1();
r.GetLB();
Console.WriteLine("-------------");
Calculate(r);
Console.WriteLine("\n----Circle----");
Circle1 c = new Circle1();
c.GetRadius();
Console.WriteLine("---------------");
Console.WriteLine("=================================");
Calculate(c);
Console.Read();
}
}
}
}

OUTPUT:
LAB 10: Write a Program in C# Demonstrate arrays of interface types (for runtime
polymorphism).
using System;
namespace Lab_10
{
interface abc
{
void xyz();
}
class sample : abc
{
public void xyz()
{
System.Console.WriteLine("In sample::xyz");
}
}
class CsIntf : abc
{
public void xyz()
{
System.Console.WriteLine("In CsIntf :: xyz");
}

static void Main(string[] args)


{
Console.WriteLine("****C# Interface Types Illustration****");
abc[] refabc = { new CsIntf(), new sample() };
for (int i = 0; i <= 1; i++)
refabc[i].xyz();
Console.WriteLine("......................................");
Console.ReadLine();
}
}
}

OUTPUT:

You might also like