0% found this document useful (0 votes)
12 views4 pages

C# Assignment 4: Monday, October 21, 2024 11:24 AM

The document outlines a C# assignment involving the creation of various classes and interfaces, demonstrating object-oriented programming principles such as inheritance, polymorphism, and encapsulation. It includes tasks to create base and derived classes for animals, vehicles, and employees, as well as implementing interfaces and demonstrating boxing and unboxing. Each section provides code examples and requirements for the implementation of specific functionalities.

Uploaded by

usmangani892003
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)
12 views4 pages

C# Assignment 4: Monday, October 21, 2024 11:24 AM

The document outlines a C# assignment involving the creation of various classes and interfaces, demonstrating object-oriented programming principles such as inheritance, polymorphism, and encapsulation. It includes tasks to create base and derived classes for animals, vehicles, and employees, as well as implementing interfaces and demonstrating boxing and unboxing. Each section provides code examples and requirements for the implementation of specific functionalities.

Uploaded by

usmangani892003
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/ 4

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 {

public int NumberOfDoors { get; set;}


public override void Drive(){
Console.WriteLine($"Car with {NumberOfDoors} doors is moving at speed {Speed} mph") ;
}
}
class Bicycle : Vehicle {

public bool HasBasket { get; set;}


public override void Drive(){

Console.WriteLine("Bicycle" + (HasBasket ? "with" : "without") + " a basket is moving at speed {Speed} mph") ;
}
}

Quick Notes Page 1


/*
3. Write a base class Person that contains:
● A property Name (string).
● A constructor that initializes the Name property.
● A method Greet() that prints "Hello, my name is {Name}."
Create a derived class Employee that inherits from Person:
● Add a property JobTitle (string).
● Use the base keyword to call the base class constructor to initialize the Name property.
● Override the Greet() method to include the JobTitle by calling the base class Greet() method using the base
keyword, and then adding "I work as a {JobTitle}."
*/

namespace A4.Q3;
class Person {
public string Name { get; set; }
public Person(string name) => Name = name ;

public virtual void Greet() {


Console.WriteLine($"Kon'nichiwa, watashi no namae wa {Name}-desu.") ;
}
}
class Employee : Person {
public string JobTitle { get; set; }
public Employee(string name, string job_title) : base(name) => JobTitle = job_title ;
public override void Greet() {
base.Greet();
Console.WriteLine($"I work as a {JobTitle}.") ;
}
}
class Test {
public static void Run(string[] args){
Employee emp1 = new("Aki", "Software Developer");
new Employee("Aki2", "Software Developer").Greet();
}
}

/*
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.");

Quick Notes Page 2


public virtual void Work() => Console.WriteLine("Employee is working.");
}
class Manager : Employee {
public override void Work() => Console.WriteLine("Manager is overseeing working.");
}
sealed class Director : Employee {
public override void Work() => Console.WriteLine("Director is managing the company.");
}
// class MD : Director { } // sealed member can't be inherited

/*
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();

Quick Notes Page 3


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}") ;
}
}

Quick Notes Page 4

You might also like