Your Ad Here

Sunday, June 15, 2008

Implementing Polymorphism

/* The given C sharp example decalres abstract class Shape
* that implements polymorpism by overloading and
* overridding methods. The clas Shape2 consists
* of a virtual method ShapeType(). The method
* CalculateArea() is implemented twice by having
* different signatures and different return types.
* The class Square inherits the base class Shape2
* and overrides the base class method called ShapeType().
* The main() method creates an instance of the derieved
* class Square and the base class Shape2. the instance
* of the base class Shape2 invokes the method
* CalculateArea(). Depending on the parameter that
* the method CalcualteArea() takes, the apropiate
* method is called.
*/
using System;

namespace Inherit
{
class Shape2
{
public virtual void ShapeType()
{
Console.WriteLine("This message will be used to display type of the shape");
}
public int CalculateArea(int side)
{
return side * side;
}
public double CalculateArea(double baseBreadth, double height)
{
return 05 * baseBreadth * height;
}
}
class Square :Shape2
{
public override void ShapeType()
{
Console.WriteLine("A square is a Polygon");
}
public static void Main(string[] args)
{
Square sqr = new Square();
sqr.ShapeType();
Console.WriteLine("Area IS : "+sqr.CalculateArea(6));
}
}
}

//Output

//A square is a Polygon
//Area IS : 36

Saturday, June 14, 2008

Multi level inheritance

/* Write a program in C sharp on parameteried
construtor in multilevel inheritance*
*/
using System;

namespace Inherit
{
class Metals
{
string _metalType;
public Metals(String type)
{
_metalType = type;
Console.WriteLine("Metal\t\t:" + _metalType);
}
}

class SteelCompany : Metals
{
string _grade;
public SteelCompany(string grade) : base("Steel")
{
_grade = grade;
Console.WriteLine("Grade\t\t:" + _grade);
}
}
class Automobiles : SteelCompany
{
string _part;
public Automobiles(string part): base("Cast Iron")
{
_part = part;
Console.WriteLine("Part\t\t:" + _part);
}
public static void Main()
{
Automobiles auto = new Automobiles("Mercedes");
}
}
}

//Output

//Metal :Steel
//Grade :Cast Iron
//Part :Mercedes

Multilevel Inhetitance 2

/* C# program to implemmet multi level inheritance.
This program demonstrates how base class constructors are inherited using multilevel inheritance. */

using System;

namespace Inherit
{
class Shape
{
public Shape()
{
Console.WriteLine("Constructor of base class Shape");
}
}
class Polygon : Shape
{
public Polygon()
{
Console.WriteLine("Constructor of Base class Polygon");
}
}
class Quad : Polygon
{
public Quad()
{
Console.WriteLine("Contructor of class Quad");
}
public static void Main(string[] args)
{
Quad obj= new Quad();
}
}
}

//Output

//Constructor of base class Shape
//Constructor of Base class Polygon
//Contructor of class Quad

Multilevel Inheritance

/* Program in C# for implementation of multiway inheritance */
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Inherit
{
class One:Mammal
{
public void Noise()
{
Console.WriteLine("Dog Barks");
}

static void Main(string[] args)
{
One obj = new One();
obj.Eat();
obj.feature();
obj.Noise();
}
}

class Animal
{
public void Eat()
{
Console.WriteLine("Every Animal Eats Something");
}
}

class Mammal : Animal
{
public void feature()
{
Console.WriteLine("Mammal Gives birth to young ones");
}
}
}
//Output

//Every Animal Eats Something
//Mammal Gives birth to young ones
//Dog Barks

Tuesday, June 3, 2008

Matrix Operations - Addition, Subtraction, Multiplication

/* Simple C sharp program for adding, subtracting and multiplyilng two matrices.
The program takes inputs for rows and columns of the two matrices seperately and then calculates the following :

1. Addition
2. Subtraction
3. Multiplication.

Functions have been created for addition, subtraction, multiplication and display of the matrices. The program using OOPs is posted seperately. The Functions and variables have to be declared static.

*/

using System;

namespace Array_Pgm
{

class MatrixCalc
{
static int i, j;

static void addMatrix(int[,] A, int[,] B, int m, int n)
{
int[,] C = new int[10, 10];
for (i = 0; i < m; i++)
{
for (j = 0; j < n; j++)
C[i, j] = A[i, j] + B[i, j];
}
display(C, m, n);
}

static void subMatrix(int[,] A, int[,] B, int m, int n)
{
int[,] C = new int[10, 10];
for (i = 0; i < m; i++)
{
for (j = 0; j < n; j++)
C[i, j] = A[i, j] - B[i, j];
}
display(C, m, n);
}

static void display(int[,] A, int m, int n)
{
for (i = 0; i < m; i++)
{
for (j = 0; j < n; j++)
Console.Write(A[i, j] + "\t");
Console.WriteLine();
}
}

static void mulMatrix(int[,]A,int [,]B,int m,int n)
{
int [,]C = new int[10,10];
for(i=0;i< m;i++)
{
for(j=0;j< n;j++)
{
C[i,j] = 0;
for(int k=0;k< n;k++)
{
C[i,j] = C[i,j] + A[i,k] * B[k,j];
}
}
}
display(C, m, n);
}

public static void Main(string[] args)
{
// Input First Matrix say A;
int m, n;
Console.Write("Enter No Of Roes And Columns Of Matrix A : ");
m =Convert.ToInt16(Console.ReadLine());
n =Convert.ToInt16(Console.ReadLine());
int[,] A = new int[10, 10];
Console.Write("\nEnter The First MNatrix : ");
for (i = 0; i < m; i++)
{
for (j = 0; j < n; j++)
A[i, j] =Convert.ToInt16(Console.ReadLine());
}

// Input Second Matrix say B;
int p, q;
Console.Write("Enter No Of Rows And Columns Of Matrix B :");
p =Convert.ToInt16(Console.ReadLine());
q =Convert.ToInt16(Console.ReadLine());
int[,] B = new int[10, 10];
Console.Write("\nEnter The Second Matrix:");
for (i = 0; i < p; i++)
{
for (j = 0; j < q; j++)
B[i, j] = Convert.ToInt16(Console.ReadLine());
}
Console.Clear();

// Display The Two Matrixes

Console.WriteLine("\nMatrix A : ");
display(A, m, n);

Console.WriteLine("\nMatrix B: ");
display(B, p, q);

// Menu
Console.WriteLine("**** MENU For Matrix Opearations *****");
Console.WriteLine(" 1. Addition");
Console.WriteLine(" 2. Subtraction");
Console.WriteLine(" 3. Multiplication");
Console.WriteLine(" 4. Exit");
Console.WriteLine("Enter Your option");
int choice = Convert.ToInt16(Console.ReadLine());

switch (choice)
{
case 1:
if (m == p && n == q)
{
Console.WriteLine("Addition ");
addMatrix(A, B, m, n);
}
else
Console.WriteLine("ERROR !!!!!! Rows and Columns Of Both martixes should be same");
break;

case 2:
if (m == p && n == q)
{
Console.WriteLine("Subtraction ");
subMatrix(A, B, m, n);
}
else
Console.WriteLine("ERROR !!!!!! Rows and Columns Of Both martixes should be same");
break;

case 3:
if (n == p)
{
Console.WriteLine("Multiplication: ");
mulMatrix(A, B, m, q);
}
else
Console.WriteLine("ERROR !!!!!! Column of First matrix and Row Of Second Matrix should be same");
break;

case 4:
Console.Beep(1000, 1000);
break;
/* default:
Console.WriteLine("Error");*/
}
}
}
}