Tuesday, September 26, 2017

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

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace IntrDemo {
    public interface Shape
    {
        void area();
    }
        public class circle : Shape
        {
            public void area()
            {
            Console.WriteLine("------Calculating area of CIRCLE--------");
            Console.Write("Enter the radius : ");
            float r = float.Parse(Console.ReadLine());
            Console.WriteLine("Area of Circle" + (3.142) * r * r);
            }
        }
        public class Square : Shape
        {
            public void area()
            {
            Console.WriteLine("------Calculating area of Square--------");
            Console.Write("Enter the length : ");
            float side = float.Parse(Console.ReadLine());
            Console.WriteLine("Area of square" +(side*side));
        }
     }
        class program
        {
            static void Main(string[] args)
            {
            Console.WriteLine("------Calculating area of Square--------");
            Shape[] s = new Shape[2];
            s[0] = new circle();
            s[1] = new Square();
                for (int i = 0; i < s.Length; i++)
                {
                s[i].area();
                Console.ReadLine();
                }
            }
        }
}

9 . WRITE A PROGRAM TO ILLUSTRATE THE USE OF DIFFERENT PROPERTIES IN C#.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Properties
{
class point
{
int getx, gety;
public int x
{
get { return getx; } set {
getx = value; }
}
public int y
{
get { return gety; } set {
gety = value; }
}
}
class Program
{
static void Main(string[] args)
{
point start = new point();
point end = new point();
start.x = 10;
start.y = 20;
end.x = 100;
end.y = 200;
Console.Write( "\npoint 1 : "+ start.x + " " + end.x);
Console.Write("\npoint 2 :"  + start.y + " " + end.y);
Console.ReadLine();
}
}
}

8 WRITE A PROGRAM TO DEMONSTRATE ABSTRACT CLASS AND ABSTRACT METHODS IN C#.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Lab8
 {
abstract class person
  {
protected string fname;
protected string lname;
public person(string fn, string ln)
{
fname = fn;
lname = ln;
}
public abstract void display() ;
}

class emp : person
{
public ushort year;
public emp(string fn, string ln, ushort yr): base(fn, ln)
{
year = yr;
}
public override void display()
  {
Console.WriteLine("Employee :" + fname + " " + lname + " " + year);
}
}
class worker : person
 {
    public String company;
             public worker(string fn, string ln, string c) : base(fn, ln)
{
company = c;   
}   
public override void display() { 
Console.WriteLine("Worker :" + fname + " " + lname + " " + company); 
}   
}   
class Program   
{   
         static void Main(string[] args) 
{
Console.WriteLine("**ABSTRACT CLASS  AND ABSTRACT METHODS DEMO **"); 
person p2 = new emp("RAM", "KUMAR", 2012); 
person p3 = new worker("RAM", "KUMAR", "ABC TECH SOLS"); 
p2.display();   
p3.display();   
Console.ReadLine();   
}   
}   
}    

7 Write a program to demonstrate delegates.

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

namespace _6
{
    public delegate int DelegateSample(int a,int b);
    public class sampleclass
    {
        public int add(int x, int y)
        {
            return x + y;
        }
        public int sub(int x, int y)
        {
            return x - y;
        }
        class program {
            static void Main(String[] args)
            {
                sampleclass s = new sampleclass();
                DelegateSample del = s.add;
                int i = del(10, 20);
                Console.WriteLine("Add result is"+i);
                DelegateSample del1 = s.sub;
                int j = del1(10, 2);
                Console.WriteLine("Sub result is" + j);
                Console.ReadLine();

            }
        }
    }
}

6. 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 VirtualKey
{
class person
{
protected string fname; protected string lname;
public person(string fn, string ln)
{
fname = fn; lname = ln;
}
public virtual void display()
{
Console.WriteLine("Person  :" + fname + " " + lname);
}
}
class emp : person
{
public ushort year;
public emp(string fn, string ln, ushort yr) : base(fn, ln)
{
year = yr;
}
public override void display()
{
Console.WriteLine("Employee :"+fname+" "+lname+" "+year);
}
}
class worker : person
{
public String company;
public worker(string fn, string ln, string c):base(fn, ln)
{
company=c;
}
public override void display()
{
Console.WriteLine("Worker :" + fname + " " + lname + " " +company);
}
}
class Program
{
static void Main(string[] args)
{
Console.WriteLine("\n\n*** VIRTUAL AND OVERRIDE KEYWORDS DEMO ***");
person p1 = new person("RAVI", "KUMAR"); person p2 = new emp("RAVI", "KUMAR",2012);
person p3 = new worker("RAVI", "KUMAR","XYZ SOLUTIONS"); p1.display();
p2.display();
p3.display();
Console.ReadLine();
}
}
}

Sunday, September 3, 2017

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

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

namespace ConsoleApplication14
{
    class Program
    {
        public static void Main(string[] args)
       {
            try
            {
           
                int zero = 0;
                Console.WriteLine("In try block: attempting division by zero");
                int myInt = 1 / zero;
                Console.WriteLine("You never see this message!");
            }
            catch
            {
             
                Console.WriteLine("In catch block: an exception was thrown");
            }
            finally
            {
                Console.WriteLine("In finally block: do any cleaning up here");
                Console.ReadKey();
            }

        }
    }
}


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

namespace ConsoleApplicationavi11
{
    class Lab11
    {
        public static void Main()
        {
            try
            {
                int zero = 0;
                Console.WriteLine("In try block: attempting division by zero");
                int myInt = 1 / zero;
                Console.WriteLine("You never see this message!");
            }
            catch
            {
                Console.WriteLine("In catch block:   an exception was thrown");
            }
            finally
            {
                Console.WriteLine("In finally block:   do any cleaning up here");
            }
            Console.ReadLine();
        }
    }
}

4. Find 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;

namespace lab4
{
    public class prgm4
    {
        public static void Main()
        {
            int[][] jag = new int[3][];
            jag[0] = new int[2];
            jag[1] = new int[4];
            jag[2] = new int[3];
            int[] sum = new int[3];
            int result = 0;
            int i, j;
            for (i = 0; i < 3; i++)
                sum[i] = 0;
            Console.WriteLine("Enter the size of the array ");
            for (i = 0; i < jag.Length; i++)
            {
                Console.WriteLine("Enter the values off array " + (i + 1) + " jag:");
                for (j = 0; j < jag[i].Length; j++)
                {
                    jag[i][j] = int.Parse(Console.ReadLine());
                }
            }
            for (i = 0; i < jag.Length; i++)
            {
                for (j = 0; j < jag[i].Length; j++)
                {
                    sum[i] = sum[i] + jag[i][j];
                }
            }
            for (i = 0; i < 3; i++)
                result = result + sum[i];
            Console.WriteLine("The result of individual jags row-wise: ");
            for (i = 0; i < 3; i++)
                Console.Write(sum[i] + "\t");
            Console.WriteLine();
            Console.WriteLine("The final sum of all the elements in the jagged array:" + result);
            Console.Read();
        }
    }
}