21
13MCA57 - .NET Laboratory AMCEC Dept. of MCA Rajesh. N PART – A 1. Write a Program in C# to demonstrate Command line arguments processing. using System; namespace BATCH1 { class LAB1 { static void Main(string[] args) { int num, length, sum = 0; Console.WriteLine("The given command line args are:"); foreach (string n in args) { num = int.Parse(n); Console.WriteLine(num); sum = sum + num; } Console.WriteLine("The sum of given command line args is:" + sum); length = args.Length; Console.WriteLine("the reverse of given command line args is:"); for (int i = length - 1; i >= 0; i--) { num = int.Parse(args[i]); Console.WriteLine(num); } Console.ReadLine(); } } }

13MCA57 - .NET Laboratory PART A · 13MCA57 - .NET Laboratory AMCEC Dept. of MCA Rajesh. N PART – A 1. Write a Program in C# to demonstrate Command line arguments processing. using

Embed Size (px)

Citation preview

13MCA57 - .NET Laboratory

AMCEC Dept. of MCA Rajesh. N

PART – A

1. Write a Program in C# to demonstrate Command line arguments processing.

using System;

namespace BATCH1

{

class LAB1

{

static void Main(string[] args)

{

int num, length, sum = 0;

Console.WriteLine("The given command line args are:");

foreach (string n in args)

{

num = int.Parse(n);

Console.WriteLine(num);

sum = sum + num;

}

Console.WriteLine("The sum of given command line args is:" + sum);

length = args.Length;

Console.WriteLine("the reverse of given command line args is:");

for (int i = length - 1; i >= 0; i--)

{

num = int.Parse(args[i]);

Console.WriteLine(num);

}

Console.ReadLine();

}

}

}

13MCA57 - .NET Laboratory

AMCEC Dept. of MCA Rajesh. N

2. Write a Program in C# to demonstrate boxing and Unboxing. using System;

namespace BATCH1

{

struct Student

{

public string USN;

public string name;

public int totmarks;

}

class LABPGM2

{

static void Main(string[] args)

{

Student s;

Console.WriteLine("Enter USN:");

s.USN = Console.ReadLine();

Console.WriteLine("Enter the Student Name:");

s.name = Console.ReadLine();

Console.WriteLine("Enter the Student Total Marks:");

s.totmarks = int .Parse ( Console.ReadLine());

display(s);

Console.ReadLine();

}

public static void display(Object s1) //boxing

{

Student s2 = (Student)s1; //unboxing

Console.WriteLine(" STUDENT DETAILS");

Console.WriteLine("----------------");

Console.WriteLine("USN:{0}", s2.USN);

Console.WriteLine("Name:{0}", s2.name);

Console.WriteLine("Total Marks:{0}", s2.totmarks);

}

}

}

13MCA57 - .NET Laboratory

AMCEC Dept. of MCA Rajesh. N

3. Write a program to demonstrate Operator overloading.

using System;

namespace BATCH1

{

class Complex

{

private int x;

private int y;

public Complex()

{ }

public Complex(int i, int j)

{

x = i;

y = j;

}

public void ShowXY()

{

if (y > 0)

Console.WriteLine("{0}+i{1}", x, y);

else

Console.WriteLine("{0}-i{1}", x, Math.Abs(y));

}

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

{

Complex t = new Complex();

t.x = c1.x + c2.x;

t.y = c1.y + c2.y;

return t;

}

}

class LABPGM3

{

static void Main(string[] args)

{

int r1, i1, r2, i2;

13MCA57 - .NET Laboratory

AMCEC Dept. of MCA Rajesh. N

Console.WriteLine("\t\t C# Program for Operator Overloading");

Console.WriteLine("\t\t ----------------------------------");

Console.WriteLine("Enter the real and imaganry part for First Complex Number:");

r1 = int.Parse(Console.ReadLine());

i1 = int.Parse(Console.ReadLine());

Console.WriteLine("Enter the real and imaganry part for Second Complex Number:");

r2 = int.Parse(Console.ReadLine());

i2 = int.Parse(Console.ReadLine());

Complex c1 = new Complex(r1, i1);

Complex c2 = new Complex(r2, i2);

Console.Write("\t First Complex No:\t");

c1.ShowXY();

Console.Write("\t Second Complex No:\t");

c2.ShowXY();

Complex c3 = new Complex();

c3 = c1 + c2;

Console.Write("\n Sum of Two Complex No is:\t");

c3.ShowXY();

Console.ReadLine();

}

}

}

13MCA57 - .NET Laboratory

AMCEC Dept. of MCA Rajesh. N

4. Find the sum of all the elements present in a jagged array of 3 inner arrays. using System;

namespace BATCH1

{

class LABPGM4

{

static void Main(string[] args)

{

int sum = 0, i, j;

int[][] a; //Jaa

Console.WriteLine("Enter the Row size:");

int n = int.Parse(Console.ReadLine());

a = new int[n][];

for (i = 0; i < a.GetLength(0); i++)

{

Console.WriteLine("The row {0}", i + 1);

Console.WriteLine("--------------");

Console.WriteLine("Enter the inner columns size:");

int m = int.Parse(Console.ReadLine());

a[i] = new int[m];

Console.WriteLine("Enter the inner array elements:");

for (j = 0; j < a[i].Length; j++)

{

a[i][j] = int.Parse(Console.ReadLine());

}

}

Console.WriteLine("\n\n Jagged Array is:");

for (i = 0; i < a.GetLength(0); i++)

{

for (j = 0; j < a[i].Length; j++)

{

Console.Write(a[i][j] + " ");

sum = sum + a[i][j];

}

Console.WriteLine();

13MCA57 - .NET Laboratory

AMCEC Dept. of MCA Rajesh. N

}

Console.WriteLine("\n ---------------------");

Console.WriteLine("The Sum is:" + sum);

Console.WriteLine("---------------------");

Console.ReadKey();

}

}

}

13MCA57 - .NET Laboratory

AMCEC Dept. of MCA Rajesh. N

5. Using Try, Catch and Finally blocks write a program in C# to demonstrate error handling. using System;

namespace BATCH1

{

class LABPGM5

{

static int m = 10;

static int n = 0;

static void Division()

{

try

{

int k = m / n;

}

catch (ArgumentException e)

{

Console.WriteLine("Exception caught:" + e.Message);

}

finally

{

Console.WriteLine("Inside division method");

Console.ReadLine();

}

}

static void Main(string[] args)

{

try

{

Division(); //dividebyzero

}

catch (DivideByZeroException e)

{

Console.WriteLine("Exception caught:" + e.Message);

}

finally

13MCA57 - .NET Laboratory

AMCEC Dept. of MCA Rajesh. N

{

Console.WriteLine("inside main method");

Console.ReadLine();

}

}

}

}

13MCA57 - .NET Laboratory

AMCEC Dept. of MCA Rajesh. N

6. Demonstrate Use of Virtual and override key words in C# with a simple program.

using System;

namespace BATCH1

{

public class student

{

public virtual void getdata()

{

Console.WriteLine("Enter the name of the student:");

string name = Console.ReadLine();

Console.WriteLine("Enter the USN:");

string usn = Console.ReadLine();

}

}

class LABPGM6:student

{

public override void getdata()

{

base.getdata();

Console .WriteLine ("Enter the marks for differnt Subjects");

Console .WriteLine ("C#:");

int csharp=int.Parse (Console .ReadLine());

Console .WriteLine ("\n CPP:");

int cpp=int.Parse (Console .ReadLine ());

Console .WriteLine ("\n C:");

int c=int.Parse (Console .ReadLine ());

Console .WriteLine ("\n Total={0}",csharp +cpp +c );

}

static void Main()

{

// LABPGM6 obj = new LABPGM6 ();

//obj.getdata();

LABPGM6 obj1 = new LABPGM6();

obj1.getdata();

13MCA57 - .NET Laboratory

AMCEC Dept. of MCA Rajesh. N

Console.ReadLine();

}

}

}

13MCA57 - .NET Laboratory

AMCEC Dept. of MCA Rajesh. N

7. Write a program to demonstrate delegates.

using System;

namespace BATCH3

{

public delegate int DelegatSample(int a, int b);

public class SingleCastDelegate

{

public int Add(int x, int y)

{

return x + y;

}

public int Sub(int x, int y)

{

return x - y;

}

}

class LABPGM7a

{

static void Main(string[] args)

{

SingleCastDelegate sc = new SingleCastDelegate();

DelegatSample delgate1 = sc.Add;

int i = delgate1(10, 20);

Console.WriteLine("Demonstration of SingleCast Delegate");

Console.WriteLine("-------------------------------------");

Console.WriteLine("SUM Value:"+i);

DelegatSample delgate2 = sc.Sub;

int j = delgate2(20, 10);

Console.WriteLine("SUB Value:"+j);

Console.ReadKey();

}

}

}

13MCA57 - .NET Laboratory

AMCEC Dept. of MCA Rajesh. N

7. Write a program to demonstrate delegates.

using System;

namespace BATCH1

{

public delegate void MultiDelegate(int a, int b);

public class Sampleclass

{

public static void Add(int x, int y)

{

Console.WriteLine("Addition Value: " + (x + y));

}

public static void Sub(int x, int y)

{

Console.WriteLine("Subtraction Value: " + (x - y));

}

public static void Mul(int x, int y)

{

Console.WriteLine("Multiply Value: " + (x * y));

}

}

class LABPGM7b

{

static void Main(string[] args)

{

Console.WriteLine("Demonstration of Multicast Delegates");

Console.WriteLine("------------------------------------");

Sampleclass sc = new Sampleclass();

MultiDelegate del = Sampleclass.Add;

del += Sampleclass.Sub;

del += Sampleclass.Mul;

del(10, 5);

Console.ReadLine();

}

}

}

13MCA57 - .NET Laboratory

AMCEC Dept. of MCA Rajesh. N

8. Write a program to demonstrate abstract class and abstract methods in C#. using System;

namespace BATCH1

{

abstract class vehicle

{

protected float price;

protected string model;

public abstract double CalculateEMI(int months, double rate);

}

class car : vehicle

{

public car(int price, string model)

{

this.price = price;

this.model = model;

}

public override double CalculateEMI(int months, double rate)

{

double total = price + price * rate / 100;

double totalEMI = total / months;

return totalEMI;

}

public void display()

{

Console.WriteLine("Car Model={0}", model);

Console.WriteLine("Car Price={0}", price);

}

}

class LABPGM8

{

public static void Main()

{

car c = new car(500000, "Indica Vz");

13MCA57 - .NET Laboratory

AMCEC Dept. of MCA Rajesh. N

c.display();

Console.WriteLine("Enter the no.of months and interest rate");

int mnt = int.Parse(Console.ReadLine());

float rate = float.Parse(Console.ReadLine());

Console.WriteLine("The EMI for Car " + mnt + "Months:" + c.CalculateEMI(mnt, rate));

Console.ReadKey();

}

}

}

13MCA57 - .NET Laboratory

AMCEC Dept. of MCA Rajesh. N

9. Write a program to illustrate the use of different properties in C#.

using System;

namespace BATCH1

{

class Myclass

{

private int x;

private static int y;

public Myclass()

{

x = 100;

}

public int xproperty

{

get

{

return x;

}

set

{

x = value;

}

}

public int yproperty

{

get

{

return x;

}

}

public int z

{

set

{

13MCA57 - .NET Laboratory

AMCEC Dept. of MCA Rajesh. N

x = value;

}

}

public static int p

{

get

{

return y;

}

set

{

y = value;

}

}

public int display()

{

return x;

}

}

abstract class Myclass1

{

public int x;

public abstract int q

{

get;

set;

}

}

class concrete : Myclass1

{

public override int q

{

get

13MCA57 - .NET Laboratory

AMCEC Dept. of MCA Rajesh. N

{

return x;

}

set

{

x = value;

}

}

}

class LABPGM9

{

public static void Main()

{

Console.WriteLine("\n\n General Property------>");

Myclass mc1 = new Myclass();

mc1.xproperty = 10; //xproperty set block invoked

int val = mc1.xproperty; // xproperty get block invoked

Console.WriteLine("\nPrivate Member x={0}", val);

Myclass mc2 = new Myclass();

Console.WriteLine("\n Private member x:{0}", mc2.yproperty);

Console.WriteLine("\n\n\n Static Property -------->");

Myclass.p = 200;

val = Myclass.p;

Console.WriteLine("\n Private static member y:{0}", val);

Console.WriteLine("\n\n\n Write - Only Prperty-------->");

Myclass mc3 = new Myclass();

mc3.z = 20;

Console.WriteLine("\n Private member x:{0}", mc3.display());

Console.WriteLine("\n\n Abstract Property ----------->");

concrete mc4 = new concrete();

mc4.q = 1000;//set

val = mc4.q;//get

Console.WriteLine("\n Private Member x={0}", val);

Console.ReadLine();

13MCA57 - .NET Laboratory

AMCEC Dept. of MCA Rajesh. N

}

}

}

13MCA57 - .NET Laboratory

AMCEC Dept. of MCA Rajesh. N

10. Demonstrate arrays of interface types (for runtime polymorphism) with a C# program.

using System;

namespace BATCH1

{

public interface shape

{

void area();

}

public class Circle : shape

{

public void area()

{

Console.WriteLine("Caluclatig area of Circle");

Console.WriteLine("-------------------------");

Console.WriteLine("Enter the radius of Circle:");

double radius = double.Parse(Console.ReadLine());

double area = 3.14 * radius * radius;

Console.WriteLine("Area of Circle is:{0}", area);

}

}

public class Square : shape

{

public void area()

{

Console.WriteLine("\n\nCaluclatig area of Square");

Console.WriteLine("-----------------------------");

Console.WriteLine("Enter the side:");

float side = float.Parse(Console.ReadLine());

Console.WriteLine("Area of Square is:{0}", side * side);

}

}

class LABPGM10

{

public static void Main()

13MCA57 - .NET Laboratory

AMCEC Dept. of MCA Rajesh. N

{

shape[] findarea = new shape[2];

findarea[0] = new Circle();

findarea[1] = new Square();

for (int i = 0; i < findarea.Length; i++)

{

findarea[i].area();

}

Console.ReadKey();

}

}

}

13MCA57 - .NET Laboratory

AMCEC Dept. of MCA Rajesh. N