Você está na página 1de 14

T.Y.B.Sc. (I.T.

) – Advanced Web Programming –Paper (III) [Your Roll No]

Practical No: [2]: Working with Object Oriented C# and ASP .NET

a) Create simple application to perform following operations

i. Finding factorial Value ii. Money Conversion

iii. Quadratic Equation iv. Temperature Conversion

Source Code: - CS Code

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleAppDiv2
{
class p2
{
static void Main(string[] args)
{
p2 p = new p2();
int a;
Console.WriteLine("1.factorial 2.currency conversion 3.quadratic equation
4.temperature conversion");
Console.WriteLine("Enter your choice");
a = Convert.ToInt32(Console.ReadLine());
switch (a)
{
case 1:
{
p.fact();
break;
}
case 2:
{
p.curry();
break;
}
case 3:
{
p.quad();
break;
}
case 4:
{
p.temp();
break;
}
default:
{
Console.WriteLine("Try again");
break;
}
}
}
public void fact()
{
int i, num, fact = 1;
Console.WriteLine("Enter the no.");

Page 1
T.Y.B.Sc. (I.T.) – Advanced Web Programming –Paper (III) [Your Roll No]

num = Int32.Parse(Console.ReadLine());
for (i = 1; i <= num; i++)
{
fact = fact * i;
}
Console.WriteLine("factorial of" + fact);
Console.Read();
}
public void curry()
{
double rs, usd;
Console.WriteLine("enter amount of usd");
usd = Int32.Parse(Console.ReadLine());
rs = 68.52 * usd;
Console.WriteLine(usd + "$=Rs." + rs);
Console.Read();
}
public void temp()
{
int cel, far;
Console.WriteLine("enter temperature in cel");
cel = Int32.Parse(Console.ReadLine());
far = (cel * 9) / 5 + 32;
Console.WriteLine("temperature in farenheit is:" + far);
Console.Read();
}
public void quad()
{
int a,b,c,ans,r1,r2;
Console.WriteLine("enter value of a");
a=Int32.Parse(Console.ReadLine());
Console.WriteLine("enter value of b");
b=Int32.Parse(Console.ReadLine());
Console.WriteLine("enter value of c");
c=Int32.Parse(Console.ReadLine());
ans = (b * b) - 4 * a * c ^1/2;
Console.WriteLine(ans);

if(ans==0)
{
Console.WriteLine("roots are imaginary");
}
else
{
if (ans < 0)
{
Console.WriteLine("roots are complex root");
r1= -b + (((b * b) - 4 * a * c)^(1 / 2) / (2 * a));
Console.WriteLine("root 1 is:" +r1);
r2= -b - (((b * b) - 4 * a * c)^(1 / 2) / (2 * a));
Console.WriteLine("root 2 is:" +r2);
}
else
{
Console.WriteLine("roots are real");
r1= -b + (((b * b) - 4 * a * c)^(1 / 2) / (2 * a));
Console.WriteLine("root 1 is" +r1);
r2= -b - (((b * b) - 4 * a * c)^(1 / 2) / (2 * a));
ans=((b * b) - 4 * a * c)^1/2;
Console.WriteLine(ans);
if (ans == 0)
{

Page 2
T.Y.B.Sc. (I.T.) – Advanced Web Programming –Paper (III) [Your Roll No]

Console.WriteLine("roots are imaginary");


}
else
{
if (ans < 0)
{
Console.WriteLine("roots are complex root");
r1= -b + (((b * b) - 4 * a * c)^(1 / 2) / (2 * a));
Console.WriteLine("root 1 is:" +r1);
r2= -b - (((b * b) - 4 * a * c)^(1 / 2) / (2 * a));
Console.WriteLine("root 2 is:" +r2);
}
else
{
Console.WriteLine("roots are real");
r1= -b + (((b * b) - 4 * a * c)^(1 / 2) / (2 * a));
Console.WriteLine("root 1 is" +r1);
r2= -b - (((b * b) - 4 * a * c)^(1 / 2) / (2 * a));
Console.WriteLine("root 2 is:" +r2);
}
}
}
}
Console.ReadLine();
}
}
}

Output:-

Page 3
T.Y.B.Sc. (I.T.) – Advanced Web Programming –Paper (III) [Your Roll No]

Page 4
T.Y.B.Sc. (I.T.) – Advanced Web Programming –Paper (III) [Your Roll No]

b) Create simple application to demonstrate use of following concepts

i. Function Overloading ii. Inheritance (all types)


iii. Constructor overloading iv. Interfaces

Source Code: - CS Code

