Net practicals lab mannual

Preview:

Citation preview

.NET PRECTICALS

UTTRANCHAL INSTITUTE OF MANAGEMENT

PACTICAL REPORT ON

C#

DOT NETMASTER of COMPUTER APPLICATION

Submitted To: - Submitted By:-Rahul singh Abhishek kr pathak UIM, Dehradun MCA 4th SEM,

1

.NET PRECTICALS

UIM, Dehradun

2

.NET PRECTICALS

List of ProgramsSerial Number

Name of the program Remark

1 Write a program to display a hello/welcome message2 WAP for Demonstrating Data Type Conversion3 WAP for Demonstrating String Handling4 Given the radius of the circle, WAP to compute the

area of the circle, circumference of the circle and display their value.

5 Admission to a professional course is subject to the following condMks in maths>=80;Mks in Physics>=75;Mks in chem.>=60; Total Mks all three>=250 or total in maths and physics>= 180Given the details about the mks, WAP to process the applications to list the eligible candidates.

6 WAP that will read the value of x and evaluate the following functionY= { 1 for x > 0; 0 for x = 0; -1 for x < 0 }Using a) nested if statements b) else if statements and c) conditional operators?

7 shown below is the Floyd’s triangle 12345678910...79………….91WAP to print this triangle.

8 WAP to produce the following patterns* $$$$ 1** $$$ 22 *** $$ 333 **** $ 4444

9 WAP to generate and then sum the Fibonacci series10 Demonstrate the typical use of the following jump

statements: break continue

In a single program

3

.NET PRECTICALS

11 WAP for Declaring Class and invokeing a method.12 WAP to explain the concept of boxing and unboxing13 WAP to demonstrate the addition of byte type of

variables14 WAP for using System.Collection Namespace 15 WAP for Implementing Jagged Arrays16 Given the two one-dimensional arrays A and B which

are sorted in ascending order. WAP to merge them into a single sorted array C that contains every items from arrays A and B in ascending order

17 WAP for Using Reflection Class18 WAP for Using GDI+ Classess

19 WAP for Displaying Constructors and Destructors

20 WAP to implement Single inheritance

21 WAP to implement MultiLevel inheritance22 WAP to implement Polymorphism23 WAP to implement Operator Overloading

24 WAP to deal with Exception Handling

25 WAP fo displaying Randomly Generated button and Click Event

26 WAP to Demonstrate Multi Threading Example

27 WAP to implement delegates

28 WAP to implement multicast delegates

29 WAP for displaying Data Handling Application

30 WAP to use Window Form Application

31 Design and develop application as of your own choice

4

.NET PRECTICALS

1.Write a program to display a hello/welcome messageusing System;using System.Collections.Generic;using System.Linq;using System.Text;

namespace ConsoleApplication2{ class Program { static void Main(string[] args) { Console.Write("Welcome MCA in UIM"); Console.ReadKey(); } }}

5

.NET PRECTICALS

2. WAP for Demonstrating Data Type Conversionusing System;using System.Collections.Generic;using System.Linq;using System.Text;

namespace ConsoleApplication3{ class Program { static void Main(string[] args) { string str1 = "123.45"; Single sngl1 = Convert.ToSingle(str1); int x = Convert.ToInt16(sngl1);

Console.WriteLine("Single 1 = {0}", sngl1); Console.WriteLine("Integer 1 = {0}", x);

double y = Convert.ToDouble(str1); Console.WriteLine("Double = {0}", y); Int16 z = Convert.ToInt16(y); Console.WriteLine("Int16 = {0}", z); Console.ReadLine(); } }}

6

.NET PRECTICALS

3.. WAP for Demonstrating String Handlingusing System;using System.Collections.Generic;using System.Linq;using System.Text;

namespace String_Handling{ class Program { static void Main(string[] args) { String s1 = "preeti shahi"; String s2 = s1.Insert(2, "i"); Console.WriteLine(s2);

String s3 = "preeti shahi"; Boolean b = s3.Equals(s1); Console.WriteLine(b.ToString());

Console.WriteLine(s1.ToLower()); Console.WriteLine(s1.ToUpper());

String s4 = s1.Substring(5); Console.WriteLine(s4); Console.ReadKey();

} }}

