Saturday, June 5, 2010

Classes and Objects in C#


using System;


using System.Collections.Generic;


using System.Linq;


using System.Text;


 


namespace Test_in_cs


{


    class cone


    {


        private float height, radius;


        public cone(float h, float r)


        {


            height = h;


            radius = r;


        }


        public void setdata(float h, float r)


        {


            height = h;


            radius = r;


        }


        public void displaydata()


        {


            Console.WriteLine("Height= " + height);


            Console.WriteLine("Radius= " + radius);


        }


        public void volume()


        {


            float v;


            v = (1 / 3.0f) * 3.14f * radius * radius * height;


            Console.WriteLine("Volume= " + v);


        }


    }


    class Program


    {


        static void Main(string[] args)


        {


            //Class instantiation and member calling.


            cone c1 = new cone(10.0f, 3.5f);


            cone c2 = new cone(20.0f, 6.2f);


            c1.displaydata();


            c1.volume();


            c2.displaydata();


            c2.volume();


            Console.WriteLine("\nData after setting:");


            c1.setdata(100.0f,100.0f);


            c1.displaydata();


            //Class instantiation and member calling.


            Console.Write("Enter the string: ");


            String s2 = Console.ReadLine();


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


            {


                if (s2[i] == ' ')


                {


                    s2 = s2.Remove(i, 1);


                    s2 = s2.Insert(i,"space");


                }


            }


            Console.WriteLine(s2);


        }


    }


}

No comments:

Post a Comment