Victorian Institute of Technology: OOP with C# Assignment Solution

Verified

Added on  2023/06/11

|6
|690
|365
Homework Assignment
AI Summary
This assignment solution addresses the principles of Object-Oriented Programming (OOP) with C#. It begins by explaining two core OOP principles: inheritance and polymorphism, providing clear definitions and examples of their application. The solution then presents a C# code implementation, starting with the creation of a `Rectangle` class. This class includes private variables for `width` and `height`, a constructor to initialize these variables, and a public method `CalcArea()` to compute the area of the rectangle. A separate driver class, `Calculate`, demonstrates how to instantiate the `Rectangle` class and use the `CalcArea()` function to calculate and display the area. The solution includes the complete code for both classes, along with the expected output, providing a practical guide to OOP concepts in C#.
Document Page
Running head: OBJECT ORIENTED PROGRAMMING WITH C#
Object Oriented Programming with C#
Name of the student:
Name of the University:
Author note:
tabler-icon-diamond-filled.svg

Secure Best Marks with AI Grader

Need help grading? Try our AI Grader for instant feedback on your assignments.
Document Page
1
OBJECT ORIENTED PROGRAMMING WITH C#
Table of Contents
Task 1...............................................................................................................................................2
Task 2...............................................................................................................................................3
Rectangle Class (Rectangle.cs)....................................................................................................3
Driver class (Calculate.cs)...........................................................................................................3
Output..........................................................................................................................................4
Document Page
2
OBJECT ORIENTED PROGRAMMING WITH C#
Task 1
Two main principles of Object Oriented Programming are:
Inheritance: This is the process by which classes within the same package can inherit
certain characteristics and behaviors from leading or existing classes. It is the child class
that is known to inherit the accessible properties from the parent class, also known as the
root class. The concept of inheritance helps to conceptualize the hierarchical
classification of classes and objects. Though inheritance, programmers can manage to re-
use various sections of the code. This helps in the reducing space complexity of
programs, as the redundant information and functionalities can be inherited from the
existing classes. These data or method members are initialized once in the parent classes
and are then used by the instances of the child classes. The accessibility of these members
in the child classes depends on the access specifiers of these members. Public data
members can be accesses by all classes of the package, protected members can only be
accessed by the child classes and private members cannot be accessed outside the same
class (Smith, 2015).
Polymorphism: Polymorphism is the term that is used to describe the quality of an
element that allows it to exist in more than one form. In object oriented programming,
polymorphism refers to the ability via which more than one method are able to exist with
the same name but performing a different set of actions. This is done through passing a
variety of parameter or argument types and numbers. There are two types of
polymorphism namely, static polymorphism and dynamic polymorphism. Method or
constructor overloading is the form of static polymorphism. Through this, multiple
method instances can be created to perform a variety of operations however they contain
Document Page
3
OBJECT ORIENTED PROGRAMMING WITH C#
the same name. The compiler calls the right method or constructor based on the
parameter list that has been passed as the arguments. This is also known as Compile-time
polymorphism. Method overriding one the other hand is an example of dynamic
polymorphism where the methods to be called are determined at run-time. This is
applicable when inherited classes try to reinitialize and use methods of the same name as
that of their parent class (Mezini, 2013).
Task 2
Rectangle Class (Rectangle.cs)
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApp1
{
class Rectangle
{
private double height;
private double width;
public Rectangle(double height, double width)
{
this.height = height;
this.width = width;
}
public double CalcArea()
{
return height * width;
}
}
}
Driver class (Calculate.cs)
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
tabler-icon-diamond-filled.svg

Secure Best Marks with AI Grader

Need help grading? Try our AI Grader for instant feedback on your assignments.
Document Page
4
OBJECT ORIENTED PROGRAMMING WITH C#
namespace ConsoleApp1
{
class Calculate
{
public static void Main(String[] args)
{
Rectangle obj = new Rectangle(10.5, 12);
Console.WriteLine("The area of the rectangle = "+obj.CalcArea()+" sq.
meters");
Console.ReadKey();
}
}
}
Output
Document Page
5
OBJECT ORIENTED PROGRAMMING WITH C#
References
Mezini, M. (2013). Variational Object-Oriented Programming Beyond Classes and
Inheritance (Vol. 470). Springer Science & Business Media.
Smith, B. (2015). Object-oriented programming. In Advanced ActionScript 3 (pp. 1-23). Apress,
Berkeley, CA.
chevron_up_icon
1 out of 6
circle_padding
hide_on_mobile
zoom_out_icon
[object Object]