Learn C# with Examples and Exercises
Question 1
Create class Rectangle with instance data and write methods to input, calculate area and print the area.
Answer
using System;
class Rectangle
{
double length;
double width;
double area;
public void setData()
{
Console.Write("Entre value for length ");
length = Convert.ToDouble(Console.ReadLine());
Console.Write("Entre value for Width ");
width = Convert.ToDouble(Console.ReadLine());
}
public void calArea()
{
area = length * width;
}
public void displayData()
{
Console.WriteLine("Lenght={0}", length);
Console.WriteLine("Width={0}", width);
Console.WriteLine("Area={0}", area);
}
public static void Main(string[] args)
{
Rectangle r1=new Rectangle();
r1.setData();
r1.calArea();
r1.displayData();
}
}
Note:
1) Save above program as Rectangle.cs
2) Complie using csc Rectangle.cs
3) Run the Executable by double click
Question 2
Write any c# class with all type of constructor
Answer
using System;
class NewStudent
{
string name = "Jhon";
int age = 30;
// Default Constructor
public NewStudent()
{
name = "White";
age = 45;
Console.WriteLine("Name={0}", name);
Console.WriteLine("Age={0}" , age);
}
// Parametriazed Constructor
public NewStudent(string x, int y)
{
name = x;
age = y;
Console.WriteLine("Name={0}", name);
Console.WriteLine("Age={0}" , age);
}
// Copy Constructor
public NewStudent(NewStudent S)
{
name = S.name;
age = S.age;
Console.WriteLine("Name={0}", name);
Console.WriteLine("Age={0}" , age);
}
// Private Constructor
private NewStudent(string a)
{
name = a;
Console.WriteLine("Name={0}", name);
}
public static void Main(string[] args)
{
NewStudent s1=new NewStudent("Ganesh",40);
NewStudent s2=new NewStudent();
}
}
Question 3
Write any class as parent , and write a child class by using parent class.
Answer
using System;
class Father
{
public string firstname = "White";
protected string bank = "Commercial";
internal string address = "16 Old Road";
string designation = "Director";
}
class Child : Father
{
public string secondname = "Red";
public string degree = "Science";
public void display()
{
Console.WriteLine("First Name is " + firstname);
//Console.WriteLine(designation); //Can't access because it is private
Console.WriteLine("Second Name is " + secondname);
Console.WriteLine("Address is " + address);
}
public static void Main(string[] args)
{
Child c1=new Child();
c1.display();
}
}
Question 4
Write a Console or Windows application to illustrate the concept of Overloading in C#.
Answer
using System;
class Rect
{
double length;
double width;
double area;
public Rect()
{
length=0;
width=0;
}
public void setData()
{
length=0;
width=0;
}
public void setData(double l)
{
length=l;
width=l;
}
public void setData(double l, double w)
{
length=l;
width=w;
}
public void calArea()
{
area = length * width;
}
public void displayData()
{
Console.WriteLine("Lenght={0}", length);
Console.WriteLine("Width={0}", width);
Console.WriteLine("Area={0}", area);
Console.WriteLine("------------------------------");
}
public static void Main(string[] args)
{
Rect r1=new Rect();
r1.calArea();
r1.displayData();
Rect r2=new Rect();
r2. setData(12);
r2.calArea();
r2.displayData();
Rect r3=new Rect();
r3. setData(10,15);
r3.calArea();
r3.displayData();
}
}
No comments:
Post a Comment