7

.NET PRECTICALS

4 . Given the radius of the circle, WAP to compute the area of the circle, circumference of the circle and display their value.using System;using System.Collections.Generic;using System.Linq;using System.Text;

namespace Circle{ class Program { static void Main(string[] args) { const double pi = 3.14; Console.WriteLine("Enter the radius of the circle"); double r = Convert.ToDouble(Console.ReadLine()); double area = pi * r * r; Console.WriteLine("Area is:" + area.ToString()); double curc = 2 * pi * r; Console.WriteLine("Circumference is:" + curc.ToString()); Console.ReadKey();

} }

8

.NET PRECTICALS

5.Admission to a professional course is subject to the following condMks in maths>=80;Mks in Physics>=75;Mks in chem.>=60; Total Mks all three>=250 or total in maths and physics>= 180Given the details about the mks, WAP to process the applications to list the eligible candidates.using System;

class Program { static void Main(string[] args) { Console.WriteLine("Enter the marks of Maths,Physics and Chemistry"); int Math = int.Parse(Console.ReadLine()); int Phys = int.Parse(Console.ReadLine()); int Chem = int.Parse(Console.ReadLine()); int total = Math + Phys + Chem; int MP = Math + Phys; if (total >= 250 || MP >= 180) { if (Math >= 80 && Phys >= 75 && Chem >= 60) { Console.WriteLine("Candidates is Eligible"); } else { Console.WriteLine("Candidates is not Eligible"); } } else { Console.WriteLine("Candidates is not Eligible"); } Console.ReadKey(); } }

9

.NET PRECTICALS

10

.NET PRECTICALS

6.WAP that will read the value of x and evaluate the following functionY= { 1 for x > 0; 0 for x = 0; -1 for x < 0 }Using a) nested if statements b) else if statements and c) conditional operators?using System;using System.Collections.Generic;using System.Linq;using System.Text;

namespace Evaluate_x{ class Program { static void Main(string[] args) { Console.WriteLine("Enter the value of X"); int x = int.Parse(Console.ReadLine()); int y = 0; //Using Nested If Statement if (x > 0) { y = 1; } else if (x == 0) { y = 0; } else if (x < 0) { y = -1; } Console.WriteLine("Using else-if Statement:" + y.ToString()); y = (x == 0) ? 0 : (x > 0) ? 1 : (x < 0) ? -1 : 0; Console.WriteLine("Using Conditional Operation:" + y.ToString()); Console.ReadKey(); } }}

11

.NET PRECTICALS

12

.NET PRECTICALS

7. shown below is WAP to show the Floyd’s triangle on the basis of rowsusing System;

class Program { static void Main(string[] args) { int n, i, c, a = 1; Console.WriteLine("Enter the number of rows till you want to print"); n = int.Parse(Console.ReadLine()); for (i = 1; i <= n; i++) { for (c = 1; c <= i; c++) { Console.Write(a.ToString()); a++; } Console.WriteLine(); } Console.ReadKey(); } }

13

.NET PRECTICALS

8. shown below is the Floyd’s triangle 12345678910...79………….91WAP to print this triangle.using System;

class Program{ static void Main(string[] args) { int n, i, c, a = 1; bool k = true; Console.WriteLine("Enter the number till you want to print"); n = int.Parse(Console.ReadLine()); for (i = 1; i <= n; i++) { for (c = 1; c <= i; c++) { if(a==n) { k=false; break; } Console.Write( a.ToString()); a++; } if (k == false) { break; } Console.WriteLine(); } Console.ReadKey(); }}

14

.NET PRECTICALS

9.WAP to produce the following patterns

15

.NET PRECTICALS

* $$$$ 1** $$$ 22 . *** $$ 333 **** $ 4444

using System;

class Program { static void Main(string[] args) { int n, c, k; Console.WriteLine("Enter The Number Of Rows"); n = int.Parse(Console.ReadLine()); //Number Tirangle for (c = 1; c <= n; c++) { for (k = 1; k <= c; k++) { Console.Write(c); }

Console.WriteLine(); } Console.WriteLine(); //Dollar Tirangle for (c = 1; c <= n; c++) { for (k = 1; k <= c; k++) { Console.Write("$"); }

Console.WriteLine(); } Console.WriteLine(); // Star Traingle for (c = n; c >= 1; c--) { for (k = 1; k <= c; k++) { Console.Write("*"); }

Console.WriteLine(); } Console.ReadKey(); } }

