C# Assignment 4: Monday, October 21, 2024 11:24 AM
C# Assignment 4: Monday, October 21, 2024 11:24 AM
/*
1. Create a base class Animal with the following:
● A property Name (string).
● A method MakeSound() that prints a generic message like "Animal makes a sound."
Then create two derived classes, Dog and Cat, that inherit from Animal:
● Dog should override MakeSound() to print "Dog barks."
● Cat should override MakeSound() to print "Cat meows."
*/
namespace A4.Q1;
class Animal {
public string? Name {get; set;}
public virtual void MakeSound() {
Console.WriteLine("Animal makes a sound") ;
}
}
class Dog : Animal {
public override void MakeSound(){
Console.WriteLine($"{this.Name} Barks...!") ;
}
}
class Cat : Animal {
public override void MakeSound() {
Console.WriteLine($"{this.Name} Meows...!") ;
}
}
class Test {
public static void Run(string[] args) {
Dog d1 = new() { Name="Bruno" } ;
Cat c1 = new() { Name="Lucifer" };
d1.MakeSound() ;
c1.MakeSound() ;
}
}
/*
2. Create a base class Vehicle with the following:
● A property Speed (int).
● A method Drive() that prints "Vehicle is moving at speed X."
Then create two derived classes:
● Car, which has an additional property NumberOfDoors (int) and overrides the Drive()
method to include the number of doors.
● Bicycle, which has an additional property HasBasket (bool) and overrides the
Drive() method to include whether it has a basket.
*/
namespace A4.Q2;
class Vehicle {
public int Speed { get; set; }
public virtual void Drive() {
Console.WriteLine($"Vehicle is moving at speed {Speed} mph") ;
}
}
class Car : Vehicle {
Console.WriteLine("Bicycle" + (HasBasket ? "with" : "without") + " a basket is moving at speed {Speed} mph") ;
}
}
namespace A4.Q3;
class Person {
public string Name { get; set; }
public Person(string name) => Name = name ;
/*
4. Create a base class Shape with the following:
● A method Draw() that prints "Drawing a shape."
● Mark this method as virtual, so it can be overridden.
Create a derived class Circle that:
● Overrides the Draw() method to print "Drawing a circle."
● Marks the Draw() method as sealed, so it cannot be overridden further.
Finally, create another class FilledCircle that inherits from Circle, but cannot override the Draw() method.
*/
namespace A4.Q4;
class Shape {
public virtual void Draw() => Console.WriteLine("Drawing a shape.") ;
}
class Circle : Shape {
public sealed override void Draw() => Console.WriteLine("Drawing a circle.") ;
}
class FilledCircle : Circle {
//public override void Draw() { }
//cannot override sealed method
}
/*
5. Create a base class Employee with the following:
● A method Work() that prints "Employee is working."
Create a derived class Manager that:
● Overrides the Work() method to print "Manager is overseeing work."
Create a class Director that inherits from Manager but marks the class as sealed, preventing any further inheritance.
● Override the Work() method to print "Director is managing the company."
Demonstrate that no class can inherit from Director.
*/
class Employee {
public virtual void Work() => Console.WriteLine("Employee is working.");
/*
6. Create a base class Animal with the following:
● A method Speak() that prints "Animal is making a sound."
Create two derived classes:
● Dog, which overrides Speak() to print "Dog barks."
● Cat, which overrides Speak() to print "Cat meows."
In the Main method:
● Create an array of Animal objects, including instances of Dog and Cat.
● Loop through the array and call the Speak() method on each object. Use runtime polymorphism to invoke the correct
method for each derived class.
*/
namespace A4.Q6;
class Animal {
public virtual void Speak() => Console.WriteLine("Animal is making a sound.") ;
}
class Dog : Animal {
public override void Speak() => Console.WriteLine("Dog barks.");
}
class Cat : Animal {
public override void Speak() => Console.WriteLine("Cat meows.");
}
class Test {
public static void Run(string[] args) {
Animal[] vehicles = new Animal[10];
for (int i=0; i<10; i++) {
vehicles[i] = i%2 == 0 ? new Cat() : new Dog();
}
for(int i=0; i<10; i++) vehicles[i].Speak();
}
}
/*
7. Create an interface IVehicle with the following:
● A method Drive() that prints a generic driving message.
Implement the IVehicle interface in two classes:
● Car, which implements Drive() to print "Car is driving."
● Bike, which implements Drive() to print "Bike is riding."
In the Main method:
● Create an array of IVehicle vehiclejects, including instances of Car and Bike.
● Loop through the array and call the Drive() method on each vehicleject, demonstrating runtime polymorphism with
interface-based polymorphism
*/
namespace A4.Q7;
interface IVehicle {
public void Drive() {
Console.WriteLine("Driving a vehicle.");
}
}
class Car : IVehicle {
public void Drive() {
Console.WriteLine("Driving a car.");
}
}
class Bike : IVehicle {
public void Drive() {
Console.WriteLine("Riding a bike.");
}
}
class Test {
public static void Run(string[] args) {
IVehicle[] vehicles = new IVehicle[10];
for (int i=0; i<10; i++) {
vehicles[i] = i%2 == 0 ? new Car() : new Bike();
}
for(int i=0; i<10; i++) vehicles[i].Drive();
/*
8. Write a program that demonstrates boxing and unboxing using the following steps:
● Declare an int variable and assign a value to it.
● Perform boxing by assigning the int value to an object variable.
● Perform unboxing by converting the object variable back to an int.
● Print the results to show that the value was successfully boxed and unboxed
*/
namespace A4.Q8;
class Test {
public static void Run(string[] args) {
int x = 10 ;
Console.WriteLine($"Original int value: {x}") ;
object boxed = x;
Console.WriteLine($"Boxed value: {boxed}") ;
int y = (int)boxed ;
Console.WriteLine($"Unboxed int value: {y}") ;
}
}