0% found this document useful (0 votes)
60 views

C# Lab - Part - B

The document discusses C# programs demonstrating various OOP concepts like abstract classes, inheritance, polymorphism, delegates, boxing, unboxing and more. It includes the code and output for programs implementing these concepts.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
60 views

C# Lab - Part - B

The document discusses C# programs demonstrating various OOP concepts like abstract classes, inheritance, polymorphism, delegates, boxing, unboxing and more. It includes the code and output for programs implementing these concepts.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 9

PART-B

//1. Write a program in C# to demonstrate abstract class and abstract methods.


using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Linq;
using System.Text;

namespace CosoleApplication3
{
abstract class MotorBike
{

public abstract void sports();


}

class SportsBike : MotorBike


{

// provide implementation of abstract method


public override void sports()
{
Console.WriteLine("Sports Bike is YAMAHA");
}

class MountainBike : MotorBike


{

// provide implementation of abstract method


public override void sports()
{
Console.WriteLine("Mountain Bike is HIMALAYAN");
}

}
class Program
{
static void Main(string[] args)
{
// create an object of SportsBike class
SportsBike s1 = new SportsBike();
s1.sports();

// create an object of MountainBike class


MountainBike m1 = new MountainBike();
m1.sports();

Console.ReadLine();
}
}
}

OUTPUT:

CHETAN M, LECTURER, Dept OF BCA, GFGC HOSADURGA Page 1


//2. Write a program in C# to perform single level inheritance.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Linq;
using System.Text;

namespace single_inheritance
{

public class Employee


{
public float salary = 40000;
}
public class Programmer : Employee
{
public float bonus = 10000;
}
class TestInheritance
{
public static void Main(string[] args)
{
Programmer p1 = new Programmer();

Console.WriteLine("Salary of an employee is : " + p1.salary);


Console.WriteLine("Bonus for an employee is : " + p1.bonus);
Console.ReadLine();

}
}
}

OUTPUT:

CHETAN M, LECTURER, Dept OF BCA, GFGC HOSADURGA Page 2


//3. Write a program in C# to perform Switch Statements to display percentage of a student.

using System;
using System.Collections.Generic;
using System.Text;

namespace ConsoleApplication2
{
class Program
{
static void Main(string[] args)
{
int mark;
Console.WriteLine("enter the mark");
mark = Convert.ToInt32(Console.ReadLine());
switch (mark / 10)
{
case 10:
case 9:
Console.WriteLine("Grade: A");
break;

case 8:
Console.WriteLine("Grade: B");
break;

case 7:
Console.WriteLine("Grade: C");
break;

case 6:
Console.WriteLine("Grade: D");
break;

case 5:
Console.WriteLine("Grade: E");
break;

default:
Console.WriteLine("Grade: F");
break;
}
Console.ReadLine();
}
}
}

OUTPUT:

Case : 1 Case : 2

CHETAN M, LECTURER, Dept OF BCA, GFGC HOSADURGA Page 3


//4. Write a c# program to find the sum of each rows of a given jagged arrays of 3 inner arrays
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace lab4
{
public class prgm4
{
public static void Main()
{
int[][] jag = new int[3][];
jag[0] = new int[2];
jag[1] = new int[4];
jag[2] = new int[3];
int[] sum = new int[3];
int result = 0;
int i, j;
for (i = 0; i < 3; i++)
sum[i] = 0;
Console.WriteLine("Enter the size of the array ");
for (i = 0; i < jag.Length; i++)
{
Console.WriteLine("Enter the values off array " + (i + 1) + " jag:");
for (j = 0; j < jag[i].Length; j++)
{
jag[i][j] = int.Parse(Console.ReadLine());
}
}
for (i = 0; i < jag.Length; i++)
{
for (j = 0; j < jag[i].Length; j++)
{
sum[i] = sum[i] + jag[i][j];
}
}
for (i = 0; i < 3; i++)
result = result + sum[i];
Console.WriteLine("The result of individual jags row-wise: ");
for (i = 0; i < 3; i++)
Console.Write(sum[i] + "\t");
Console.WriteLine();
Console.WriteLine("The final sum of all the elements in the jagged array:" + result);
Console.Read();
}
}
}

OUTPUT:

CHETAN M, LECTURER, Dept OF BCA, GFGC HOSADURGA Page 4


// 5. C# Program for Timer control properties start and stop
using System;
using System.Timers;
namespace ConsoleApplication1
{
class Program
{
private static System.Timers.Timer myTimer;
static void Main(string[] args)
{
myTimer = new System.Timers.Timer(1000);
myTimer.Elapsed += OnTimedEvent;
myTimer.AutoReset = true;
myTimer.Enabled = true;
Console.WriteLine("\nPress Enter key to stop...\n");
Console.WriteLine("Timer started {0:HH:mm:ss.fff} ", DateTime.Now);
Console.ReadLine();
myTimer.Stop();
myTimer.Dispose();
Console.WriteLine("Finished...");
}
private static void OnTimedEvent(Object source, ElapsedEventArgs e)
{
Console.WriteLine("Elapsed event raised at {0:HH:mm:ss.fff}", e.SignalTime);
}
}
}

OUTPUT

CHETAN M, LECTURER, Dept OF BCA, GFGC HOSADURGA Page 5


// 6. C# Program to Implement Arithmetic Operations using Delegates

using System;
delegate int NumberChanger(int n);
namespace example
{
class Delegate
{
static int num = 10;
public static int AddNum(int a)
{
num += a;
return num;
}

public static int MultNum(int b)


{
num *= b;
return num;
}
public static int getNum()
{
return num;
}

static void Main(string[] args)


{

NumberChanger n1 = new NumberChanger(AddNum);


NumberChanger n2 = new NumberChanger(MultNum);
n1(300);
Console.WriteLine("Value of Num: {0}", getNum());
n2(250);
Console.WriteLine("Value of Num: {0}", getNum());
Console.ReadKey();
}
}
}

OUTPUT

CHETAN M, LECTURER, Dept OF BCA, GFGC HOSADURGA Page 6


// 7. a) C# Program to Implement boxing

using System;
namespace boxing_unboxing
{
class GFG
{

// Main Method
static public void Main()
{

// assigned int value


// 2020 to num
int num = 2020;

// boxing
object obj = num;

// value of num to be change


num = 100;

System.Console.WriteLine("Value - type value of num is : {0}", num);


System.Console.WriteLine("Object - type value of obj is : {0}", obj);
Console.ReadLine();
}
}
}

OUTPUT:

CHETAN M, LECTURER, Dept OF BCA, GFGC HOSADURGA Page 7


//7. b) C# Program to Implement unboxing

using System;
namespace boxing_unboxing
{

class GFG
{

// Main Method
static public void Main()
{

// assigned int value


// 23 to num
int num = 23;

// boxing
object obj = num;

// unboxing
int i = (int)obj;

// Display result
Console.WriteLine("Value of ob object is : " + obj);
Console.WriteLine("Value of i is : " + i);
Console.ReadLine();
}
}
}

OUTPUT:

CHETAN M, LECTURER, Dept OF BCA, GFGC HOSADURGA Page 8


// 8. C# PROGRAM TO DEMONSTRATE VIRTUAL AND OVERRIDE METHDS
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Generics
{
class A
{
public virtual void show()
{
Console.WriteLine("Hello: Base Class!");
Console.ReadLine();
}
}

class B : A
{
public override void show()
{
Console.WriteLine("Hello: Derived Class!");
Console.ReadLine();
}
}

class Polymorphism
{
public static void Main()
{
A a1 = new A();
a1.show();
B b1 = new B();
b1.show();
A a2 = new B();
a2.show();
}
}
}

OUTPUT:

CHETAN M, LECTURER, Dept OF BCA, GFGC HOSADURGA Page 9

You might also like