Вы находитесь на странице: 1из 38

Q<1>: Write a Class to simulate a Circle Shape.

Each circle has its radius and center point Provide 2 constructors: a parameter less and one which takes center point and radius as parameter. Provide mechanism to find the diameter (twice of radius) and area (PI*square of radius). Provide a mechanism to move a circle from its stating location to somewhere else. Apply proper access modifiers and use proper fields, method and properties.
using using using using System; System.Collections.Generic; System.Linq; System.Text;

namespace shape1 { public class circle { int x1; int y1; double r1; public circle() { } public circle(int x, int y, double r) { x1 = x; y1 = y; r1 = r; } public void calculate() { double d = 2 * r1; double area = 3.14 * r1 * r1; Console.WriteLine("Diameterof circle=" + d); Console.WriteLine("Area of circle=" + area); } } class abc { static void Main(string[] args) { circle s = new circle(0, 0, 2); s.calculate(); Console.ReadLine(); } } }

OUTPUT:

amre

Page 1

Q<2>: Create an abstract class having properties to access the length and breadth of a 2D shape either rectangle or Square and an abstract method to calculate their area. Override these methods and derived class, also implement a bool method to check whether 2D shape is a rectangle of a square. using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace shap1 { public abstract class shape { protected int hight , breadth ; public abstract int x { set; } public abstract int y { set; } public abstract void calculate(); } public class rectSquare : shape { public override int x { set { hight = value; } } public override int y { set { breadth = value; } } public override void calculate() { int a = hight * breadth; if (hight == breadth) Console.Write(" Area of Square=" + a); else Console.Write(" Area of Rectangle=" + a); } } amre Page 2

class xyz { static void Main(string[] args) { rectSquare s=new rectSquare(); s.x=10; s.y=9; s.calculate(); Console.Read(); } } } OUTPUT:

amre

Page 3

Q<3>: Create an interface Products containing properties to set and display price and name of the product. Give the implementation in a class and display the values. using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace Interface { interface product { double p { set; get; } string n { set; get; } } class my : product { double price; string name; public double p { set { price = value; } get { return price; } } public string n { set { name = value; } get { return name; } } } class Program { amre Page 4

static void Main(string[] args) { my o = new my(); o.n = "shirt"; Console.WriteLine("Name of product :"+o.n); o.p = 2000; Console.WriteLine("Price of the product :"+o.p); Console.ReadLine(); } } } OUTPUT:

amre

Page 5

Q<4>: Using params,show how you can acces elements of various types.

using using using using

System; System.Collections.Generic; System.Linq; System.Text;

namespace rajparams { class param1 { public void Sum(params double[] a) { double s = 0; for (int i = 0; i < a.Length; i++) { s = s + a[i]; } Console.WriteLine("sum of Array elements=" + s); } } class program { public static void Main() { param1 o = new param1(); o.Sum(14,4,45, 6); o.Sum(17.7, 18.37, 46.7); o.Sum(16, 13, 9,1 ,7, 8); Console.ReadLine(); } } }

OUTPUT:

amre

Page 6

Q<5>: Create and access elements of 1D and 2D array using indesers. using System; namespace Indexerproperty { class arrayList { int [] data = new int[10]; public int this[int i] { set { data[i] = value; } get { return (data[i]); } } } class progam { static void Main(string[] args) { arrayList o = new arrayList(); o[0] = 45; o[1] = 56; o[2] = 78; o[3] = 67; for (int i = 0; i < 4; i++) { Console.WriteLine(o[i]); } Console.ReadLine(); }

}}
OUTPUT:

amre

Page 7

Q<6>: WAP to create a delegate to find whether a number is an even of odd.

using using using using

System; System.Collections.Generic; System.Linq; System.Text;

namespace ConsoleApplication4 { delegate void evenOdd(int n); class DelegateEven { public static void processNo(int n) { if (n % 2 == 0) Console.Write("Number is Even"); else Console.Write("Number is Odd"); } static void Main(string[] args) { evenOdd a = new evenOdd(processNo); a(111); Console.Read(); } } }