16

.NET PRECTICALS

10..WAP to generate and then sum the fibonacci series:-

17

.NET PRECTICALS

using System;

class Program { static void Main(string[] args) { Console.WriteLine("Enter the terms in Fibonacci series:-"); int n = int.Parse(Console.ReadLine()); int a = -1, b = 1, c = 0, sum = 0; for (int i = 1; i <= n; i++) { c = a + b; sum = sum + c; a = b; b = c; } Console.WriteLine(sum.ToString()); Console.ReadKey(); } }

11. Demonstrate the typical use of the following jump statements:

18

.NET PRECTICALS

break continue

In a single programusing System;

class Program { static void Main(string[] args) { //Continue Statement for (int i = 0; i <= 15; i++) { if (i == 5) { continue; } Console.Write(i.ToString() + " "); } Console.WriteLine();

//Break Statement for (int i = 0; i <= 15; i++) { if (i == 10) { break; } Console.Write(i.ToString() + " "); }

Console.ReadKey(); } }

19

.NET PRECTICALS

20

.NET PRECTICALS

12.WAP for Declaring Class and invoking a method.using System;

class Rect { int l, b; public Rect(int l, int b) { this.l = l; this.b = b; } public int getArea() { return l * b; } } class Program { static void Main(string[] args) { Rect a = new Rect(28, 15); Console.WriteLine("Area of rectangle is : " + a.getArea().ToString()); Console.ReadKey();

} }

21

.NET PRECTICALS

