AWP Practical 2-1
AWP Practical 2-1
NETWITHC#
IT-7152
2018-2019
Practical No: [2]: Working with Object Oriented C# and ASP .NET
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()
{
Page 1
T.Y.B.SC. (I.T.) - ASP.NETWITHC#
IT-7152
2018-2019
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));
Page 2
T.Y.B.SC. (I.T.) - ASP.NETWITHC#
IT-7152
2018-2019
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));
Console.WriteLine("root 2 is:" +r2);
}
}
}
}
Console.ReadLine();
}
}
}
Output:-
Page 3
T.Y.B.SC. (I.T.) - ASP.NETWITHC#
IT-7152
2018-2019
Page 4
T.Y.B.SC. (I.T.) - ASP.NETWITHC#
IT-7152
2018-2019
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();
Page 5
T.Y.B.SC. (I.T.) - ASP.NETWITHC#
IT-7152
2018-2019
}
}
}
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);
Page 6
T.Y.B.SC. (I.T.) - ASP.NETWITHC#
IT-7152
2018-2019
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();
Page 7
T.Y.B.SC. (I.T.) - ASP.NETWITHC#
IT-7152
2018-2019
b b1 = new b();
Console.WriteLine("Sum of two number is "+(a1.input1()+b1.input2()));
}
}
class multilevel
{
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);
Page 8
T.Y.B.SC. (I.T.) - ASP.NETWITHC#
IT-7152
2018-2019
}
public int sub(int a, int b)
{
return (a - b);
}
}
class Program
{
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
Page 9
T.Y.B.SC. (I.T.) - ASP.NETWITHC#
IT-7152
2018-2019
{
public int add(int a, int b)
{
return (a + b);
}
}
class sum2 : h1
{
public int add(int a, int b)
{
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
{
Page 10
T.Y.B.SC. (I.T.) - ASP.NETWITHC#
IT-7152
2018-2019
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)
{
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;
Page 11
T.Y.B.SC. (I.T.) - ASP.NETWITHC#
IT-7152
2018-2019
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 Program
{
static void Main(string[] args)
{
sum1 s1 = new sum1();
Console.WriteLine("Sum of two number is "+s1.add(10, 20));
Console.ReadLine();
}
}
}
Output:-
Page 12
T.Y.B.SC. (I.T.) - ASP.NETWITHC#
IT-7152
2018-2019
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);
}
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.) - ASP.NETWITHC#
IT-7152
2018-2019
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();
}
}
}
Page 14
T.Y.B.SC. (I.T.) - ASP.NETWITHC#
IT-7152
2018-2019
Output:-
Page 15