OUTPUT:

amre

Page 8

Q<7>: Create a delegate to perform two mathematical operations to find square & square-root of a number.
using using using using System; System.Collections.Generic; System.Linq; System.Text;

namespace square { public delegate void squarRoot(int a, int b); class root { public static void display(int a, int b) { Console.WriteLine("Square of 12=" + a * a); double y = Math.Sqrt(b); Console.WriteLine("Square Root of a 195=" + } static void Main(string[] args) { squarRoot n = new squarRoot(display); n(12,195); Console.ReadLine(); } } }

y );

Output-

amre

Page 9

Q<8>: Write to reverse a string using delegate.

using using using using

System; System.Collections.Generic; System.Linq; System.Text;

namespace reverse { public delegate string reverseString(string n); class Program { string s1; public static string display(string a) { string temp = ""; int i; Console.WriteLine("Reverse string."); for( i = a.Length - 1; i >=0; i--) temp += a[i]; return temp; } static void Main(string[] args) { string str; reverseString a = new reverseString(display); str=a("amrendra kumar"); Console.WriteLine (str); Console.ReadLine(); } } }

OUTPUT:

amre

Page 10

Q<9>: Create a delegate to perform the following operations on two numbers Addition Substraction Find maximum of two numbers Implement using multicast delegate.
using using using using System; System.Collections.Generic; System.Linq; System.Text; operation(int a , int b);

namespace addition { public delegate void class AddSub { {

public static void add(int a, int b) Console.WriteLine("Addition = " + (a + b)); } public static void sub(int a, int b) { Console.WriteLine("Subtraction=" + (a-b)); } public static void maximum(int a, int b) { if (a>b) Console.WriteLine("Maximum no=" + a); else Console.WriteLine("Maximum no=" + b); } static void Main(string[] args) { operation n2; operation n = new operation(add); operation n1 = new operation(sub); operation n3 = new operation(maximum); n2 = n; n2 += n1; n2 += n3; n2(20,5); Console.ReadLine(); } } }

OUTPUT:

amre

Page 11

Q<10>: WAP to create an event, which is fired when you pass an object to a method which is responsible for firing the event ,Event handler checks whether argument object passed is an integer of double, typecast it and display it.
using using using using System; System.Collections.Generic; System.Linq; System.Text;

namespace first { public class Test { private int value; public delegate void Handler(); public event Handler ChangeNum; protected virtual void OnNumChanged() { if (ChangeNum != null) { ChangeNum(); } else { Console.WriteLine("Event is fired!"); } } public Test(int n) { SetValue(n); } public void SetValue(int n) { if (value != n) { value = n; OnNumChanged(); } } } class program { static void Main(string[] args) { Test e = new Test(2); e.SetValue(7); e.SetValue(11); Console.ReadKey(); } } }

amre

Page 12

OUTPUT:

amre

Page 13

Q<11>: WAP that welcomes person whose name is passed to it as parameter. Write your own exception handling code to catch an exception thrown when no name is passed.
using using using using System; System.Collections.Generic; System.Linq; System.Text;

namespace exception { class myException : Exception { public myException(string msg) : base(msg) { } } class program { static void Main(string[] args) { Console.WriteLine("Enter the string:"); string name = Console.ReadLine(); program o = new program(); o.function(name); Console.ReadKey(); } public void function(string name) { try { if (name == "") throw new myException("Null string"); else Console.WriteLine("Welcome:-" + name); } catch (myException o) { Console.WriteLine(o.ToString()); } } }

OUTPUT:

amre

Page 14

Q<12>: Create & initialize an integer array of size 10.Write exception handling code to handle case when an attempt is made to access a non existent arry element.
using using using using System; System.Collections.Generic; System.Linq; System.Text;

namespace exceptionEx14 { class RajException : Exception { public RajException(string msg) : base(msg) { } } class rajMain { static void Main(string[] args) { int flag = 0; Console.WriteLine("Enter the element for checking:"); int n = Int32.Parse(Console.ReadLine()); int[] a = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 }; try { for (int i = 0; i < a.Length; i++) { if (a[i] == n) { Console.WriteLine("Element Exist"); flag = 1; } } if (flag == 0) throw new RajException("element doesn't exist"); } catch (RajException obj) { Console.WriteLine(obj.ToString()); } Console.ReadLine(); } } }

