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

Lab-3 by 3075

DAA expirement-3

Uploaded by

Dhruv Khant
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views

Lab-3 by 3075

DAA expirement-3

Uploaded by

Dhruv Khant
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

FACULTY OF ENGINEERING AND TECHNOLOGY

Department of Computer Engineering


01CE0523 - .NET Technologies

Experiment 3
AIM: Program on Polymorphism and Exception Handling

Code:

Polymorphism

using System;

namespace PolymorphismAndException
{
class Cartype
{
public virtual void Type()
{
Console.WriteLine("This is a generic car.");
}

public virtual void ShowDetails()


{
Console.WriteLine("Base class: Cartype");
}
}

class CarSound : Cartype


{
public override void Type()
{
Console.WriteLine("Vroom!! Vroom!!");
}

public override void ShowDetails()


{
Console.WriteLine("Derived class: CarSound");
}
}

class Excep
{
public void Div(int numerator, int divisor)
{
try
{
int result = numerator / divisor;
Console.WriteLine($"Result of {numerator} / {divisor} is: {result}");
}
catch (DivideByZeroException ex)
{
Console.WriteLine("Exception caught: Division by zero is not allowed.");
Console.WriteLine("Details: " + ex.Message);
}
catch (Exception ex)
{
Console.WriteLine("An unexpected error occurred.");
Console.WriteLine("Details: " + ex.Message);
}

Dhruv Khant (92310103075) BATCH: C


FACULTY OF ENGINEERING AND TECHNOLOGY
Department of Computer Engineering
01CE0523 - .NET Technologies

}
}

class MainClass
{
static void Main(string[] args)
{
Cartype c1 = new Cartype();
CarSound s1 = new CarSound();
Excep e1 = new Excep();

Console.WriteLine("Choose the operation from the following:\n1. Polymorphism\n2.


Exception Handling\n");
int choice;

if (int.TryParse(Console.ReadLine(), out choice))


{
Console.WriteLine($"You chose option {choice}.");

switch (choice)
{
case 1:
Console.WriteLine("Executing polymorphism:");

Cartype c2 = new CarSound();


c1.Type();
s1.Type();
c2.Type();
c1.ShowDetails();
s1.ShowDetails();
break;

case 2:
Console.WriteLine("Executing exception handling:");
e1.Div(10, 2);
e1.Div(10, 0);

default:
Console.WriteLine("Invalid choice. Please choose 1 or 2.");
break;
}
}
else
{
Console.WriteLine("Invalid input. Please enter a number.");
}
}
}
}

Dhruv Khant (92310103075) BATCH: C


FACULTY OF ENGINEERING AND TECHNOLOGY
Department of Computer Engineering
01CE0523 - .NET Technologies

Output-

Dhruv Khant (92310103075) BATCH: C

You might also like