0% found this document useful (0 votes)
6 views9 pages

Prove

The document explains key object-oriented programming principles: abstraction, encapsulation, inheritance, and polymorphism, with examples from various projects. It illustrates how these principles enhance flexibility and reusability in code, allowing for easier modifications and additions. Specific implementations in projects like YouTube Videos, Online Ordering, Event Planning, and Exercise Tracking demonstrate the practical applications of these concepts.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views9 pages

Prove

The document explains key object-oriented programming principles: abstraction, encapsulation, inheritance, and polymorphism, with examples from various projects. It illustrates how these principles enhance flexibility and reusability in code, allowing for easier modifications and additions. Specific implementations in projects like YouTube Videos, Online Ordering, Event Planning, and Exercise Tracking demonstrate the practical applications of these concepts.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 9

Prove: Articulate—Programming with Classes

1. What is abstraction?
Abstraction is the process through which something complex is simplified. It involves
capturing the most important and essential aspects while discarding unnecessary
details. With abstraction, only the required elements are shown, while background
details are hidden.

1.1. How did I use abstraction in the final project?


In the project Abstraction with YouTube Videos, I used abstraction with the classes
Comment and Video. The Comment class abstracts the concept of a comment,
encapsulating the name of the commenter as well as the text of the comment. The
internal details of how the comments are managed are not shown.

The Video class abstracts the concept of a video, encapsulating attributes such as
the title, the author, the length, and the associated comments. Internally, it manages
the list of comments, the number of comments, and the information about the video.

1.2. How did using these principles help your final project become more flexible
for future changes?

Abstraction allows code to be more flexible. The Comment and Video classes can be
reused in different parts of the project or even in other projects. If there is a project
that handles comments and videos, these classes can be reused without needing to
reimplement them.
In the YouTube Videos code, we have the Comment class, and since the code
collects comments, it could also collect "likes". The Comment class can be adapted
accordingly:

FROM:

class Comment
{
public string CommenterName { get; private set; }
public string Text { get; private set; }

public Comment(string commenterName, string text)


{
CommenterName = commenterName;
Text = text;
}
}

TO:
class Comment
{
public string CommenterName { get; private set; }
public string Text { get; private set; }
public int Likes { get; private set; } = 0;

public Comment(string commenterName, string text)


{
CommenterName = commenterName;
Text = text;
}

public void Like()


{
Likes++;
}
}

** public int Likes { get; private set; } = 0; and public void Like() { Likes++; }
can be added to have the option to aggregate “likes”.

2. What is encapsulation?

Encapsulation means hiding or wrapping of data and methods into a single unit.

2.1. How did I use encapsulation in the final project - Online Ordering?
In the Online Ordering project, there are: Class Address, customer, product, and
order.
In the class Class Customer, there are encapsulated attributes like _name and
_address. These classes are private. On the other hand, Name and Address are
public properties and they give controlled access to the previous attributes (_name
and _address). There is a method: IsInUSA. Ths method encapsulates the logic for
determining if the customer is in the USA, using the address information.

2.2. How did using these principles help your final project become more flexible
for future changes?
This project is about sales, so, I think that is possible to add a discount feature to the
Order class. First must be added a private _discount attribute and a method to set
the discount. Then, in the class Program, specifically in the Main method, would be
possible to set the discount for an order.
FROM:
class Order
{
private List<Product> _products = new List<Product>();
private Customer _customer;

public List<Product> Products { get { return _products; } }


public Customer Customer { get { return _customer; } }

public Order(Customer customer)


{
_customer = customer;
}

public void AddProduct(Product product)


{
_products.Add(product);
}

public decimal GetTotalCost()


{
decimal totalCost = 0;
foreach (var product in _products)
{
totalCost += product.GetTotalCost();
}

totalCost += _customer.IsInUSA() ? 5 : 35;


return totalCost;
}

public string GetPackingLabel()


{
string packingLabel = "Packing Label:\n";
foreach (var product in _products)
{
packingLabel += $"Name: {product.Name}, Product ID: {product.ProductId}\
n";
}
return packingLabel;
}

public string GetShippingLabel()


{
return $"Shipping Label:\n{_customer.Name}\
n{_customer.Address.GetFullAddress()}";
}
}

TO:
class Order
{
private List<Product> _products = new List<Product>();
private Customer _customer;
private decimal _discount = 0;

public List<Product> Products { get { return _products; } }


public Customer Customer { get { return _customer; } }

public Order(Customer customer)


{
_customer = customer;
}

public void AddProduct(Product product)


{
_products.Add(product);
}

public void SetDiscount(decimal discount)


{
_discount = discount;
}

public decimal GetTotalCost()


{
decimal totalCost = 0;
foreach (var product in _products)
{
totalCost += product.GetTotalCost();
}

totalCost += _customer.IsInUSA() ? 5 : 35;


totalCost -= _discount;
return totalCost;
}

public string GetPackingLabel()


{
string packingLabel = "Packing Label:\n";
foreach (var product in _products)
{
packingLabel += $"Name: {product.Name}, Product ID: {product.ProductId}\
n";
}
return packingLabel;
}

public string GetShippingLabel()


{
return $"Shipping Label:\n{_customer.Name}\
n{_customer.Address.GetFullAddress()}";
}
}
In the class Program, specifically in the Main method, the discount is set:
// Orders
Order order1 = new Order(customer1);
order1.AddProduct(product1);
order1.AddProduct(product2);
order1.SetDiscount(10);

