Anda di halaman 1dari 7

//Write a C# program to raise an event if the "marks" of a studen //is less than zero or greater than 100 while

creating an instance of // a student class comprising of fields Usn,studName,marks. using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace ConsoleApplication2 { class Program { public string usn; public string studName; public int marks; public delegate void Marks(string msg); public static event Marks LessThanZero; public static event Marks GreaterThanHundred; public Program() { } public Program(string usn, string studName, int marks) { this.usn = usn; this.studName = studName; this.marks = marks; } public void checkMarks() { if (marks < 0) LessThanZero("invalid marks less than zero"); else if (marks > 100) GreaterThanHundred(" invalid marks greater than hundred"); } } class Program1 { static void Main(string[] args) { Program p1 = new Program("4sf06cs067", "sandhya", 101); Program p2 = new Program("4sf06cs068", "sapna", -1); Program.LessThanZero += new Program.Marks(OnLessThanZero); Program.GreaterThanHundred += new Program.Marks(OnGreaterThanHundred); p1.checkMarks(); p2.checkMarks();

Program.LessThanZero -= new Program.Marks(OnLessThanZero); Program.GreaterThanHundred -= new Program.Marks(OnGreaterThanHundred); Console.ReadLine(); } public static void OnLessThanZero(string msg) { Console.WriteLine("Message:{0}", msg); } public static void OnGreaterThanHundred(string msg) { Console.WriteLine("Message:{0}", msg); } } } //write a complete C# program to add and subtract two complex nos by writing //Add and Subtract methods which could be called through the delegate method of programming. using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace ConsoleApplication3 { class Complex { public int x; public int y; public Complex() { } public Complex( int x,int y) { this.x=x; this.y=y; } public static Complex AddComplex(Complex a1,Complex a2) { Complex c1=new Complex(a1.x+a2.x,a1.y+a2.y); return c1; } public static Complex SubtractComplex(Complex a1,Complex a2) { int newx=a1.x-a2.x;

if (newx < 0) Console.WriteLine("error"); int newy=a1.y-a2.y; if(newy<0) Console.WriteLine("error"); return new Complex(newx,newy); } public override string ToString() { return x + "+i" + y; } public delegate Complex noComplex(Complex a, Complex b); public static void Main() { noComplex n = new noComplex(AddComplex); Complex p1 = new Complex(10, 20); Complex p2 = new Complex(5, 10); Complex p3 = n(p1, p2); Console.WriteLine(n.Method); Console.WriteLine(p3.ToString()); noComplex n1 = new noComplex(SubtractComplex); Complex p4 = new Complex(10, 20); Complex p5 = new Complex(5, 10); Complex p6 = n1(p1, p2); Console.WriteLine(n1.Method); Console.WriteLine(p6.ToString()); Console.ReadLine(); } } }

// Its is required to maintain a dictionary of names and telephone nos of persons. //we may need to retrieve the telphone no of a person given his name. // Design C# program to do this using indexer. using System; using System.Collections.Generic; using System.Linq; using System.Text;

using System.Collections.Specialized; namespace ConsoleApplication4 { class Program { private Dictionary<string,int> teleDictionary; public Program() { Dictionary<string, int> teleDictionary = new Dictionary<string, int>(); } public Program2 this[string name] { get { return (Program2)teleDictionary[name]; } set { teleDictionary.Add(name, value); } } } public class Program2 { public string name; public int value; public Program2(string name, int value) { this.name = name; this.value = value; } } public class Program1 { public static void Main() { Program telephone = new Program(); telephone["sandhya"] = new Program2("sandhya",1234); Console.WriteLine("getting sandhya using indexer"); Program2 san = telephone["sandhya"]; Console.WriteLine("{0}'s telephone no is {1}",san.name,san.value); Console.Read();

} } }

//Write the following classes with appropriate members:customer that holds all bank customer details //an interface Loan to calculate premium and other details // Two subclasses account and LoanAccount to create customers with and without loan respectively //Show implementation for this problem.

using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace ConsoleApplication5 { class Customer { public string cname; public string address; public int phno; } public interface Loan { void calPremium(); void totalLoan(); } class Account:Customer { public string cname; public string address; public int phno; public int accno; public int accbal; public Account() { } public Account(string cname, string address, int phno, int accno, int accbal) {

this.cname = cname; this.address = address; this.phno = phno; this.accno = accno; this.accbal = accbal; } } class LoanAccount:Customer,Loan { public string cname; public string address; public int phno; public int accno; public int accbal; public int loanAm; public int loanNo; public LoanAccount() { } public LoanAccount(string cname, string address, int phno, int accno, int accbal,int loanAm,int loanNo) { this.cname = cname; this.address = address; this.phno = phno; this.accno = accno; this.accbal = accbal; this.loanAm=loanAm; this.loanNo=loanNo; } public void calPremium() { Console.WriteLine("calculating premiun"); //calculation of premium Console.WriteLine("premium is "); } public void totalLoan() { Console.WriteLine("calculating total loan"); //calculation of total loan Console.WriteLine("the total loan is"); }

} class Bank { static void Main(string[] args) { Account a = new Account("sandhya", "bejai", 34736446,123, 40000); LoanAccount la = new LoanAccount("san", "lalbagh", 4756476, 30000, 234, 50000, 1234); Loan l = (Loan)la; l.calPremium(); l.totalLoan(); Console.Read(); } } }