Академический Документы
Профессиональный Документы
Культура Документы
Laboratory Assignments
Console.ReadLine();
}
}
}
catch (Exception ex)
{
Console.WriteLine("Invalid Input!");
}
Console.ReadLine();
}
}
3. Write a program in C# to find the area of polygon – triangle and rectangle by using
multiple constructors.
Ans:
class Program
{
const double pi = 3.14;
static void Main(string[] args)
{
try
{
Polygon tri = new Polygon(2.5, 6.0);
Polygon rect = new Polygon(6, 2);
Console.WriteLine("Area of Triangle :
"+tri.AreaTriangle().ToString());
Console.WriteLine("Area of Rectangle : " +
rect.AreaRectangle().ToString());
Console.ReadLine();
}
catch (Exception ex)
{
Console.WriteLine("Invalid Input!");
}
Console.ReadLine();
}
}
class Polygon
{
double bas, hght;
int length, bredth;
public Polygon(int h, int w)
{
this.bredth = h;
this.length = w;
}
public Polygon(double b, double h)
{
this.bas = b;
this.hght = h;
}
public double AreaTriangle()
{
double area = (0.5) * (bas * hght);
return area;
}
5. Using foreach looping statement, write a program in C# to display all the elements of
string array.
6. Write a program in C# to create a string array to store 10 employees name. Display the
employees name to the console by passing array as a parameter to the display
function.
8. Write a class called rectangle which consists of members side_1, side_2 and
displayArea(). Write another class square which inherits class rectangle. Take side of
square as input and display the area of square on console.
9. Write a program to sort 20 decimal numbers in decreasing order and print the result.
11. Write a program in C# to sort the students list based on their names. The students list
is stored in a string array.
using System;
class Complex
{
private int x;
private int y;
public Complex()
{
}
public Complex(int i, int j)
{
x = i;
y = j;
}
public void ShowXY()
{
Console.WriteLine(\”{0} {1}\”,x,y);
}
public static Complex operator -(Complex c)
{
Complex temp = new Complex();
temp.x = -c.x;
temp.y = -c.y;
return temp;
}
}
class MyClient
c2 = -c1;
Ans.
class TestBoxing
{
static void Main()
{
int i = 797;
object o = i; // Implicit boxing
class TestUnboxing
{
static void Main()
{
int i = 797;
object o = i; // implicit boxing
try
{
int j = (int) o; // attempt to unbox
System.Console.WriteLine("Unboxing Possible.");
}
catch (System.InvalidCastException e)
{
System.Console.WriteLine("{0} Error: Incorrect unboxing.", e.Message);
}
}
}
14. Write a program in C# to throw and catch any arithmetic exception.
Ans:
using System;
using System.Threading;
public class Alpha
{
// This method that will be called when the thread is started
public void Beta()
{
while (true)
{
Console.WriteLine("Alpha.Beta is running in its own thread.");
}
}
};
Console.WriteLine();
Console.WriteLine("Alpha.Beta has finished");
try
{
Console.WriteLine("Try to restart the Alpha.Beta thread");
oThread.Start();
}
catch (ThreadStateException)
{
Console.Write("ThreadStateException trying to restart Alpha.Beta. ");
Console.WriteLine("Expected since aborted threads cannot be restarted.");
}
return 0;
}
}