1.Function Overloading

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApp1
{
class Program
{
public int Add(int a, int b)
{
int sum = a + b;
return sum;
}
public int Add(int a, int b, int c)
{
int sum = a + b + c;
return sum;
}
public static void Main(String[] args)
{
Program ob = new Program();

int sum1 = ob.Add(1, 2);


Console.WriteLine("sum of the two " + "integer value : " + sum1);

int sum2 = ob.Add(1, 2, 3);


Console.WriteLine("sum of the three " + "integer value : " + sum2);
}
}

Page 5
T.Y.B.Sc. (I.T.) – Advanced Web Programming –Paper (III) [Your Roll No]

Output:-

2. Inheritance

i) Single:-

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApp1
{
class Program
{
interface s1
{
int area(int a, int b);
}
class rect : s1
{
public int area(int a, int b)
{
return (a * b);
}
}
public static void Main(String[] args)
{
rect r = new rect();
int result = r.area(5, 10);
Console.WriteLine("Area of rectangle is "+ result);
Console.ReadLine();
}
}

Page 6
T.Y.B.Sc. (I.T.) – Advanced Web Programming –Paper (III) [Your Roll No]

ii) Multilevel:-

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleAppDiv2
{
class a
{
public int input1()
{
Console.WriteLine("Enter a Number 1");
int n1 = Convert.ToInt32(Console.ReadLine());
return n1;
}
}
class b:a
{
public int input2()
{
Console.WriteLine("Enter a Number 2");
int n2 = Convert.ToInt32(Console.ReadLine());
return n2;
}
}
class c:b
{
public void add()
{
a a1 = new a();
b b1 = new b();
Console.WriteLine("Sum of two number is "+(a1.input1()+b1.input2()));
}
}
class multilevel
{

Page 7
T.Y.B.Sc. (I.T.) – Advanced Web Programming –Paper (III) [Your Roll No]

static void Main(string[] args)


{
c j = new c();
j.add();
Console.ReadLine();
}
}
}

Output:-

iii) Multiple

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApp1
{
interface c1
{
int add(int a, int b);
}
interface c2
{
int sub(int a, int b);
}
class compute : c1, c2
{
public int add(int a, int b)
{
return (a + b);
}
public int sub(int a, int b)
{
return (a - b);
}
}
class Program
{

Page 8
T.Y.B.Sc. (I.T.) – Advanced Web Programming –Paper (III) [Your Roll No]

static void Main(string[] args)


{
compute c = new compute();
int sum = c.add(10, 20);
int min = c.sub(40, 30);
Console.WriteLine(sum);
Console.WriteLine(min);
Console.ReadLine();
}
}
}

Output:-

iv) Hierarchical

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApp1
{
interface h1
{
int add(int a, int b);
}
class sum1 : h1
{
public int add(int a, int b)
{
return (a + b);
}
}
class sum2 : h1
{
public int add(int a, int b)
{

Page 9
T.Y.B.Sc. (I.T.) – Advanced Web Programming –Paper (III) [Your Roll No]

return (a + b);
}
}
class Program
{
static void Main(string[] args)
{
sum1 s1 = new sum1();
sum2 s2 = new sum2();
Console.WriteLine(s1.add(10, 20));
Console.WriteLine(s2.add(20, 30));
Console.ReadLine();
}
}
}

Output:-

3. Constructor Overloading

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApp1
{
class Age
{
string user;
int age;
public Age()
{
user = "Harshal";
age = 18;
Console.WriteLine("Previous User {0} and he was {1} year old", user, age);
}
public Age(string name, int age1)
{

Page 10
T.Y.B.Sc. (I.T.) – Advanced Web Programming –Paper (III) [Your Roll No]

user = name;
age = age1;
Console.WriteLine("Current User {0} and he is {1} year old", user, age);
}
}
class Program
{
static void Main(string[] args)
{
Age gs = new Age();
Age gs1 = new Age("Akash", 19);
Console.ReadLine();
}
}
}

Output:-

4. Interfaces

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApp1
{
interface h1
{
int add(int a, int b);
}
class sum1 : h1
{
public int add(int a, int b)
{
return (a + b);

Page 11
T.Y.B.Sc. (I.T.) – Advanced Web Programming –Paper (III) [Your Roll No]

}
}
class Program
{
static void Main(string[] args)
{
sum1 s1 = new sum1();
Console.WriteLine("Sum of two number is "+s1.add(10, 20));
Console.ReadLine();
}
}
}

Output:-

c) Create simple application to demonstrate use of following concepts

i. Using Delegates and events ii. Exception handling

Source Code: - CS Code

1) Using Delegates and events

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApp1
{
delegate int Arithop(int a, int b);
class Mathsop
{
public static int Add(int a, int b)
{
return (a + b);

Page 12
T.Y.B.Sc. (I.T.) – Advanced Web Programming –Paper (III) [Your Roll No]

}
public static int Sub(int a, int b)
{
return (a - b);
}
public static int Mul(int a, int b)
{
return (a * b);
}
public static int Div(int a, int b)
{
return (a / b);
}
}
class Program
{
public static void Main()
{
Arithop op1 = new Arithop(Mathsop.Add);
Arithop op2 = new Arithop(Mathsop.Sub);
Arithop op3 = new Arithop(Mathsop.Mul);
Arithop op4 = new Arithop(Mathsop.Div);
int r1 = op1(10, 20);
int r2 = op2(50, 30);
int r3 = op3(5, 4);
int r4 = op4(30, 3);
Console.WriteLine(r1);
Console.WriteLine(r2);
Console.WriteLine(r3);
Console.WriteLine(r4);
Console.ReadLine();
}
}
}

Output:-

Page 13
T.Y.B.Sc. (I.T.) – Advanced Web Programming –Paper (III) [Your Roll No]

2) Exception Handling

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApp1
{
class Program
{
int result;
public void division(int num1, int num2)
{
try
{
result = num1 / num2;
}
catch (DivideByZeroException e)
{
Console.WriteLine("Exception: {0}", e);
}
}
static void Main(string[] args)
{
Program eh = new Program();
eh.division(25, 0);
Console.ReadLine();
}
}
}

Output:-

Page 14

Você também pode gostar