Lab 4
Lab 4
To study and controlling data members using access specifiers, static class members and ENCAPSULATION USING ACCESSORS AND
MUTATORS
Encapsulation
Encapsulation is defined 'as the process of enclosing one or more items within a physical or logical package'. Encapsulation, in object-oriented
programming methodology, prevents access to implementation details. Encapsulation is used to restrict access to the members of a class so as to
prevent the user of a given class from manipulating objects in ways that are not intended by the designer. While encapsulation hides the internal
implementation of the functionalities of class without affecting the overall functioning of the system, it allows the class to service a request for
functionality and add or modify its internal structure (data or methods) to suit changing requirements.
Encapsulation in C# is implemented with different levels of access to object data that can be specified access modifiers.
Access modifiers
Encapsulation in C# is implemented with different levels of access to object data that can be specified using the following access modifiers:
Tasks
Task:
2. Write a program to explain method in C#. Create a static function factorial() that accept a number from user and
returns factorial of the number.
Input:
Output:
3. Write a main method for your Vehicle class that creates a few vehicles and prints out their field values. Make the fields in
your Vehicle class private, and add accessor and mutator methods for the fields.
Input:
Output:
5. Create a class called Student that an institute might use to represent a record for students qualifying from the institute. A Student record
should include five pieces of information as either instance variables or auto-implemented properties: a student's id (type string), a student's
name (type string) and three separate variables for scores in three subjects (type decimal). Your class should have a constructor that
initializes the five values. Provide a property with a get and set accessor for any instance variables. For the scores in subjects, if the value
passed to the set accessor is negative, the value of the instance variable should be left unchanged. Also, provide methods named
GetAggregate and GetPercentage that calculate the aggregate marks in the three subjects (sum of three subject marks) and the percentage
(i.e., sum divided by the maximum marks, 50, and then multiplied by 100), and then return the aggregate and percentage as decimal value.
Write a test app named StudentRecordTest that demonstrates class Student’s capabilities.
Input:
Output: