[go: up one dir, main page]

0% found this document useful (0 votes)
17 views5 pages

Task 3

The document contains multiple C# code snippets demonstrating various programming concepts. It includes a program for calculating the area of different shapes based on user input, a vehicle class hierarchy with derived classes for Truck, Car, and Motorcycle, and a shape base class with derived shapes like Circle, Rectangle, and Triangle. Additionally, it showcases class inheritance with examples of base and derived classes, as well as an Animal class with Dog and Bird subclasses.

Uploaded by

saylasarmin32
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)
17 views5 pages

Task 3

The document contains multiple C# code snippets demonstrating various programming concepts. It includes a program for calculating the area of different shapes based on user input, a vehicle class hierarchy with derived classes for Truck, Car, and Motorcycle, and a shape base class with derived shapes like Circle, Rectangle, and Triangle. Additionally, it showcases class inheritance with examples of base and derived classes, as well as an Animal class with Dog and Bird subclasses.

Uploaded by

saylasarmin32
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/ 5

1.

Program for Shape Area Based on User Input

using System;

class Program
{
static void Main(string[] args)
{
Console.WriteLine("Enter shape type (circle, rectangle, triangle):
");
string shape = Console.ReadLine().ToLower();

switch (shape)
{
case "circle":
Console.Write("Enter radius: ");
double radius = double.Parse(Console.ReadLine());
Console.WriteLine("Area of Circle: " + (Math.PI * radius *
radius));
break;

case "rectangle":
Console.Write("Enter length: ");
double length = double.Parse(Console.ReadLine());
Console.Write("Enter width: ");
double width = double.Parse(Console.ReadLine());
Console.WriteLine("Area of Rectangle: " + (length * width));
break;

case "triangle":
Console.Write("Enter base: ");
double b = double.Parse(Console.ReadLine());
Console.Write("Enter height: ");
double height = double.Parse(Console.ReadLine());
Console.WriteLine("Area of Triangle: " + (0.5 * b * height));
break;

default:
Console.WriteLine("Invalid shape!");
break;
}
}
}

2. Vehicle Class Hierarchy

using System;
class Vehicle
{
public string Model;
public int Year;
public string FuelType;

public virtual void FuelEfficiency()


{
Console.WriteLine("Fuel efficiency: Default value");
}

public virtual void DistanceTravelled()


{
Console.WriteLine("Distance travelled: Default value");
}

public virtual void MaxSpeed()


{
Console.WriteLine("Max speed: Default value");
}
}

class Truck : Vehicle


{
public override void FuelEfficiency()
{
Console.WriteLine("Truck fuel efficiency: 8 km/l");
}
public override void DistanceTravelled()
{
Console.WriteLine("Truck distance travelled: 500 km");
}
public override void MaxSpeed()
{
Console.WriteLine("Truck max speed: 120 km/h");
}
}

class Car : Vehicle


{
public override void FuelEfficiency()
{
Console.WriteLine("Car fuel efficiency: 15 km/l");
}
public override void DistanceTravelled()
{
Console.WriteLine("Car distance travelled: 600 km");
}
public override void MaxSpeed()
{
Console.WriteLine("Car max speed: 180 km/h");
}
}

class Motorcycle : Vehicle


{
public override void FuelEfficiency()
{
Console.WriteLine("Motorcycle fuel efficiency: 35 km/l");
}
public override void DistanceTravelled()
{
Console.WriteLine("Motorcycle distance travelled: 300 km");
}
public override void MaxSpeed()
{
Console.WriteLine("Motorcycle max speed: 160 km/h");
}
}

3. Shape Base Class with Derived Shapes

using System;

abstract class Shape


{
public abstract double Area();
}

class Circle : Shape


{
public double Radius;

public Circle(double radius)


{
Radius = radius;
}

public override double Area()


{
return Math.PI * Radius * Radius;
}
}

class Rectangle : Shape


{
public double Length, Width;
public Rectangle(double length, double width)
{
Length = length;
Width = width;
}
public override double Area()
{
return Length * Width;
}
}

class Triangle : Shape


{
public double Base, Height;

public Triangle(double b, double height)


{
Base = b;
Height = height;
}

public override double Area()


{
return 0.5 * Base * Height;
}
}

4. Class Inheritance with Inherit, InheritChild, Child

using System;

class Inherit
{
public string str = "Base class string";

public void InheritMethod()


{
Console.WriteLine("Method in Inherit: " + str);
}
}

class InheritChild : Inherit


{
public void InheritChildMethod()
{
Console.WriteLine("Method in InheritChild: " + str);
}
}
class Child : InheritChild
{
public void ChildMethod()
{
Console.WriteLine("Method in Child: " + str);
}
}
5. Animal Class with Dog and Bird

using System;

class Animal
{
public void Walk()
{
Console.WriteLine("Animal is walking...");
}

public void Eat()


{
Console.WriteLine("Animal is eating...");
}
}

class Dog : Animal


{
public int NoOfLegs = 4;

public void Bark()


{
Console.WriteLine("Dog is barking...");
}
}
class Bird : Animal
{
public int NoOfWings = 2;

public void Fly()


{
Console.WriteLine("Bird is flying...");
}

You might also like