OUTPUT:

amre

Page 15

Q<13>: WAP to compare two strings, create the exceptin NotEqual if both strings differ. Handle the exception if this case occurs.

using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace exception15 { class rajExceptin : Exception { public rajExceptin(string msg) : base(msg) { } } class rajMain { static void Main(string[] args) { string str1 = "amrendra"; string str2 = "amrendra"; try { if (str1.Equals(str2)) { Console.WriteLine("Both strings are equal"); } else { throw new rajExceptin("strings are not same"); } } catch (rajExceptin obj) { Console.WriteLine(obj.ToString()); } Console.ReadKey(); } } }
OUTPUT:

amre

Page 16

Q<14>: Write a program that processes the following string: String sentence=Learning C# is extremely easy,fun & interesting, C# has a data type byte which occupies 8 bits and Can store integers from -128 to 127.; Iterate through the string and find the number of letters, digits and punctuation characters in it and print these on the Console.
using using using using System; System.Collections.Generic; System.Linq; System.Text;

namespace charfind { class Program { static void Main(string[] args) { int count1 = 0,count2=0; string str = "Learning C# is extremely easy,fun & interesting,c# has a data type byte which occupies 8 bits and can store integers from -128 to 127."; int len = str.Length; Console.WriteLine("length of string is:"+len); foreach (char c in str) { if (char.IsLetter(c)) { count1++; } if(char.IsDigit(c)) { count2++; } } int punc = len - (count1 + count2); Console.WriteLine("no. of character occurence is:" + count1); Console.WriteLine("no. of digit occurence is:" + count2); Console.WriteLine("no. of punctuation symbol is is:" + punc); Console.ReadLine(); } } }

OUTPUT:

amre

Page 17

Q<15>: WAP to show the functionality of multithreading. using System; using System.Threading; namespace thread { class myThread { public void get() { for (int i = 1; i <= 5; i++) { Console.WriteLine("i=" + i); } } } class myThread2 { public void put() { for (int j = 5; j <= 10; j++) { Console.WriteLine("j=" + j); Thread.Sleep(1000); } } }

class program { static void Main(string[] args) { myThread obj = new myThread(); myThread2 obj1 = new myThread2(); Thread t1 = new Thread(obj.get); Thread t2 = new Thread(obj1.put); t1.Start(); Thread.Sleep(1000); t2.Start(); Console.ReadLine(); } } } amre Page 18

OUTPUT:

amre

Page 19

Q<16>: Create an attribute MyHelpAttribute having one positional parameter (string Author) define by formal parameter for public non-static read-write fields & properties of attribute class. Retrieve attribute information for the program element at run time using reflection support & check to see if class has MyHelp attribute & write out associate Authour & Topic values if attribute is present. using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace attributeEx { [AttributeUsage(AttributeTargets.Class | AttributeTargets.Struct)] class attribute : System.Attribute { public string name; public attribute(string s) { name = s; } public string getName() { return (name); } } [attribute("C#")] class Book {} class program { static void Main(string[] args) { Type t = typeof(Book); Attribute[] att = Attribute.GetCustomAttributes(t); foreach (Attribute at in att) { if (at is attribute) { attribute obj = (attribute)at; Console.WriteLine("BOOK Name:" + obj.getName()); Console.ReadLine(); } } } } } amre Page 20

OUTPUT:

amre

Page 21

Q<17>: Write a Windows application in which create a form Dateform & display the current year,month,day of month,day of week & current time in a list box.

using using using using using using using using

System; System.Collections.Generic; System.ComponentModel; System.Data; System.Drawing; System.Linq; System.Text; System.Windows.Forms;

