Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 12

Advanced Web Programming

Practical #2
Name Rahul Dilip Rajdeo Roll Number 21302B0018
Subject/Course: Advanced Web Programming
Topic Working with Object Oriented C#

1. Working with Object Oriented C#

a. Create simple application to perform following operations i. Finding factorial Value ii. Money Conversion iii.
Quadratic Equation iv. Temperature Conversion
FACTORIAL MAIN FILE:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApp12
{
class Program
{
static void Main(string[] args)
{
Factorial f = new Factorial();
Console.WriteLine("enter a number");
int n = Convert.ToInt32(Console.ReadLine());
f.getFact(6);
}
}
}
FACTORIAL CLASS:
using System;
namespace ConsoleApp12
{
class Factorial
{
public void getFact(int n)
{
int f = 1;
for (int i = 1; i <= n; i++)
{
f = f * i;
}
Console.WriteLine("factorial=" + f);
Console.Read();
}
}
}

Vidyalankar School of Information Technology


OUTPUT:-

B]Conversion:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApp2
{
class Program
{
static void Main(string[] args)
{
conversion t = new conversion();
Console.WriteLine("enter dollars");
int n = Convert.ToInt32(Console.ReadLine());
t.dollartoRs(n);
Console.WriteLine("enter rupees");
int a = Convert.ToInt32(Console.ReadLine());
t.RsToDollar(a);
Console.ReadLine();
}
}
}
Conversion Class:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApp2
{
class conversion
{
public void dollartoRs(int d)
{
double r = d * 82.32;
Console.WriteLine(d + "dollars:" + "Rs." + r);
}
public void RsToDollar(int r)
{
double d = r / 82.32;
Console.WriteLine("Rs." + r + "=$" + d);
}

Vidyalankar School of Information Technology


}
}
OUTPUT:-

b. Create simple application to demonstrate use of following concepts


i. Function Overloading ii. Inheritance (all types)
iii. Constructor overloading iv. Interfaces
1]Function Overloading:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApp2
{
class Program
{
static void Main(string[] args)
{
FunctionOverloading f = new FunctionOverloading();
Console.WriteLine("enter 3 nos.");
int a = Convert.ToInt32(Console.ReadLine());
int b = Convert.ToInt32(Console.ReadLine());
int c = Convert.ToInt32(Console.ReadLine());
f.Add(a);
f.Add(a, b);
f.Add(a, b, c);
Console.ReadKey();

}
}
}
FunctionOverloadingClass:-
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApp2
{
class FunctionOverloading
{
Vidyalankar School of Information Technology
public void Add(int n)
{
Console.WriteLine("Result a=" + (n + 20));
}
public void Add(int a, int b)
{
Console.WriteLine("Result b=" + (a + b));
}
public void Add(int a, int b, int c)
{
Console.WriteLine("Result c=" + (a + b + c));
}
}
}
OUTPUT:

2]
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApp2
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Enter length and breadth of rectangle:");
int l = Convert.ToInt32(Console.ReadLine());
int b = Convert.ToInt32(Console.ReadLine());
Rectangle r1 = new Rectangle();
Rectangle r2 = new Rectangle(l, b);
Rectangle r3 = new Rectangle(r2);
r1.Display();
r2.Display();
r3.Display();
Console.ReadKey();

}
}
}
RECTANGLE CLASS:-
using System;
using System.Collections.Generic;
Vidyalankar School of Information Technology
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApp2
{
class Rectangle
{
int length, breadth; //instance variables
public Rectangle() //default Construtor
{
length = 10;
breadth = 20;
}
//parametrized Constructor
public Rectangle(int length, int breadth)
{
this.length = length;
this.breadth = breadth;
}
//copy Constructor
public Rectangle(Rectangle r)
{
this.length = r.length;
this.breadth = r.breadth;
}
static Rectangle()
{
Console.WriteLine("static Constructor ");
}
public void Display()
{
Console.WriteLine("area of rectangle" + (length * breadth));
}
}
}
OUTPUT:-

Inheritance

Vidyalankar School of Information Technology


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

namespace Furniture
{
public class Furniture
{
public string material;
public double price;

public Furniture(string material, double price)


{
this.material = material;
this.price = price;
}
}
public class table : Furniture
{
double height, surface_area;

public table(string material, double price, double height, double surface_area) : base(material, price)
{
this.height = height;
this.surface_area = surface_area;
this.material = material;
this.price = price;
}

public void display()


{
Console.WriteLine("Material:" + material);

Vidyalankar School of Information Technology


Console.WriteLine("Price:" + price);
Console.WriteLine("Height:" + height);
Console.WriteLine("Surface Area:" + surface_area);

}
}
class Program
{
static void Main(string[] args)
{
table t = new table("wood", 12000, 12, 100);
t.display();
Console.ReadKey();
}
}
}

Interface:
Code:

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

Vidyalankar School of Information Technology


using System.Threading.Tasks;

namespace ConsoleApp6

interface Gross

int TA { get; set; }

int DA { get; set; }

void gross_sal();

class Employee

public string name;

public Employee(string name)

this.name = name;

public int basic_sal(int b) {

return b;

class salary : Employee, Gross

int hra, ta, da;

Vidyalankar School of Information Technology


public salary(int hra,string name):base(name)

this.hra = hra;

public int TA

get { return ta; }

set { ta = value; }

public int DA

get { return da; }

set { da = value; }

public void gross_sal()

Console.WriteLine("HRA:" + hra);

Console.WriteLine("TA:" + TA);

Console.WriteLine("DA:" + DA);

Console.WriteLine("Gross salary:" + (basic_sal(20000) + hra + TA + DA));

class Program

static void Main(string[] args)

Vidyalankar School of Information Technology


salary s = new salary(100000, “Sahil");

s.TA = 5000;

s.DA = 25000;

s.gross_sal();

Console.ReadLine();

Output:

c. Create simple application to demonstrate use of Delegates and events

Vidyalankar School of Information Technology


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

namespace ConsoleApp6
{
public delegate void delmethod(string name);
class EventEx
{
public event delmethod myevent;
public void Raise_Event(string msg)
{
if(myevent != null)
{
myevent(msg);
}

}
public void getMessage(string name)
{
Console.WriteLine(name);
}
}
class Program
{
static void Main(string[] args)
{
EventEx ex = new EventEx();
delmethod d1 = ex.getMessage;
ex.myevent += d1;
ex.Raise_Event("My Event Raised");
Console.ReadKey();
}
}
}

Output:

Vidyalankar School of Information Technology


Vidyalankar School of Information Technology

You might also like