13.WAP to explain the concept of boxing and un boxingusing System; class Program { static void Main(string[] args) { int i = 123; object o = i;//Boxing Console.WriteLine("Boxing OK");

int j = (int)o; //UnBoxing System.Console.WriteLine("Unboxing OK");

try { int k = (short)o;//InvalidCastException Generated System.Console.WriteLine("Unboxing OK"); } catch (System.InvalidCastException e) { System.Console.WriteLine("Incorrect Unboxing.", e.Message); } Console.ReadKey();

} }

22

.NET PRECTICALS

14.WAP to demonstrate the addition of byte type of variables:-using System;class Program { static void Main(string[] args) { byte b1 = 88; byte b2 = 79; //byte sum = b1 + b2; error cannot implicitly convert int to byte int sum = b1 + b2; //No error Console.WriteLine(sum.ToString()); Console.ReadKey(); } }

23

.NET PRECTICALS

15. WAP for using System.Collection Namespaceusing System;using System.Collections;namespace system.collection_array_list{ class Program { static void Main(string[] args) { ArrayList n = new ArrayList(); n.Add("Preeti Shahi"); n.Add("Yamini Bisht"); n.Add("Gaurav Sharma"); n.Add("Sachin Sisodia"); n.Add("Abhishek Sharma"); n.Add("Abhishek Pathak"); Console.WriteLine("Capacity=" + n.Capacity); Console.WriteLine("Element present=" + n.Count); for (int i = 0; i < n.Count; i++) { Console.Write(n[i] + " "); } Console.WriteLine(); //Sorting Array List n.Sort(); for (int i = 0; i < n.Count; i++) { Console.Write(n[i] + " "); } Console.WriteLine();

//Removing An element n.RemoveAt(2); for (int i = 0; i < n.Count; i++) { Console.Write(n[i] + " "); } Console.ReadKey();

} }}

24

.NET PRECTICALS

25

.NET PRECTICALS

16.WAP for Implementing Jagged Arraysusing System;class Program { static void Main(string[] args) { const int rows = 3; int[][] jagArr = new int[rows][]; jagArr[0] = new int[2]; jagArr[1] = new int[3]; jagArr[2] = new int[4];

jagArr[0][1] = 67; jagArr[1][0] = 83; jagArr[1][1] = 57; jagArr[2][0] = 63; jagArr[2][3] = 14;

for (int i = 0; i < 2; i++) { Console.Write(jagArr[0][i] + " "); for (int j = 0; j < 3; j++) { Console.Write(jagArr[1][j] + " "); for (int k = 0; k < 4; k++) { Console.Write(jagArr[2][k] + " "); } Console.WriteLine(); } Console.WriteLine(); } Console.ReadKey();

} }

26

.NET PRECTICALS

27

.NET PRECTICALS

17. Given the two one-dimensional arrays A and B which are sorted in ascending order. WAP to merge them into a single sorted array C that contains every items from arrays A and B in ascending orderusing System; class Program { static void Main(string[] args) { int[] a = { 10, 20, 30, 40, 50, 60, 70, 80, 90 }; int[] b = { 18, 36, 54, 72, 90, 108, 126, 144, 120 }; int[] c = new int[a.Length + b.Length];

//Mearging To New Array C a.CopyTo(c, 0); b.CopyTo(c, a.Length);

//Sorting Array C for (int i = 0; i < c.Length; i++) { for (int j = i + 1; j < c.Length; j++) { if (c[i] > c[j]) { int temp = c[i]; c[i] = c[j]; c[j] = temp; } } }

for (int i = 0; i < c.Length; i++) { Console.WriteLine(c[i]); } Console.ReadKey(); } }

28

.NET PRECTICALS

29

.NET PRECTICALS

18. WAP for Using Reflection Classusing System;using System.Reflection;

namespace Wap_To_Reflection{ class Program { static void Main(string[] args) { Type t = typeof(System.String); ConstructorInfo[] ci = t.GetConstructors(); Console.WriteLine("Constructors are"); foreach (ConstructorInfo ctemp in ci) { Console.WriteLine(ctemp.ToString()); } Console.WriteLine(); Console.WriteLine("Methods are"); MethodInfo[] mifo = t.GetMethods(); foreach (MethodInfo mifot in mifo) { Console.WriteLine(mifot.ToString()); } Console.ReadKey(); } }}

30

.NET PRECTICALS

19. WAP for Using GDI+ Classesusing System;using System.Collections.Generic;using System.ComponentModel;using System.Data;using System.Drawing;using System.Linq;using System.Text;using System.Windows.Forms;

namespace WindowsFormsApplication6{ public partial class Form1 : Form { public Form1() { InitializeComponent(); }

private void button1_Click(object sender, EventArgs e) { Graphics g = CreateGraphics(); Color forecolor = Color.Black; Color backcolor = Color.White; Font font = new Font("Algerian", 26); g.FillRectangle(new SolidBrush(backcolor), ClientRectangle);

g.DrawString("Hello friends … m VICKY", font, new SolidBrush(forecolor), 15, 15);

Pen myPen = new Pen(new SolidBrush(forecolor), 2); Point p1 = new Point(40); Point p2 = new Point(90); g.DrawRectangle(myPen, 50, 150, 100, 50); g.DrawEllipse(myPen, 100, 50, 80, 40); g.FillEllipse(Brushes.Black, 100, 50, 80, 40);

} }}

31

.NET PRECTICALS

32

.NET PRECTICALS

20. WAP for Displaying Constructors and Destructorsusing System;using System.Collections.Generic;using System.Linq;using System.Text;

namespace Constructor_and_Distructor{ class Rect { private int l, b; public Rect(int l, int b) { Console.WriteLine("Constructor Call"); this.l = l; this.b = b; } public int area() { return l * b; } Rect() { Console.WriteLine("Destructor call"); } }

class Program { static void Main(string[] args) { Rect r = new Rect(32, 24); Console.WriteLine(r.area().ToString()); Console.ReadKey();

} }}

33

.NET PRECTICALS

34

.NET PRECTICALS

21. WAP to implement Single inheritanceusing System;

class Item { public void x() { Console.WriteLine("Item Code=***"); } } class Fruit : Item { public void type() { Console.WriteLine("Fruit type = Mango"); } } class SingleInheritance { static void Main(string[] args) { Item item = new Item(); Fruit fruit = new Fruit(); item.x(); fruit.type(); Console.ReadKey(); } }

35

.NET PRECTICALS

22. WAP to implement MultiLevel inheritanceusing System;class Student { public string name; public int age; public Student(string name, int age) { this.name = name; this.age = age; } } class Player : Student { public string sport; public string team; public Player(string sport, string team, string name, int age) : base(name, age) { this.sport = sport; this.team = team; }

} class CricketPlayer : Player { public int runs; public CricketPlayer(int runs, string sport, string team, string name, int age) : base(sport, team, name, age) { this.runs = runs; }

public void Show() { Console.WriteLine("Player:" + name); Console.WriteLine("Age:" + age); Console.WriteLine("Sport:" + sport); Console.WriteLine("Team:" + team); Console.WriteLine("Runs:" + runs); } } class Program { static void Main(string[] args) { CricketPlayer u = new CricketPlayer(210, "Cricket", "India", "Sachin Tendulkar", 43); u.Show(); Console.ReadKey();

} }

36

.NET PRECTICALS

37

.NET PRECTICALS

23. WAP to implement Polymorphismusing System;

class Fruit { public virtual void display() { Console.WriteLine("Seasonal Fruits"); } } class SeasonalFruit : Fruit { public override void display() { Console.WriteLine("Seasonal Fruit:Mango"); } } class NonSeasonalFruit : Fruit { public override void display() { Console.WriteLine("NonSeasonal Fruit:Grapes"); } } class InclusionPolymorphism { static void Main(string[] args) { Fruit m = new Fruit(); m = new SeasonalFruit();//Upcasting m.display(); m = new NonSeasonalFruit();//Upcasting m.display(); Console.ReadKey(); } }

38

.NET PRECTICALS

39

.NET PRECTICALS

24. WAP to implement Operator Overloadingusing System;class A{ int a, b; public void input(int x, int y) { a = x; b = y; } public void output() { Console.WriteLine(a + " " + b); } public static A operator +(A a1, A a2)//Binary Operator OverLoad(+) { A a3 = new A(); a3.a = a1.a + a2.a; a3.b = a1.b + a2.b; return a3; }}class B{ static void Main() { A a1 = new A(); A a2 = new A(); A a3 = new A(); a1.input(38, 46); a1.output(); a2.input(40, 59); a2.output(); a3 = a1 + a2; a3.output(); Console.Read(); }}

40

.NET PRECTICALS

41

.NET PRECTICALS

25. WAP to deal with Exception Handlingusing System;class A{ static void Main() { try { int a, b, c; Console.WriteLine("Enter a and b:"); a = int.Parse(Console.ReadLine()); b = int.Parse(Console.ReadLine()); c = a / b; Console.WriteLine("Output is" + c); } catch (Exception e) { Console.WriteLine("Divide by zero is an error"); } Console.Read(); }}

42

.NET PRECTICALS

26. WAP for displaying Randomly Generated button and Click Eventusing System;using System.Collections.Generic;using System.ComponentModel;using System.Data;using System.Drawing;using System.Linq;using System.Text;using System.Windows.Forms;

namespace Randomly_Generated_Button{ public partial class RandomGenBt : Form { private Button b1; private int counter1,counter2; public RandomGenBt() { counter1 = 25; counter2 = 25; InitializeComponent(); }

private void cmdGen_Click(object sender, EventArgs e) { b1 = new Button(); b1.Location = new Point(counter1, counter2); b1.Size = new Size(50, 25); b1.Text = counter1.ToString() + " " + counter2.ToString(); b1.Click+=new EventHandler(x); this.Controls.Add(b1); counter1 = counter1 + 52; counter2 = counter2 + 27; } private void x(object sender,EventArgs e) { MessageBox.Show(b1.Text.ToString()); } }}

43

.NET PRECTICALS

44

.NET PRECTICALS

27. WAP to Demonstrate Multi Threading Exampleusing System;using System.Collections.Generic;using System.Threading;

class Program { static void Main(string[] args) { // creating more than one thread in the application Thread FirstThread = new Thread(FirstFunction); Thread SecondThread = new Thread(SecondFunction); Thread ThirdThread = new Thread(ThirdFunction); Console.WriteLine("\nThreading Starts...\n");

//Starting more than one thread in the application FirstThread.Start(); SecondThread.Start(); ThirdThread.Start(); }

public static void FirstFunction() { for (int number = 1; number <= 5; number++) { Console.WriteLine("First Thread Displays: " + number); Thread.Sleep(200); } }

public static void SecondFunction() { for (int number = 1; number <= 5; number++) { Console.WriteLine("Second Thread Displays: " + number); Thread.Sleep(500); } }

public static void ThirdFunction() { for (int number = 1; number <= 5; number++) { Console.WriteLine("Third Thread Displays: " + number); Thread.Sleep(700); } Console.Write("\nPress ENTER to quit..."); Console.ReadLine(); }

}

45

.NET PRECTICALS

46

.NET PRECTICALS

28. WAP to implement delegatesusing System;delegate void MCA();class A{ public void f1() { Console.WriteLine("f1 Method"); } public void f2() { Console.WriteLine("f2 Method"); }}class B{ static void Main() { A a1 = new A(); MCA x; x = a1.f1; x += a1.f2; x(); Console.Read(); }}

47

.NET PRECTICALS

29. WAP to implement multicast delegatesusing System;

public delegate void MCA(); class Deltest { public void a() { Console.WriteLine("Single Delegate"); } public void b() { Console.WriteLine("Multicast Delegate"); } } class Program { static void Main(string[] args) { Deltest dt = new Deltest(); MCA d = new MCA(dt.a); d += new MCA(dt.b); d(); Console.ReadKey(); } }

48

.NET PRECTICALS

30. WAP for displaying Data Handling Applicationusing System;using System.Collections.Generic;using System.ComponentModel;using System.Data;using System.Drawing;using System.Linq;using System.Text;using System.Windows.Forms;using System.Data.Odbc;

public partial class DataHandling : Form{ OdbcConnection con; OdbcDataAdapter da; DataSet ds; OdbcDataReader dr; OdbcCommand cmd; private int ID; public DataHandling() { con = new OdbcConnection("DSN=Icon;"); con.Open(); da = new OdbcDataAdapter("Select * From tblItem", con); ds = new DataSet(); da.Fill(ds); InitializeComponent(); }

private void cmdShow_Click(object sender, EventArgs e) { dgItems.DataSource = ds.Tables[0];

}

private void dgItems_Click(object sender, EventArgs e) { txtName.Text = dgItems.CurrentRow.Cells[1].Value.ToString(); txtID.Text = dgItems.CurrentRow.Cells[2].Value.ToString(); ID = (int)dgItems.CurrentRow.Cells[0].Value; }

private void cmdUpdate_Click(object sender, EventArgs e) { cmd = new OdbcCommand("Update tblItem Set Item_Name='" + txtName.Text.ToString() + "',Item_Code='" + txtID.Text.ToString() + "' Where Item_ID=" + ID, con); cmd.ExecuteNonQuery();

}}

49

.NET PRECTICALS

50

.NET PRECTICALS

31.WAP to use Window Form Applicationusing System;using System.Collections.Generic;using System.ComponentModel;using System.Data;using System.Drawing;using System.Linq;using System.Text;using System.Windows.Forms;

namespace windows_Form_application{ public partial class MDIContainer : Form { private int counter; public MDIContainer() { counter = 0; InitializeComponent(); }

private void cmdNewForm_Click(object sender, EventArgs e) { NewForm nf = new NewForm(); nf.MdiParent = this; nf.Text = "Form Number .: " + counter.ToString(); counter = counter + 1; nf.Show();

}

private void cmdChangeColor_Click(object sender, EventArgs e) { CDialog.ShowDialog(); this.ActiveMdiChild.BackColor = CDialog.Color; } }}

51

.NET PRECTICALS

52