namespace datetimewin { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void Form1_Load(object sender, EventArgs e) { label1.Text = System.DateTime.Now.ToLongDateString(); label2.Text = System.DateTime.Now.ToShortTimeString(); }

} }

OUTPUT:

amre

Page 22

Q<18>: Write a windows application to create a Mini Calculator.

using using using using using using using using

System; System.Collections.Generic; System.ComponentModel; System.Data; System.Drawing; System.Linq; System.Text; System.Windows.Forms;

namespace Mycalculator { public partial class Form1 : Form { decimal FirstValue; string Operation;

public Form1() { InitializeComponent(); txtNumber.Focus(); } private void btnadd_Click(object sender, EventArgs e) { FirstValue = Convert.ToDecimal(txtNumber.Text); Operation = "ADD"; txtNumber.Clear(); txtNumber.Focus(); } private void btnequal_Click(object sender, EventArgs e) { if (Operation == "ADD") { txtNumber.Text = ((FirstValue) + (Convert.ToDecimal(txtNumber.Text))).ToString(); } else if (Operation == "SUB") { txtNumber.Text = (FirstValue Convert.ToDecimal(txtNumber.Text)).ToString(); } else if (Operation == "DIVIDE") { txtNumber.Text = (FirstValue / Convert.ToDecimal(txtNumber.Text)).ToString(); }

amre

Page 23

else if (Operation == "MUL") { txtNumber.Text = (FirstValue * Convert.ToDecimal(txtNumber.Text)).ToString(); } } private void btnsub_Click(object sender, EventArgs e) { FirstValue = Convert.ToDecimal(txtNumber.Text); Operation = "SUB"; txtNumber.Clear(); txtNumber.Focus(); } private void btndiv_Click(object sender, EventArgs e) { FirstValue = Convert.ToDecimal(txtNumber.Text); Operation = "DIVIDE"; txtNumber.Clear(); txtNumber.Focus(); } private void btnprod_Click(object sender, EventArgs e) { FirstValue = Convert.ToDecimal(txtNumber.Text); Operation = "MUL"; txtNumber.Clear(); txtNumber.Focus(); } private void btnclr_Click(object sender, EventArgs e) { txtNumber.Clear(); txtNumber.Focus(); } private void btn7_Click(object sender, EventArgs e) { txtNumber.Text = txtNumber.Text + 7; txtNumber.Focus(); int length = txtNumber.Text.Length; txtNumber.Select(length, length); } private void btn8_Click(object sender, EventArgs e) { txtNumber.Text = txtNumber.Text + 8; txtNumber.Focus(); int length = txtNumber.Text.Length;

amre

Page 24

txtNumber.Select(length, length); } private void btn9_Click(object sender, EventArgs e) { txtNumber.Text = txtNumber.Text + 9; txtNumber.Focus(); int length = txtNumber.Text.Length; txtNumber.Select(length, length); } private void btn4_Click(object sender, EventArgs e) { txtNumber.Text = txtNumber.Text + 4; txtNumber.Focus(); int length = txtNumber.Text.Length; txtNumber.Select(length, length); } private void btn5_Click(object sender, EventArgs e) { txtNumber.Text = txtNumber.Text + 5; txtNumber.Focus(); int length = txtNumber.Text.Length; txtNumber.Select(length, length); } private void btn6_Click(object sender, EventArgs e) { txtNumber.Text = txtNumber.Text + 6; txtNumber.Focus(); int length = txtNumber.Text.Length; txtNumber.Select(length, length); } private void btn3_Click(object sender, EventArgs e) { txtNumber.Text = txtNumber.Text + 3; txtNumber.Focus(); int length = txtNumber.Text.Length; txtNumber.Select(length, length); } private void btn2_Click(object sender, EventArgs e) { txtNumber.Text = txtNumber.Text + 2;

amre

Page 25

txtNumber.Focus(); int length = txtNumber.Text.Length; txtNumber.Select(length, length); } private void btn1_Click(object sender, EventArgs e) { txtNumber.Text = txtNumber.Text + 1; txtNumber.Focus(); int length = txtNumber.Text.Length; txtNumber.Select(length, length); } private void btn0_Click(object sender, EventArgs e) { txtNumber.Text = txtNumber.Text + 0; txtNumber.Focus(); int length = txtNumber.Text.Length; txtNumber.Select(length, length); } private void btnsqrt_Click(object sender, EventArgs e) { } } }

