Tuesday, December 13, 2011

Program to explain classes and objects in C-Sharp


//This program creates two vehicle objects.
using System;
class Vehicle
{
    public int passengers, fuelcap, mpg;
}
class Twovehicle
{
    static void Main()
    {
        Vehicle bus = new Vehicle();
        Vehicle car = new Vehicle();

        int range1, range2;

        // Assign values to fields in bus
        bus.passengers = 25;
        bus.fuelcap = 16;
        bus.mpg = 21;

        // Assign values to fields in bus
        car.passengers = 4;
        car.fuelcap = 14;
        car.mpg = 12;
       
        //compute the ranges assuming a full tank of gas
        range1 = bus.fuelcap * bus.mpg;
        range2 = car.fuelcap * car.mpg;

        Console.WriteLine("Bus can carry " + bus.passengers + " with a range of " + range1);
        Console.WriteLine("Car can carry " + car.passengers + " with a range of " + range2);
    }
}

Pythagorean theorem to find the length of the hypotenuse given the lengths of the two opposing sides using C-sharp


// Pythagorean theorem to find the length of the hypotenuse given the lengths of the two opposing sides.
using System;
class Hypo
{
    static void Main()
    {
        double x, y, z;

        x = 3;
        y = 4;

        z = Math.Sqrt((x * x) + (y * y));
        Console.WriteLine("The Hypotenuse is " +z);
    }
}

Program to display conversion table of inches to meters in C-Sharp


//This program displays a conversion table of inches to meters and prints a blank line after each 12 count.
using System;
class InchtoMeterTable
{
    static void Main()
    {
        double inches, meters;
        int counter;

        counter = 0;
        for (inches = 1.0; inches <= 144.0; inches++)
        {
            //convert to meters
            meters = inches / 39.37;
            Console.WriteLine(inches + " inches is " + meters + " meters.");
            counter++;

            //Every 12th line, print a blank line.
            if (counter == 12)
            {
                Console.WriteLine();
                counter = 0; //reset the line counter
            }

        }
    }
}

Program to compute your weight on moon in C-Sharp


// Compute your weight on moon
using System;
    class moon
    {
        static void Main()
        {
            double earthweight; // wieght on earth
            double moonweigth; // wieght on moon

            earthweight = 165.0;
            moonweigth = earthweight * 0.17;
            Console.WriteLine(earthweight + " earth-pounds is equivalent to " + moonweigth + " moon-pounds.");
        }
    }

Program to convert Fahrenheit to Celsius in C-Sharp


/* Program to convert Fahrenheit to Celsius */
using System;
class FtoC
{
    static void Main()
    {
        double f;
        double c;
        f = 59.0;
        c = 5.0 / 9.0 * (f - 32.0);
        Console.Write(f + " degrees fahrenheit is ");
        Console.WriteLine(c + " degree celsius.");
    }
}

Google : The top most search engine

Google : MAGIC BOX

nRelate - Posts and Homepage

LinkWithin

Related Posts Plugin for WordPress, Blogger...

Which is the toughest subject ?