Wednesday, July 30, 2014

Understanding Up Casting and Down Casting (C# reference)

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

namespace Apps
{

    class Doctor1
    {
        protected string name, specialization;
        public Doctor1(string name, string specialization)
        {
            this.name = name;
            this.specialization = specialization;
        }
        public void Print()
        {
            Console.WriteLine(name);
            Console.WriteLine(specialization);
        }
    }
    class ResidentalDoctors1 : Doctor1
    {
        protected int salary;
        public ResidentalDoctors1(string name, string specialization, int salary)
            : base(name, specialization)
        {
            this.salary = salary;
        }
        public new void Print()
        {
            base.Print();
            Console.WriteLine(salary);
        }
        public int GetPay()
        {
            return salary;
        }
    }
    class Consultant1 : Doctor1
    {
        protected int fees, visitNum;
        public Consultant1(string name, string specialization, int fees, int visitNum)
            : base(name, specialization)
        {
            this.fees = fees;
            this.visitNum = visitNum;
        }
        public new void Print()
        {
            base.Print();
            Console.WriteLine(fees);
            Console.WriteLine(visitNum);
        }
        public int GetPay()
        {
            return fees * visitNum;
        }
    }
    class UnderstandingUpDownCasting
    {
        public static void Main()
        {
            //ResidentalDoctors rd = new ResidentalDoctors("Steve", "General", 100);
            //Consultant c = new Consultant("Chitti", "General", 300, 10);

            Doctor1 d = new Consultant1("Chitti", "General", 300, 10);  // Upcasting
            Consultant1 c;
            c = (Consultant1)d; //Downcasting


            //Console.WriteLine(d.GetPay());
            //Console.WriteLine(c.GetPay());
            //rd.Print();
           // c.Print();
        }
    }
}

Program to Copy the file contents from one file to copy part of line in another file throughout



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

namespace Apps
{
    class FileContentSeperation
    {
        static void Main(string[] args)
        {
            //FileStream fs = new FileStream() 
            StreamReader sfilecontent1 = new StreamReader(@"D:\Practice Applications\Srikanth Tech\File1.txt");
            StreamWriter dfilecontent2 = new StreamWriter(@"D:\Practice Applications\Srikanth Tech\File2.txt");
            string line;
            //Console.WriteLine(line);
            while ((line = sfilecontent1.ReadLine()) != null)
            {
                string[] words = line.Split(',');
                string k = words[1];
                dfilecontent2.Write(k.Trim() + "\r\n");
            }
            sfilecontent1.Close();
            dfilecontent2.Close();
        }
    }
}

Program to find Fibanocci Series (C# reference)

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

namespace Apps
{

    class FibanocciServiesClass
    {
        private int temp;
        public void Fibanocci(int j)
        {
            int a = 1;
            int b = 0;
            if (j == 0)
                Console.WriteLine(0);
            else
            {
                Console.Write(b+"\t");
                Console.Write(a + "\t");

                while (temp < j)
                {
                    temp = a + b;
                    b = a;
                    a = temp;
                    if (temp < j)
                    Console.Write(temp + "\t");
                }
            }
        }
    }
    class FibanocciSeries
    {
        public static void Main()
        {
            Console.WriteLine("Enter a number");
            int i = Int32.Parse(Console.ReadLine());
            FibanocciServiesClass s1 = new FibanocciServiesClass();
            s1.Fibanocci(i);
        }
    }
}

Program to find the Armstrong Number

Consider a number 153. The number has three digits so it becomes the power of each digit. Example: 1^3 + 5^3 + 3^3 = 153. The resultant after calculation is equal to the number itself(Armstrong Number).

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

namespace Apps
{
    class ArmstrongNumberM
    {
        //private int number;
        private int count = 0, tempnum = 0, armstrong = 0,i=0;
        private int[] digits = new int[5];
        public int CountDigits(int num)
        {

            // count digits
            tempnum = num;
            while (tempnum % 10 > 0)
            {
                digits[count] = tempnum % 10;
                tempnum = tempnum / 10;
                count++;
            }
            int s = Evaluator();
            return s;
        }
        public int Evaluator()   // Scope of this method is within countDigits() only so calling this method in it.
        {
            for (int i = 0; i < count; i++)
            {
                armstrong += (int)Math.Pow((double)digits[i], (double)count);
            }
            return armstrong;


        }
    }
    class ArmstrongNumber
    {
        public static void Main()
        {
            Console.WriteLine("Enter the Number to Check for Armstrong or not");
            ArmstrongNumberM s1 = new ArmstrongNumberM();
            //  Console.WriteLine(s1.CountDigits(Int32.Parse(Console.ReadLine())));        // Passing the input to count number of digits method
            int numberentered = int.Parse(Console.ReadLine());
            int returnvalue = s1.CountDigits(numberentered);
            if (numberentered == returnvalue)
            {
                Console.WriteLine("Entered number is Armstrong Number");
            }
            else
                Console.WriteLine("Entered number is NOT Armstrong Number");
        }
    }
}