OUTPUT:

amre

Page 26

Q<19>: Write a windows application using forms to compare & concatenate two strings.
using System; using using using using using using using System.Collections.Generic; System.ComponentModel; System.Data; System.Drawing; System.Linq; System.Text; System.Windows.Forms;

namespace cmpcon { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void button1_Click(object sender, EventArgs e) { label3.Visible = true; label3.Text = textBox1.Text + textBox2.Text; } private void Form1_Load(object sender, EventArgs e) { label3.Visible = false; } private void button2_Click(object sender, EventArgs e) { label3.Visible = true; if (textBox1.Text.Equals(textBox2.Text)) { label3.Text = "Both Strings are equals"; } else { label3.Text = "Both strings are not equal"; } } } }

amre

Page 27

OUTPUT:

amre

Page 28

Q<20>: Write a windows form application to count the number of vowels in a textbox. The application should have a TextBox and a Lael. The Label should be updated as the user types the text in the TextBox & should display the number of vowels in the text in the TextBox.

using using using using using using using using

System; System.Collections.Generic; System.ComponentModel; System.Data; System.Drawing; System.Linq; System.Text; System.Windows.Forms;

namespace vowelfind { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void button1_Click(object sender, EventArgs e) { label1.Visible = true; string str; str = textBox1.Text; char ch1 = 'a'; char ch2 = 'e'; char ch3 = 'i'; char ch4 = 'o'; char ch5 = 'u'; int counta = 0; int counte = 0; int counti = 0; int counto = 0; int countu = 0; char ch6 = 'A'; char ch7 = 'E'; char ch8 = 'I'; char ch9 = 'O'; char ch10 = 'U'; int int int int int countA countE countI countO countU = = = = = 0; 0; 0; 0; 0;

int j = counta + counte + counti + counto + countu + countA + countE + countI + countO + countU; foreach (char v in str)

amre

Page 29

{ if (v == ch1) { counta++; j++; } else if (v == ch2) { counte++; j++; } else if (v == ch3) { counti++; j++; } else if (v == ch4) { counto++; j++; } else if (v == ch5) { countu++; j++; } } foreach (char v in str) { if (v == ch6) { countA++; j++; } else if (v == ch7) { countE++; j++; } else if (v == ch8) { countI++; j++; } else if (v == ch9) { countO++; j++; } else if (v == ch10) { countU++; j++; } } label1.Text = j.ToString(); } private void Form1_Load(object sender, EventArgs e)

amre

Page 30

{ label1.Visible = false; } } }

OUTPUT:

amre

Page 31

Q<21>:WAP to show the functionality of Remoting.

Server side code:


using using using using using using using using using System; System.Collections.Generic; System.Linq; System.Text; System.Runtime; System.Runtime.Remoting; System.Runtime.Remoting.Channels; System.Runtime.Remoting.Channels.Http; System.Net;

namespace aServer { public class Program { static void Main(string[] args) { HttpChannel hc = new HttpChannel(8080); ChannelServices.RegisterChannel(hc, false);

RemotingConfiguration.RegisterWellKnownServiceType(typeof(NavRemote),"Nav",WellKnownObjec tMode.Singleton); Console.WriteLine("GIMT server ready........."); Console.ReadLine(); } }

public class NavRemote : MarshalByRefObject { public int getValue(int a,int b) { return a+b; } } }

Client side code:


using System; using System.Collections.Generic;

amre

Page 32

