0% found this document useful (0 votes)
3K views7 pages

Kuvempu University: Laboratory Assignments Subject Code: BSIT - 61 L

The document contains a laboratory assignment on basics of .NET programming. It includes 12 questions asking students to write C# programs demonstrating various concepts like command line arguments, calculating area of shapes using classes and constructors, operator overloading, threading, exceptions etc. Sample code is provided as answers for some of the questions.

Uploaded by

Shirsendu Roy
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3K views7 pages

Kuvempu University: Laboratory Assignments Subject Code: BSIT - 61 L

The document contains a laboratory assignment on basics of .NET programming. It includes 12 questions asking students to write C# programs demonstrating various concepts like command line arguments, calculating area of shapes using classes and constructors, operator overloading, threading, exceptions etc. Sample code is provided as answers for some of the questions.

Uploaded by

Shirsendu Roy
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 7

Kuvempu University

Laboratory Assignments

Subject: Basics of .NET


Subject Code: BSIT – 61 L

1. Write a program in C# using command line arguments to display a welcome message.


The message has to be supplied as input in the command line.

Ans. class CommandLine


{
static void Main(string[] args)
{
try
{
string text="";
for (int i = 0; i < args.Length; i++)
text = text +" "+ args[i].ToString();
Console.WriteLine(text);
}
catch (Exception ex)
{
Console.WriteLine("Invalid Input!");
}

Console.ReadLine();
}
}

2. Write a program in C# to find the area of a circle, given its radius.

Ans. class CircleArea


{
const double pi = 3.14;
static void Main(string[] args)
{
try
{
Console.WriteLine("Please Enter The Radius of the Circle");
double radius = Double.Parse(Console.ReadLine());
double area = pi * radius * radius;
Console.WriteLine("The Area of the circle is : "+area.ToString());

}
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;
}

public int AreaRectangle()


{
int area = (length * bredth);
return area;
}
}
4. Write a program in C# to add and multiply two numbers and display the results.
Demonstrate the use of ref and out parameters in this program.

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.

7. Write a program in C# to demonstrate the usage of checked and unchecked


statements.

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.

10. Write a program for matrix multiplication in C#.

11. Write a program in C# to sort the students list based on their names. The students list
is stored in a string array.

12. Write a program in C# to demonstrate operator overloading.

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

public static void Main()

Complex c1 = new Complex(10,20);

c1.ShowXY(); // displays 10 & 20

Complex c2 = new Complex();

c2.ShowXY(); // displays 0 & 0

c2 = -c1;

c2.ShowXY(); // diapls -10 & -20

13. Write a program in C# to demonstrate boxing and unboxing opertation.

Ans.

class TestBoxing
{
static void Main()
{
int i = 797;
object o = i; // Implicit boxing

i = 456; // Change the contents of i

System.Console.WriteLine("The value-type value = {0}", i);


System.Console.WriteLine("The object-type value = {0}", o);
}
}

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. class ArithmeticExceptionExample


{
public static void Main()
{
try
{
Math.Sign (Single.NaN);
}
catch (ArithmeticException e)
{
Console.WriteLine ("Exception: {0}", e);
}
}
}
15. Write a program to demonstrate the usage of threads in C#.

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.");
}
}
};

public class Simple


{
public static int Main()
{
Console.WriteLine("Thread Start/Stop/Join Sample");
Alpha oAlpha = new Alpha();
// Create the thread object, passing in the Alpha.Beta method
// via a ThreadStart delegate. This does not start the thread.
Thread oThread = new Thread(new ThreadStart(oAlpha.Beta));
// Start the thread
oThread.Start();

// Spin for a while waiting for the started thread to become


// alive:
while (!oThread.IsAlive);

// Put the Main thread to sleep for 1 millisecond to allow oThread


// to do some work:
Thread.Sleep(1);
// Request that oThread be stopped
oThread.Abort();
// Wait until oThread finishes. Join also has overloads
// that take a millisecond interval or a TimeSpan object.
oThread.Join();

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;
}
}

You might also like