Monday, January 7, 2013

C-sharp lab programming

 

 

/ * Fahreneit to celcius * /

 

using System;

 

namespace ConsoleApplication1

{

           

            class fts

{

            static void Main(string[] args)

{

            double f, c;

            int counter=0;

            for (f = 0.0; f < 5.0; f++)

{

 

            c = 5.0 / 9.0 * (f - 32.0);

            Console.WriteLine(f + " degrees Fahrenheit is " +

            c + " degrees Celsius.");

 

            counter++;

               

            

}

            Console.ReadLine();

}

 

}

   

}

 

 

 

 

           

 

 

 

 

 

 

 

 

 

 

 

OUTPUT:-

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

           


 

PROGRAM TO DISPLAY DATE AND TIME USING CLASS IN C#

 

using System;

using System.Timers;

 

class myApp

 

{

            public static void Main()

                                                      

            {

                        Timer myTimer = new Timer();

                        myTimer.Elapsed += new ElapsedEventHandler( DisplayTimeEvent );

                        myTimer.Interval = 1000;

                        myTimer.Start();

                       

                        while ( Console.Read() != 'q' )

                             

                        {

                        }

            }

 

            public static void DisplayTimeEvent( object source, ElapsedEventArgs e )

   

            {

                        Console.Write("\r{0}", DateTime.Now);

            }

 

}

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

OUTPUT:-

 

 


 

/ * PROGRAM WHICH USES INHERITANCE IN C# * /

 

using System;

namespace ConsoleApplication6

{

            class Room

            {

                        public int length;

                        public int breadth;

                        public Room(int x,int y)

                        {

                                    length=x;

                                    breadth=y;

                        }

                        public int Area()

                        {

                                    return(length*breadth);

                        }

            }

           

            class BedRoom:Room

            {

                        int height;

                        public BedRoom(int x,int y, int z):base(x,y)

                        {

                                    height=z;

                        }

                        public int Volume()

                        {

                                    return(length*breadth*height);

                        }

            }

            class Inhertest

            {

                        public static void Main(string[] args)

                        {

                                    BedRoom room1=new BedRoom(14,12,10);

                                    int area1=room1.Area();

                                    int volume1=room1.Volume();

                                    Console.WriteLine("Area1="+area1);

                                    Console.WriteLine("Volume1="+volume1);

                                    Console.Read();

                        }

            }

}

 

 

 

OUTPUT:-

 

 


 

PROGRAM TO DEMONSTRATE EXCEPTION HANDLING IN C#

 

using System;

 

namespace ConsoleApplication1

{

            class Execp

            {

                        public static void Main()

                        {

                                    int a=10;

                                    int b=5;

                                    int c=5;

                                    int x,y;

                                    try

                                    {

                                                x=a/(b-c);

                                    }

                                    catch(Exception e)

                                    {

                                                Console.WriteLine("Division by zero");

                                    }

                                    y=a/(b+c);

                                    Console.WriteLine("y="+y);

                                    Console.Read();

                        }

            }

}

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

OUTPUT:-

 


 

/ * PROGRAM USING ABSTRACT CLASS IN C# * /

 

using System;

namespace ConsoleApplication1

{          

            public abstract class Shape

            {

                        protected string color;

 

                        public Shape(string color)

                        {

                                    this.color = color;

                        }

 

                        public string getColor()

                        {

                                    return color;

                        }

                        public abstract double getArea();

 

            }

 

            public class Circle : Shape

            {

                        private double radius;

 

                        public Circle(string color, double radius) : base(color)

                        {

                                    this.radius = radius;

                        }

 

                        public override double getArea()

                        {

                                    return System.Math.PI * radius * radius;

                        }

            }

 

            public class Square : Shape

            {

                        private double sideLen;

 

                        public Square(string color, double sideLen) : base(color)

                        {

                                    this.sideLen = sideLen;

                        }

 

                        public override double getArea()

                        {

                                    return sideLen * sideLen;

                        }

            }

 

 

 

public class Example3

{

static void Main()

{

Shape myCircle = new Circle("orange", 3);

Shape mySquare = new Square("green", 4);

System.Console.WriteLine("This circle is " + myCircle.getColor()

                                    + " and its area is " + myCircle.getArea() + ".");

System.Console.WriteLine("This square is " + mySquare.getColor()

                                    + " and its area is " + mySquare.getArea() + ".");

Console.Read();

            }

            }          

}

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

OUTPUT:-

 


 

           

 

             PROGRAM USING CONSTRUCTOR AND DESTRUCTOR

 

 

 

using System;

 

namespace ConsoleApplication2

{

            class rectangle

            {

                        public int length,width;

                        public rectangle(int x,int y)

                        {

                                    length=x;

                                    width=y;

                        }

                        public int area()

                        {

                                    return(length*width);

                        }

                        ~rectangle()

                        {

                                    Console.WriteLine("length "+length+"width"+width);

                        }

            }

   

 

            class rectanglearea

            {

                        static void Main(string[] args)

                        {

                                    rectangle rect1=new rectangle(15,10);

                                    int area1=rect1.area();

                                    Console.WriteLine("Area="+area1);

                                    Console.Read();

                        }

 

            }

}

 

 

 

 

 

 

 

 

 

 

 

 

 

OUTPUT:-

 

 

 

 

 

 


 

/ * PROGRAM USING INTERFACE * /

using System;

namespace ConsoleApplication2

{

            interface addition

            {

                        int add();

            }

            interface multiplication

            {

                        int mul();

            }

                        class computation:addition,multiplication

                        {

                                    int x,y;

                                    public computation(int x,int y)

                                    {

                                                this.x=x;

                                                this.y=y;

                                    }

                                    public int add()

                                    {

                                                return(x+y);

                                    }

                                    public int mul()

                                    {

                                                return(x*y);

                                    }

                        }

                        class interfacetest1

                        {

                                    public static void Main()

                                    {

                                                computation com=new computation(10,20);

                                                addition ad=(addition) com;

                                                Console.WriteLine("sum="+ad.add());

                                                multiplication mu=(multiplication)com;

                                                Console.WriteLine("product="+mu.mul());

                                                Console.Read();

                                    }

                        }                                              

}

 

 

 

 

 

OUTPUT:-

 

 


 

/ * TO DISPLAY SIMPLE FORM USING WINDOWS APLLICATION * /

 

 

using System.Windows.Forms;

 

            public class Form1 : System.Windows.Forms.Form

            {

                        private System.Windows.Forms.Label label1;

                        private System.Windows.Forms.Button button1;

                        public Form1()

                        {

                                    InitializeComponent();

                        }

 

                        #region Windows Form Designer generated code

                        private void InitializeComponent()

                        {

                                    this.label1 = new System.Windows.Forms.Label();

                                    this.button1 = new System.Windows.Forms.Button();

                                    this.SuspendLayout();

                                    this.label1.Font = new System.Drawing.Font("Microsoft Sans Serif", 14.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));

                                    this.label1.Location = new System.Drawing.Point(120, 80);

                                    this.label1.Name = "label1";

                                    this.label1.Size = new System.Drawing.Size(120, 23);

                                    this.label1.TabIndex = 0;

                                    this.label1.Text = "WELCOME";

                                    this.button1.Location = new System.Drawing.Point(136, 160);

                                    this.button1.Name = "button1";

                                    this.button1.TabIndex = 1;

                                    this.button1.Text = "Exit";

                                    this.button1.Click += new System.EventHandler(this.button1_Click);

                                   

                                    this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);

                                    this.ClientSize = new System.Drawing.Size(400, 326);

                                    this.Controls.Add(this.button1);

                                    this.Controls.Add(this.label1);

                                    this.Name = "Form1";

                                    this.Text = "Form1";

                                    this.Load += new System.EventHandler(this.Form1_Load);

                                    this.ResumeLayout(false);

 

                        }

                       

0 comments:

Post a Comment