using System.Linq; //using System.Text; using System.Runtime; using System.Runtime.Remoting; using System.Runtime.Remoting.Channels; using System.Runtime.Remoting.Channels.Http; //using System.Net; using NavServer; namespace aClient { class Program { static void Main(string[] args) { HttpChannel hc = new HttpChannel(8086); ChannelServices.RegisterChannel(hc,false);

aRemote remote= (aRemote)Activator.GetObject(typeof(aRemote), "http://127.0.0.1:8080/Nav"); int result = remote.getValue(10,20); Console.WriteLine("Sum="+result);

Console.ReadLine();

} } }

OUTPUT:

amre

Page 33

Q<22>:Implement the socket based programming in C#. Server side code


using using using using using using System; System.Collections.Generic; System.Linq; System.Text; System.Net; System.Net.Sockets;

namespace ChatServer { class Program { static void Main(string[] args) {

TcpListener listener = new TcpListener(IPAddress.Parse("127.0.0.1"),4000); listener.Start(); Console.WriteLine("GMT server ready......"); while(true) { TcpClient c = listener.AcceptTcpClient(); NetworkStream ns = c.GetStream(); Console.WriteLine("Enter your message..."); String msg = Console.ReadLine(); byte[] buff = System.Text.Encoding.ASCII.GetBytes(msg); ns.Write(buff,0,buff.Length);

} } }

amre

Page 34

Client side code:


using using using using using using System; System.Collections.Generic; System.Linq; System.Text; System.Net; System.Net.Sockets;

namespace ChatClient { class Program { static void Main(string[] args) {

TcpClient client = new TcpClient("127.0.0.1", 4000); NetworkStream ns = client.GetStream();

byte [] msg = new byte[100]; ns.Read(msg, 0, 100);

string s = System.Text.Encoding.ASCII.GetString(msg);

Console.WriteLine("message from server is="+s);

} } }

OUTPUT:

amre

Page 35

Q<23>:WAP to input the floating number from user and print the even integer part on the console.
using using using using System; System.Collections.Generic; System.Linq; System.Text;

namespace foreacheven1 { class Program { static void Main(string[] args) { int intpart; float[] arr = new float[2]; Console.WriteLine("Enter the floating point no."); for (int i = 0; i < arr.Length; i++) { arr[i] = Single.Parse(Console.ReadLine()); } Console.WriteLine("Result..........."); foreach (float x in arr) { intpart = (int)x; if (intpart % 2 == 0) { Console.WriteLine("{0} no.", intpart); } } Console.ReadLine(); } } }

OUTPUT:

amre

Page 36

Q<24>:WAP to input the 10 floating number from user and print int part and float part on the console.
using using using using System; System.Collections.Generic; System.Linq; System.Text;

namespace floatint { class Program { static void Main(string[] args) { float[] a = new float[10]; Console.WriteLine("Enter 3 floting point number"); for (int i = 0; i < a.Length; i++) { a[i] = Single.Parse(Console.ReadLine()); } Console.WriteLine("number\t\tinteger\t\tfractional"); for (int j = 0; j <a.Length; j++) { int x = (int)a[j]; float b = a[j] - x; Console.WriteLine("{0}\t\t{1}\t\t{2}", a[j], x, b); } Console.ReadLine(); } } }

OUTPUT:

amre

Page 37

Q<25>:WAP to input the name of month from user and find the total number of day on that month
using using using using System; System.Collections.Generic; System.Linq; System.Text;

namespace month { class Program { static void Main(string[] args) { string month,strup; Console.WriteLine("Enter the name of month"); month = Console.ReadLine(); strup = month.ToLower();

switch (strup) { case "january": case "march": case "may": case "july": case "august": case "october": case "december": Console.WriteLine("{0}the number of days are 31", month); break; case "april": case "june": case "september": case "november": Console.WriteLine("{0}the number of days are 30", month); break; case "february": Console.WriteLine("{0}the number of days are 28 or 29", month); break; default: Console.WriteLine("wrong choice"); break; } Console.ReadLine(); } } }

OUTPUT:

amre

Page 38

Вам также может понравиться