output - udayacsmaterials.files.wordpress.com€¦ · web viewthus the program has been completed...
TRANSCRIPT
1
EXNO: 11 NAME :DATE: REG NO: RA16310050200
USING TRY, CATCH AND FINALLY BLOCKS WRITE A PROGRAM IN C# TO DEMONSTRATE ERROR HANDLING.
AIM:
To using try, catch and finally blocks write a program in c# to demonstrate error handling.
PROGRAM:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace errorhandling
{
class Program
{
static void Main(string[] args){int[] a = new int[3];
int n = args.Length;
try
{
if (n == 0)
{
int d = 10 / (n);
2
}
if (n == 1)
{
a[4] =6;}
catch (IndexOutOfRangeException e)
{
Console.WriteLine("Exception"+e);
}
catch (DivideByZeroException e)
{
Console.WriteLine("DivideByZeroException"+e);
}
finally
{
Console.WriteLine("finally block :: End of Program");
}
Console.ReadLine();
}
}
}
3
Output
RESULT:
Thus the program has been completed successfully.
4
EXNO: 12 NAME :DATE: REG NO: RA16310050200
DESIGN A SIMPLE CALCULATOR USING SWITCH STATEMENT IN C#.
AIM:
To design a simple calculator using switch statement in c#.
PROGRAM:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Simplecalculator
{
class Program
{
static void Main(string[] args){
float a, b; int ch;
Console.Write("Enter The First No.: ");
a = float.Parse(Console.ReadLine());
Console.Write("\nEnter the Second No.: ");
b = float.Parse(Console.ReadLine());
while (true)
{Console.WriteLine("===============================");
5
Console.WriteLine("1.Addition\n2.Subtraction\n3.Multiplication\n4.Division\n5.Moduler Division\n6.Square\n7.Square Root\n8.Exit");
Console.WriteLine("===============================");Console.Write("Enter your Choice : ");ch = int.Parse(Console.ReadLine());
switch (ch)
{case 1:
Console.WriteLine("Addition :" + a + "+" + b + "=" + (a + b));
break;
case 2:
Console.WriteLine("Subtraction :" + a + "-" + b + "=" + (a - b));
break;
case 3:
Console.WriteLine("Multiplication :" + a + "*" + b + "=" + (a * b));
break;
case 4:
Console.WriteLine("Division :" + a + "/" + b + "=" + (a / b));
break;
case 5:
Console.WriteLine("Moduler Division:" + a + "%" + b + "=" + (a % b));
break;
case 6:
Console.WriteLine("Square(" + a + ") =" + (a * a));
break;
case 7: Console.WriteLine("SquareRoot(" + a + ") =" + Math.Sqrt(a));
break;
6
default: Console.WriteLine("Invalid Input");
Environment.Exit(0);
break;
}
}
}
}
}
7
Output
RESULT:
Thus the program has been completed successfully.
8
EXNO: 13 NAME :DATE: REG NO: RA16310050200
DEMONSTRATE USE OF VIRTUAL AND OVERRIDE KEY WORDS IN C# WITH A SIMPLE PROGRAM.
AIM:
To demonstrate use of virtual and override key words in c# with a simple program.
PROGRAM:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace VirtualKey
{
class person
{
protected string fname;
protected string lname;
public person(string fn, string ln)
{
fname = fn;
lname = ln;
}
public virtual void display()
9
{
Console.WriteLine("Person :" + fname + " " + lname);
}
}
class emp : person
{public ushort year;
public emp(string fn, string ln, ushort yr): base(fn, ln)
{
year = yr;
}
public override void display()
{
Console.WriteLine("Employee :"+fname+" "+lname+" "+year);
}
}
class worker : person
{
public String company;
public worker(string fn, string ln, string c):base(fn, ln)
{
company=c;
}
public override void display()
{
Console.WriteLine("Worker :" + fname + " " + lname + " " +company);
10
}}
class Program
{static void Main(string[] args)
{
Console.WriteLine("\n\n*** VIRTUAL AND OVERRIDE KEYWORDS DEMO ***");
person p1 = new person("RAM", "KUMAR");
person p2 = new emp("RAM", "KUMAR",2012);
person p3 = new worker("RAM", "KUMAR","ABC TECH SOLS");
p1.display();
p2.display();
p3.display();
Console.ReadLine();
}
}
}
Output
RESULT:
Thus the program has been completed successfully.
11
EXNO: 14 NAME :DATE: REG NO: RA16310050200
IMPLEMENT LINKED LISTS IN C# USING THE EXISTING COLLECTIONS NAME SPACE.
AIM:
To implement linked lists in c# using the existing collections name space.
PROGRAM:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace linkedlist
{
class Program
{
static LinkedList<int> ll = new LinkedList<int>();
static LinkedListNode<int> node;
static void Main(string[] args)
{
Console.WriteLine(" LINKED LIST DEMO");
int ch, x;
Console.WriteLine("Linked List Operations");
Console.WriteLine("1.AddFirst\n2.AddLast\n3.RemoveFirst\n4.RemoveLast\n5.Remove Specified\n6.Display\n7..Exit");
12
while (true)
{
Console.Write("Enter your Choice : ");
ch = int.Parse(Console.ReadLine());
switch (ch)
{
case 1: Console.Write("Enter the Element to AddFirst : ");
x = int.Parse(Console.ReadLine());
ll.AddFirst(x);
display();
break;
case 2:
Console.WriteLine("Enter the Element to AddLast : ");
x = int.Parse(Console.ReadLine());
ll.AddLast(x);
display();
break;
case 3:
if (ll.Count == 0)
{
Console.WriteLine("Nothing to Delete...!!!");
break;
}
else
{
13
Console.WriteLine("First Element Removed Successfully");
ll.RemoveFirst();
display();
break;
}
case 4: if (ll.Count == 0)
{
Console.WriteLine("Nothing to Delete...!!!");break;
}
else
{
Console.WriteLine("Last Element Removed Successfully");
ll.RemoveLast();
display();
break;
}
case 5: if (ll.Count == 0)
{
Console.WriteLine("Nothing to Delete...!!!");
break;
}
else
{
14
Console.WriteLine("Enter the Element to Remove");
x = int.Parse(Console.ReadLine());
bool b=ll.Remove(x);
if (b == true)
{
Console.WriteLine("Element Removed Successfully");
display();
break;
}else {
Console.WriteLine("Specified Node Does not Exist");
break;
}
}
case 6:
display();
break;
default: Environment.Exit(0);
break;
}
}
}
public static void display()
{
if (ll.Count == 0)
15
{
Console.WriteLine("Nothing to Display...!!!");}else{
Console.Write("Elements in the List are:");
for (node = ll.First; node != null; node=node.Next)
Console.Write(node.Value+" ");
Console.WriteLine();
}
}
}
}
Output
RESULT:
Thus the program has been completed successfully.
16
EXNO: 15 NAME :DATE: REG NO: RA16310050200
WRITE A PROGRAM TO DEMONSTRATE ABSTRACT CLASS AND ABSTRACT METHODS IN C#.
AIM:
To write a program to demonstrate abstract class and abstract methods in c#.
PROGRAM:
using System;
namespace TryCatch {
abstract class person
{ protected string
fname; protected string
lname;
public person(string fn, string ln) {
fname = fn;
lname = ln;
}
public abstract void display()
{ Console.WriteLine("Person :" + fname + " " +
lname);
} }
class emp : person
17
{
public ushort year;
public emp(string fn, string ln, ushort yr): base(fn, ln)
{ year = yr;
}
public override void display() {
Console.WriteLine("Employee :" + fname + " " + lname + " " + year);} }class worker : person
{ public String company;
public worker(string fn, string ln, string c): base(fn, ln)
{
company = c;
}
public override void display() {
Console.WriteLine("Worker :" + fname + " " + lname + " " + company);
} }
class Program
{
static void Main(string[] args) {
Console.WriteLine("**ABSTRACT CLASS AND ABSTRACT METHODS DEMO **");
person p2 = new emp("RAM", "KUMAR", 2012);
person p3 = new worker("RAM", "KUMAR", "ABC TECH SOLS");
p2.display();
p3.display();
Console.ReadLine();
18
}
}
}Output
RESULT:
Thus the program has been completed successfully.
19
EXNO: 16 NAME :DATE: REG NO: RA16310050200
WRITE A PROGRAM IN C# TO BUILD A CLASS WHICH IMPLEMENTS AN INTERFACE WHICH ALREADY EXISTS.
AIM:
To write a program in c# to build a class which implements an interface this already exists.
PROGRAM:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Infc
{
class Point:ICloneable
{
public int x, y;
public Point(int x, int y) {
this.x = x;
this.y = y;
}
public object Clone() {
return new Point(this.x, this.y);
}
20
public override string ToString()
{
return string.Format("X= {0}; y={1}", x, y);
}
}
class Porgram{static void Main(string[] args)
{
Point p1 =new Point(10,10);
Point p2
=(Point)p1.Clone(); p2.x =
20;
Console.WriteLine(p1);
Console.WriteLine(p2);
Console.Read();
}
}
}
21
Output
RESULT:
Thus the program has been completed successfully.
22
EXNO: 17 NAME :DATE: REG NO: RA16310050200
WRITE A PROGRAM TO ILLUSTRATE THE USE OF DIFFERENT PROPERTIES IN C#.
AIM:
To write a program to illustrate the use of different properties in c#.
PROGRAM:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Properties
{
class point
{
int getx, gety;
public int x
{
get { return getx; }
set { getx = value; }
}
public int y
{
get { return gety; }
23
set { gety = value; }
}
}
class Program
{static void Main(string[] args)
{
point start = new point();
point end = new point();
start.x = 10;
start.y = 20;
end.x = 100;
end.y = 200;
Console.Write("\npoint 1 :" + start.x + " " + end.x);
Console.Write("\npoint 2 :" + start.y + " " + end.y);
Console.ReadLine();
}
}
}
24
Output
RESULT:
Thus the program has been completed successfully.
25
EXNO: 18 NAME :DATE: REG NO: RA16310050200
DEMONSTRATE ARRAYS OF INTERFACE TYPES WITH A C# PROGRAM.
AIM:
To demonstrate arrays of interface types with a c# program.
PROGRAM:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace IntrDemo
{ public interface Shape
{ void area();
}
public class Circle : Shape {
public void area() {
Console.WriteLine("*** Calculating Area of Circle ***");
Console.Write("Enter the Radius:");
float r = float.Parse(Console.ReadLine());
Console.WriteLine("Area of Circle = " + (3.142 * r * r));
}
}
public class Square : Shape {
public void area() {
26
Console.WriteLine("*** Calculating Area of Square ***");
Console.Write("Enter the Length:");
float side = float.Parse(Console.ReadLine());
Console.WriteLine("Area of Square = " + (side * side));
}}
class Program
{
static void Main(string[] args)
{
Console.WriteLine("*** Arrays of Inerface Demo ***");
Shape[] s = new Shape[2];
s[0] = new Circle();
s[1] = new Square();
for (int i = 0; i < s.Length; i++)
{
s[i].area();
Console.ReadLine();
}
}
}
}
27
Output
RESULT:
Thus the program has been completed successfully.