C# – Building classes in C# – Video

In this article I present the second solution from the homework from SoftUni – www.softUni.bg – concerning classes in C#.


This is the task:

Define a class Laptop that holds the following information about a laptop device: model, manufacturer, processor, RAM, graphics card, HDD, screen, battery, battery life in hours and price.

  • The model and price are mandatory. All other values are optional.
  • Define two separate classes: a class Laptop holding an instance of class Battery.
  • Define several constructors that take different sets of arguments (full laptop + battery information or only part of it). Use proper variable types.
  • Add a method in the Laptop class that displays information about the given instance
    • Tip: override the ToString() method
  • Put validation in all property setters and constructors. String values cannot be empty, and numeric data cannot be negative. Throw exceptions when improper data is entered.
Sample laptop description (full):   Sample laptop description (mandatory properties only)
model Lenovo Yoga 2 Pro model HP 250 G2
manufacturer Lenovo price 699.00 lv.
processor Intel Core i5-4210U (2-core, 1.70 – 2.70 GHz, 3MB cache)
graphics card Intel HD Graphics 4400
screen 13.3″ (33.78 cm) – 3200 x 1800 (QHD+), IPS sensor display
battery Li-Ion, 4-cells, 2550 mAh
battery life 4.5 hours
price 2259.00 lv.

Pretty much, we need to build two classes – one for the Laptop and one for the battery. Additionally, a third class is build, from where the two others are initialized. The process of building the code is present in the YouTube video, at the end of the article.
Here comes the code:

  • The class of the battery

  •  The class of the laptop:

The Class with the main method (called RealThing):


Enjoy it! ))


VBA Developer

Tagged with: ,