Labsheet C#
Labsheet C#
LABSHEET
Q1. Given the structure of two classes. Implement the given inheritance and test the class.
Methods and its return types are specified in the diagram. toString() method both in the
class will display the value of its data members.
a. In the given class write down Properties for accessing both X and Y and test it in
main method
Sol:
using System;
Q2. Create a class Circle which represent a real circle object , with the data
Radius – double type
Color – string type
Include three types of Constructors in the class. Create three different
object with the help of three constructors defined in the class
Add methods getRadius() and getColor() to return the radius and color
respectively
Add methods setRadius(double d) and setColor(String c) to set the
value of radius and color respectively.
Add a method getArea() will calculate and return the area of the
Circle.
Test the class in main method.
A subclass called Cylinder is derived from the superclass Circle. Cylinder class having
the
following details
height – double type
Cylinder() – default constructor
Cylinder(radius ) – will initialize the radius with some value and all
other data with default value
Cylinder(radius, height ) - will initialize the radius and height with
some value and color with default value
Cylinder(radius, height, Color) will initialize the radius , height and
color.
getHeight() and setHeight(height) to return and assign value to height
respectively
getVolume() – calculate and return the volume of the cylinder
(volume= area of circle* height)
The subclass Cylinder invokes the superclass' constructors (via base() and base(radius)
etc) and
inherits the variables and methods from the superclass Circle.
Sol:
using System;
public Circle()
{
radius = 0.0;
color = "red";
}
class Program
{
static void Main()
{
Circle circle1 = new Circle();
Circle circle2 = new Circle(5.0);
Circle circle3 = new Circle(3.0, "blue");
Console.WriteLine("Circle 1 - Radius: " + circle1.getRadius() + ", Color: " + circle1.getColor() + ", Area: " +
circle1.getArea());
Console.WriteLine("Circle 2 - Radius: " + circle2.getRadius() + ", Color: " + circle2.getColor() + ", Area: " +
circle2.getArea());
Console.WriteLine("Circle 3 - Radius: " + circle3.getRadius() + ", Color: " + circle3.getColor() + ", Area: " +
circle3.getArea());
Q3. Create a Student class which will have two private arrays to store full student names
in a class and to store the cgpa of each student respectively.
Class has a private data Count which will have the number of students in the class.
- Write down the proper constructor methods ‘
- Include the Indexer for accessing the above-mentioned private arrays in the class.
- Include methods to add new student name and marks to corresponding arrays.
- Include methods to display the name and cgpa of each student in a neat manner.
- Write down the property for count data.
- Test all methods in the class.
Sol:
using System;
class Program
{
static void Main()
{
Student studentClass = new Student(5);
studentClass.AddStudent("Alice", 3.5);
studentClass.AddStudent("Bob", 3.8);
studentClass.AddStudent("Charlie", 3.2);