3. What is inheritance?
Inheritance establishes a parent-child relationship between two classes. Thanks to
inheritance, child classes automatically inherit the methods and properties of the parent
class.
Inheritance aids in the reusability and abstraction of code. It allows a method to be
reused in multiple places.

3.1. How did I use inheritance in the final project?


In the project of Event Planning there is the class event which serves as an abstract
base class. It has properties and methods that are shared by all types of events,
such as Title, Description, Date, Time, and Address.
It also provides default implementations for some methods, like GetStandardDetails
and GetShortDescription, and declares an abstract method GetFullDetails that must
be overridden by derived classes.

3.2. How did using these principles help your final project become more flexible
for future changes?

If I want to add a new event type. It is possible without modifying the existing Event
class.It is possible to add the new class and add it to the list of events.

FROM:
class Program
{
static void Main(string[] args)
{
Address address1 = new Address("123 Main St", "Anytown", "NY", "USA");
Address address2 = new Address("456 Maple Ave", "Othertown", "CA", "USA");
Address address3 = new Address("789 Oak Blvd", "Thistown", "TX", "USA");

List<Event> events = new List<Event>


{
new Lecture("Lecture", "Introduction of the lecture.", new DateTime(2024, 6,
1), "10:00 AM", address1, "Dr. John Doe", 100),
new Reception("Wedding Reception", "Join us for a wedding celebration",
new DateTime(2024, 6, 15), "6:00 PM", address2, "[email protected]"),
new OutdoorGathering("Picnic in the Park", "Family-friendly picnic", new
DateTime(2024, 7, 4), "12:00 PM", address3, "Sunny")
};

foreach (var eventItem in events)


{
Console.WriteLine(eventItem.GetStandardDetails());
Console.WriteLine();
Console.WriteLine(eventItem.GetFullDetails());
Console.WriteLine();
Console.WriteLine(eventItem.GetShortDescription());
Console.WriteLine(new string('-', 40));
}
}
}

TO:

class Program
{
static void Main(string[] args)
{
Address address1 = new Address("123 Main St", "Anytown", "NY", "USA");
Address address2 = new Address("456 Maple Ave", "Othertown", "CA", "USA");
Address address3 = new Address("789 Oak Blvd", "Thistown", "TX", "USA");

List<Event> events = new List<Event>


{
new Lecture("Lecture", "Introduction of the lecture.", new DateTime(2024, 6,
1), "10:00 AM", address1, "Dr. John Doe", 100),
new Reception("Wedding Reception", "Join us for a wedding celebration",
new DateTime(2024, 6, 15), "6:00 PM", address2, "[email protected]"),
new OutdoorGathering("Picnic in the Park", "Family-friendly picnic", new
DateTime(2024, 7, 4), "12:00 PM", address3, "Sunny"),
new Event("New event", "New event", new DateTime(2024, 8, 10), "9:00 AM",
address1, "Jane Smith", 180)
};

foreach (var eventItem in events)


{
Console.WriteLine(eventItem.GetStandardDetails());
Console.WriteLine();
Console.WriteLine(eventItem.GetFullDetails());
Console.WriteLine();
Console.WriteLine(eventItem.GetShortDescription());
Console.WriteLine(new string('-', 40));
}
}
}

In this example, the New event class integrates seamlessly with the existing code,
demonstrating the flexibility by inheritance.

4. What is polymorphism?

4.1. How did I use polymorphism in the final project?


In the project Exercise Tracking there is the base Class Activity, this Activity class
serves as a base class for all types of activities, defining common properties
(Date and LengthInMinutes) and methods (GetDistance, GetSpeed, GetPace,
and GetSummary).
These methods are declared as virtual or abstract, allowing derived classes to
provide their specific implementations.

In this part of the code:

var activities = new Activity[]


{
new Running(new DateTime(2024, 5, 1), 30, 3.0),
new Running(new DateTime(2024, 5, 1), 30, 4.8),
new Cycling(new DateTime(2024, 5, 2), 45, 20.0),
new Swimming(new DateTime(2024, 5, 3), 60, 15)
};

foreach (var activity in activities)


{
Console.WriteLine(activity.GetSummary());
}

The loop, activity.GetSummary() calls the appropriate GetSummary method for each
specific activity type, demonstrating polymorphic behavior.

4.2. How did using these principles help your final project become more flexible
for future changes?

It is possible to create a new activity type, such as Hiking, by creating a new class
that inherits from Activity and overrides the necessary methods. The existing code
will handle the new activity type without modification, like this:

class Hiking : Activity


{
public double DistanceInMiles { get; private set; }

public Hiking(DateTime date, int lengthInMinutes, double distanceInMiles)


: base(date, lengthInMinutes)
{
DistanceInMiles = distanceInMiles;
}

public override double GetDistance()


{
return DistanceInMiles;
}

public override double GetSpeed()


{
return DistanceInMiles / (LengthInMinutes / 60.0);
}

public override double GetPace()


{
return LengthInMinutes / DistanceInMiles;
}

public override string GetSummary()


{
return $"{base.GetSummary()} Hiking - Distance: {DistanceInMiles:F1} miles,
Speed: {GetSpeed():F1} mph, Pace: {GetPace():F2} min/mile";
}
}

You might also like