Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
11 views

Programming 2 Week 2

Uploaded by

Melissa Lemus
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views

Programming 2 Week 2

Uploaded by

Melissa Lemus
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

Programming 2 Week 2: C# Console Applications Assignments

Mandatory Assignments (1-4)

These assignments introduce core concepts like inheritance, abstract classes, and method
overriding, providing a solid foundation in object-oriented programming.

1. Inheritance and Method Overriding


o Goal: Understand how inheritance works and how to override methods in
derived classes to customize behavior.
2. Abstract Base Class and Method Overriding
o Goal: Learn how to create an abstract base class and override methods in
derived classes, focusing on method implementation in a class hierarchy.
3. Inheritance and Constructor Chaining
o Goal: Explore how constructors work with inheritance and how data is passed
between base and derived classes using constructor chaining.
4. Abstract Class with Virtual Methods and Handling Data in Derived Classes
o Goal: Use an abstract class with a combination of virtual methods and data
handling in derived classes, while practicing method overriding.

Mandatory Assignments (5-7)

These assignments expand on the core concepts by applying inheritance, abstract classes, and
method overriding in a more complex, real-world context.

5. Zoo System - Basic Structure with Abstract Classes


o Goal: Build a basic zoo system using abstract classes and inheritance, focusing
on handling animal-specific behavior with overridden methods.
6. Zoo System - Managing Animal Feeding
o Goal: Extend the zoo system by adding feeding functionality for animals,
practicing method overriding for different animal types.
7. Zoo System - Animal Sounds and Feeding Management
o Goal: Coordinate animal sounds and feeding processes in the zoo system,
using inheritance and method overriding to handle behavior specific to each
animal type.
Assignment 1: Inheritance and Overriding Methods
Objective: Explore inheritance and overriding methods in derived classes.

Task:

• Create a base class Vehicle with a property Model (string) and a method void
StartEngine() that prints "Engine started".
• Create a derived class ElectricCar that inherits from Vehicle and overrides
StartEngine() to return "Electric engine started".
• Create a second derived class DieselCar that also inherits from Vehicle and
overrides the StartEngine() method by to return "Diesel engine started".
• In the Start() method, create objects of both ElectricCar and DieselCar, and call
the StartEngine() method on both. Demonstrate method overriding by calling the
method using base class and derived class references.

Sample Output:

Engine started
Electric engine started
Diesel engine started

Assignment 2: Abstract Base Class with Virtual Methods

Objective: Create an abstract class with a mix of abstract and virtual methods to demonstrate
the combination of the two.

Task:

• Create an abstract class Shape with an abstract method double CalculateArea(),


and a virtual method string GetShapeInfo() that returns a generic string "Shape
details".
• Create two derived classes: Circle (with a public property Radius) and
Rectangle (with public properties Length & Width). Both classes should
implement the CalculateArea() method. Circle should also override the
DisplayShapeInfo() method to return the specific details for a circle, including its
radius, and Rectangle should override it to return the details for a rectangle, including
its length and width.
• In the Start() method, create instances of both Circle and Rectangle. Display their
areas and their shape-specific information using polymorphism and the overridden
methods.
Sample Output:

Circle area: 78.54


Circle details: Circle with radius: 5
Rectangle area: 20.00
Rectangle details: Rectangle with length: 4, width: 5

Assignment 3: Inheritance with Constructor Chaining


Objective: Implement constructor chaining across a class hierarchy to pass data from derived
classes to base classes.

Task:

• Create a base class Person with properties Name and Age. Provide a constructor that
accepts both parameters and initializes the properties.
• Create a derived class Employee that inherits from Person and adds a property
JobTitle. Provide a constructor in Employee that accepts Name, Age, and JobTitle,
and passes Name and Age to the base class constructor using base().
• Create a second derived class Manager that inherits from Employee and adds a
property Department (string). Provide a constructor in Manager that accepts Name,
Age, JobTitle, and Department, and passes Name, Age, and JobTitle to the
Employee constructor using base().
• In the Start() method, create instances of Employee and Manager, and use their
ToString() methods to print the details of these instances.

Sample Output:

Employee: John Doe, Age: 30, Job Title: Developer


Manager: Alice Smith, Age: 45, Job Title: Team Lead, Department: IT
Assignment 4: Abstract Class with Virtual Methods and Handling Data
in Derived Classes

Objective: Use an abstract class with a combination of virtual methods and data handling in
derived classes.

Task:

• Create an abstract class Employee with the properties Name (string) and Salary
(decimal). Provide an abstract method CalculateBonus() that will be implemented in
derived classes to calculate the bonus based on the salary. Also add a constructor that
initializes all properties to this class.
• Create a derived class FullTimeEmployee that overrides the CalculateBonus()
method to return a bonus equal to 10% of the salary.
• Create a second derived class PartTimeEmployee that overrides the
CalculateBonus() method to return a bonus equal to 5% of the salary.
• Add a virtual method DisplayEmployeeDetails() in the base class Employee, which
displays the Name and Salary. Override this method in both derived classes to also
display the calculated bonus amount.
• In the Start() method, create instances of FullTimeEmployee and
PartTimeEmployee, and display their details, including the calculated bonus.

Sample Output:
Full-time employee: John Doe
Salary: 50000.00
Bonus: 5000.00

Part-time employee: Jane Smith


Salary: 20000.00
Bonus: 1000.00
Mandatory Assignments (5-7)
These assignments like the previous ones are mandatory and will guide students through
building a more complex system, applying the concepts learned in earlier tasks. Focus is
placed on the Inheritance, abstract classes and method overriding.

Assignment 5: Zoo System - Basic Structure with Abstract Classes

Objective: Build a basic zoo system using abstract classes and inheritance.

Task:

• Create an abstract class Animal with properties Name and Species, and an abstract
method string MakeSound(). Override the ToString() method to return the Name
and Species.
• Add a constructor to the Animal that accepts a name and instantiates properties Name
and Species (This should be set to the class name as a default).
• Create two derived classes: Lion and Elephant. In Lion, override MakeSound() to
return "Roar", and in Elephant, override it to return "Trumpet".
• Create a Zoo class with a method DisplayAnimalDetails(Animal animal) that
accepts an Animal object and prints its Name and Species (hint: look at the
first bullet point).
• In the Start() method, create instances of Lion and Elephant, and call the
DisplayAnimalDetails- method to display the details.

Sample Output:

Animal Name: Simba, Species: Lion


Animal Name: Dumbo, Species: Elephant

Assignment 6: Zoo System - Managing Animal Feeding

Objective: Extend the zoo system to include feeding functionality for animals.

Task:

• Extend the Animal class by adding a virtual method Feed() that returns "Feeding
{AnimalName}the {AnimalSpecies}".
• Override the Feed() method in both Lion and Elephant to customize the feeding
behavior. For example, "Feeding the lion meat" and "Feeding the elephant
vegetables".
• Create a ZooKeeper class with a method FeedAllAnimals(Animals[] animals) that
accepts a collection of Animal objects and calls the Feed() method for each one.
• In the Start() method, create instances of Lion and Elephant, add them to a
collection, and call FeedAllAnimals() to feed all animals.

Sample Output:

Feeding Simba the Lion meat


Feeding Dumbo the Elephant vegetables

Assignment 7: Zoo System - Animal Sounds

Objective: Extend the zoo system to handle animal sounds and feeding in a coordinated way.

Task:

• Extend the Zoo class with a method MakeAllAnimalsSound(Animal[] animals) that


iterates through a collection of Animal objects and calls their MakeSound() method.
• In the start() method, use the same collection of animals as assignment 6 and call
the MakeAllAnimalsSound method.

Sample Output:

Roar (Lion)
Trumpet (Elephant)

You might also like