You are on page 1of 27

1.

Write a C# Program for the Following in Console Application Inheritance ,Interface


and Polymorphism

AIM

TO Write a C# Program for the Following in Console Application Inheritance ,Interface and
Polymorphism
ALGORITHM:

1. Start the program


2. Create a class
3. Create an object in main class
4. Print the output using object for the class
5. Stop.
PROGRAM:

INHERITANCE

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace DemoApplication
{
public class Tutorial
{
protected int TutorialID;
protected string TutorialName;

public void SetTutorial(int pID,string pName)


{
TutorialID=pID;
TutorialName=pName;
}

public String GetTutorial()


{
return TutorialName;
}
}
public class Guru99Tutorial:Tutorial
{
public void RenameTutorial(String pNewName)
{
TutorialName=pNewName;
}

static void Main(string[] args)


{
Guru99Tutorial pTutor=new Guru99Tutorial();

pTutor.RenameTutorial(".Net by Guru99");

Console.WriteLine(pTutor.GetTutorial());

Console.ReadKey();
}
}
}

OUTPUT

POLYMORPHISM

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace DemoApplication
{
class Tutorial
{
public int TutorialID;
public string TutorialName;
public void SetTutorial(int pID,string pName)
{
TutorialID=pID;
TutorialName=pName;
}
public void SetTutorial(string pName)
{
TutorialName=pName;
}
public String GetTutorial()
{
return TutorialName;
}

static void Main(string[] args)


{
Tutorial pTutor=new Tutorial();

pTutor.SetTutorial(1,"First Tutorial");
Console.WriteLine(pTutor.GetTutorial());

pTutor.SetTutorial("Second Tutorial");
Console.WriteLine(pTutor.GetTutorial());

Console.ReadKey();
}
}
}

OUTPUT
INTERFACE

using System;
namespace CsharpInterface {
interface IPolygon {
// method without body
void calculateArea(int l, int b);
}
class Rectangle : IPolygon {
// implementation of methods inside interface
public void calculateArea(int l, int b) {
int area = l * b;
Console.WriteLine("Area of Rectangle: " + area);
}
}
class Program {
static void Main (string [] args) {
Rectangle r1 = new Rectangle();
r1.calculateArea(100, 200);
}
}
}
OUTPUT

Area of Rectangle: 20000


2. Write a C # for String Handling in Console Application
AIM:
To write a program to implement String handling.
ALGORITHM:
1. Start the program
2. Create a class
3. Use ToUpper, substring, format, tostring, gettype, date and time, stringbuilder,
splitingstring.
4. Print the output as required
5. Stop.

a) Use of Replace, insert, ToUpper


public class TestStringsApp
{
public static void Main(string[] args)
{
string a = "strong";

// Replace all 'o' with 'i'


string b = a.Replace('o', 'i');
Console.WriteLine(b);

string c = b.Insert(3, "engthen");


string d = c.ToUpper();
Console.WriteLine(d);
}
}
OUTPUT:
string
STRENGTHENING
b) Use of Substring
string e = "dog" + "bee";
e += "cat";
string f = e.Substring(1,7);
Console.WriteLine(f);
//Like that You can remove use syntax
for (int i = 0; i < f.Length; i++) //f.Remove(2,3);
{
Console.Write("{0,-3}", f[i]);
}

OUTPUT:
ogbeeca
● g b e e c a

c) String Formatting
public class FormatSpecApp
{
public static void Main(string[] args)
{
int x = 16;
decimal y = 3.57m;
string h = String.Format(
"item {0} sells at {1:C}", x, y);
Console.WriteLine(h);
}
}
OUTPUT:
item 12 sells at £3.45
public class FormatSpecApp1
{
public static void Main(string[] args)
{
int i = 123456;
Console.WriteLine("{0:C}", i); // £123,456.00
Console.WriteLine("{0:D}", i); // 123456
Console.WriteLine("{0:E}", i); // 1.234560E+005
Console.WriteLine("{0:F}", i); // 123456.00
Console.WriteLine("{0:G}", i); // 123456
Console.WriteLine("{0:N}", i); // 123,456.00
Console.WriteLine("{0:P}", i); // 12,345,600.00 %
Console.WriteLine("{0:X}", i); // 1E240
}
}

d) Objects and ToString


public class Thing
{
public int i = 2;
public int j = 3;
}
public class objectTypeApp
{
public static void Main()
{
object a;
a = 1;
Console.WriteLine(a);
Console.WriteLine(a.ToString());
Console.WriteLine(a.GetType());
Console.WriteLine();

Thing b = new Thing();


Console.WriteLine(b);
Console.WriteLine(b.ToString());
Console.WriteLine(b.GetType());
}
}
OUT PUT:
1
1
System.Int32

objectType.Thing
objectType.Thing
objectType.Thing
e) Strings and DateTime
using System.Globalization;

public class DatesApp


{
public static void Main(string[] args)
{
DateTime dt = DateTime.Now;
Console.WriteLine(dt);
Console.WriteLine("date = {0}, time = {1}\n",
dt.Date, dt.TimeOfDay);
Console.WriteLine(dt.ToString("D", dtfi));
Console.WriteLine(dt.ToString("f", dtfi));
Console.WriteLine(dt.ToString("F", dtfi));
Console.WriteLine(dt.ToString("g", dtfi));
Console.WriteLine(dt.ToString("G", dtfi));
Console.WriteLine(dt.ToString("m", dtfi));
Console.WriteLine(dt.ToString("r", dtfi));
Console.WriteLine(dt.ToString("s", dtfi));
Console.WriteLine(dt.ToString("t", dtfi));
Console.WriteLine(dt.ToString("T", dtfi));
Console.WriteLine(dt.ToString("u", dtfi));
Console.WriteLine(dt.ToString("U", dtfi));
Console.WriteLine(dt.ToString("d", dtfi));
Console.WriteLine(dt.ToString("y", dtfi));
Console.WriteLine(dt.ToString("dd-MMM-yy", dtfi));
}
}

OUTPUT:
23/06/2001 17:55:10
date = 23/06/2001 00:00:00, time = 17:55:10.3839296

03/01/2002
03/01/2002

03 January 2002
03 January 2002 12:55
03 January 2002 12:55:47
03/01/2002 12:55
03/01/2002 12:55:47
03 January
Thu, 03 Jan 2002 12:55:47 GMT
2002-01-03T12:55:47
12:55
12:55:47
2002-01-03 12:55:47Z
03 January 2002 12:55:47
03/01/2002
January 2002
03-Jan-02

f) StringBuilder class

class UseSBApp
{
static void Main(string[] args)
{
StringBuilder sb = new StringBuilder("Pineapple");
sb.Replace('e', 'X');
sb.Insert(4, "Banana");
sb.Append("Kiwi");
sb.AppendFormat(", {0}:{1}", 123, 45.6789);
sb.Remove(sb.Length - 3, 3);
Console.WriteLine(sb);
}
}

OutPut:
PinXBananaapplXKiwi, 123:45.6

g) Spliting Strings

class SplitStringApp
{
static void Main(string[] args)
{
string s = "Once Upon A Time In America";
char[] seps = new char[]{' '};
foreach (string ss in s.Split(seps))
Console.WriteLine(ss);
}
}

OutPut:
Once
Upon
A
Time
In
America
3. Implement a C# Program to Find Volume of Cube
and Cuboid Using Delegates.
CUBE:
using System;
public class VolumeOfCube
{
public static void Main()
{
Console.Write("Enter Side: ");
decimal Side = Convert.ToDecimal(Console.ReadLine());
decimal Volume = Side * Side * Side;
Console.WriteLine("Volume of cube=" + Volume);
Console.ReadKey();
}
}
OUTPUT

CUBOID:
using System;
class VolumeaOfCuboid
{
static void Main()
{
float length, width, height;
length = 5;
width = 3;
height = 4;
float volume = height * width * length;
Console.WriteLine("Volume of Cuboid is:" + volume);
Console.ReadKey();
}
}
OUTPUT
Volume of Cuboid is:60
4.Write a Program in C# to demonstrate Operator
overloading.
PROGERAM
// C# program to illustrate the
// unary operator overloading
using System;
namespace Calculator {
class Calculator {
public int number1 , number2;
public Calculator(int num1 , int num2)
{
number1 = num1;
number2 = num2;
}
// Function to perform operation
// By changing sign of integers
public static Calculator operator -(Calculator c1)
{
c1.number1 = -c1.number1;
c1.number2 = -c1.number2;
return c1;
}
// Function to print the numbers
public void Print()
{
Console.WriteLine ("Number1 = " + number1);
Console.WriteLine ("Number2 = " + number2);
}
}
class EntryPoint
{
// Driver Code
static void Main(String []args)
{ // using overloaded - operator
// with the class object
Calculator calc = new Calculator(15, -25);
calc = -calc;
// To display the result
calc.Print();
}
}
}

OUTPUT
Number1 = -15
Number2 = 25
5. Write a program in C# to demonstrate the usage of
object and class.
PROGRAM
using System;
public class Student
{
int id;//data member (also instance variable)
String name;//data member(also instance variable)
public static void Main(string[] args)
{
Student s1 = new Student();//creating an object of Student
s1.id = 101;
s1.name = "Sonoo Jaiswal";
Console.WriteLine(s1.id);
Console.WriteLine(s1.name);

}
}

OUTPUT:

101
Sonoo Jaiswal

6. Create a webpage to implement all validation controls


in ASP.Net.
PROGRAM
REQUIRED FIELD VALIDATION CONTROL
The Required Field Validator control is simple validation control, which checks to see if the
data is entered for the input control. You can have a Required Field Validator control for each
form element on which you wish to enforce Mandatory Field rule.

1. <asp:RequiredFieldValidator ID="RequiredFieldValidator3" runat="server"


2. Style="top: 98px; left: 367px; position: absolute; height: 26px; width: 162px"
3. ErrorMessage="password required" ControlToValidate="TextBox2">
4. </asp:RequiredFieldValidator>

COMPARE VALIDATOR CONTROL


The Compare Validator control allows you to make comparison to compare data entered in an
input control with a constant value or a value in a different control.

It can most commonly be used when you need to confirm password entered by the user at the
registration time. The data is always case sensitive.

1. <asp:RequiredFieldValidator ID="RequiredFieldValidator2" runat="server" Style="top:


145px;
2. left: 367px; position: absolute; height: 26px; width: 162px" ErrorMessage="passw
ord required"
3. ControlToValidate="TextBox3"></asp:RequiredFieldValidator>

RANGE VALIDATOR CONTROL


The Range Validator Server Control is another validator control, which checks to see if a
control value is within a valid range. The attributes that are necessary to this control are:
MaximumValue, MinimumValue, and Type.

1. <asp:RangeValidator ID="RangeValidator1" runat="server"


2. Style="top: 194px; left: 365px; position: absolute; height: 22px; width: 105px"
3. ErrorMessage="RangeValidator" ControlToValidate="TextBox4" MaximumValue="100
" MinimumValue="18" Type="Integer"></asp:RangeValidator>

REGULAREXPRESSIONVALIDATOR CONTROL
A regular expression is a powerful pattern matching language that can be used to identify
simple and complex characters sequence that would otherwise require writing code to
perform.

In the example I have checked the email id format:

1. <asp:RegularExpressionValidator ID="RegularExpressionValidator1" runat="server" St


yle="top: 234px;
2. left: 366px; position: absolute; height: 22px; width: 177px"
3. ErrorMessage="RegularExpressionValidator" ControlToValidate="TextBox5"
4. ValidationExpression="\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*"></asp:Reg
ularExpressionValidator>

The complete code for the above 4 controls is as,

Default.aspx Design

CUSTOMVALIDATOR CONTROL
You can solve your purpose with ASP.NET validation control. But if you still don't find
solution you can create your own custom validator control.

The CustomValidator Control can be used on client side and server side. JavaScript is used to
do client validation and you can use any .NET language to do server side validation
.

Source Code

1. <%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inhe


rits="_Default" %>
2. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://ww
w.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
3. <html xmlns="http://www.w3.org/1999/xhtml">
4. <head runat="server">
5. <title>Untitled Page</title>
6. </head>
7. <body>
8. <form id="form1" runat="server">
9. <div>
10. <asp:Label ID="Label1" runat="server" Text="User ID:"></asp:Label>
11. <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
12. <asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server"
13. ControlToValidate="TextBox1" ErrorMessage="User id required"></asp:Requir
edFieldValidator>
14.
15. <asp:CustomValidator ID="CustomValidator1" runat="server" OnServerValidate="
UserCustomValidate"
16. ControlToValidate="TextBox1"
17. ErrorMessage="User ID should have atleast a capital, small and digit and should
be greater than 5 and less
18. than 26 letters"
19. SetFocusOnError="True"></asp:CustomValidator>
20. </div>
21. <asp:Button ID="Button1" runat="server" onclick="Button1_Click" Text="Submit" /
>
22. </form>
23.

VALIDATION SUMMARY
ASP.NET has provided an additional control that complements the validator controls.
The Validation Summary control is reporting control, which is used by the other validation
controls on a page. You can use this validation control to consolidate errors reporting for all
the validation errors that occur on a page instead of leaving this up to each and every
individual validation control.The validation summary control will collect all the error
messages of all the non-valid controls and put them in a tidy list.

1. <asp:ValidationSummary ID="ValidationSummary1" runat="server"


2. style="top: 390px; left: 44px; position: absolute; height: 38px; width: 625px" />

Default.aspx Design
7.Program with ASP.Net by Connecting With SQL.
a.Create Login Form to Enter Into Website.
b. Building Web Form that Displays Data from a
Database.
8. Create a Website Using Master Page Concept in
ASP.Net
PROGRAM
Step 1

Create new project in Visual Studio 2015

Go to File-> New-> Web Site-> Visual C#->ASP.NET Empty Website-> Entry


Application Name-> OK.

Step 2

Create Master Page

Project name-> Add-> Add New Item->Visual C#-> Master Page->Write Master Page
Name-> Add.
HomePage.master master page is created.

Add HTML code in Homepage.master page, as shown below.

Note

Here, we used contentplaceholder control.


1. <%@ Master Language="C#" AutoEventWireup="true" CodeFile="HomePa
ge.master.cs" Inherits="HomePage" %>
2. <!DOCTYPE html>
3. <html xmlns="http://www.w3.org/1999/xhtml"> head runat="serv
er">
4. <title></title>
5. </head>
6.
7. <body>
8. <form id="form1" runat="server">
9. <div>
10. <table border="1">
11. <tr>
12. <td colspan="2" style="text-align: ce
nter">
13. <h1>Header</h1>
14. </td>
15. </tr>
16. <tr>
17. <td style="text-align: center; height
: 480px; width: 250px">
18. <h1>Menu</h1>
19. </td>
20. <td style="text-align: center; height
: 480px; width: 700px">
21. <asp:ContentPlaceHolder ID="MainC
ontentPlaceHolder1" runat="server">
22. <h1>you can change Content he
re</h1>
23. </asp:ContentPlaceHolder>
24. </td>
25. </tr>
26. <tr>
27. <td colspan="2" style="text-align: ce
nter">
28. <h1>Footer</h1>
29. </td>
30. </tr>
31. </table>
32. </div>
33. </form>
34. </body>
35.
36. </html>

Go to the design mode and you will see the image, as shown below.

Step 3

Adding the Content Pages to Master Page

Master Page -> Add Content Page.


Note

1. write your code inside Content Placeholder.See the below code.


2. <%@ Page Title="" Language="C#" MasterPageFile="~/HomePage.maste
r" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_
Default" %> < asp: Content ID = "Content1"
3. ContentPlaceHolderID = "MainContentPlaceHolder1"
4. runat = "Server" > < table > < tr > < th > < h1 > Student Inform
ation < /h1> < /th> < /tr> < tr > < td > < b > Name: Chetan Narg
und < /b> </td > < /tr> < tr > < td > < b > College: AIT < /b> <
/td > < /tr> < tr > < td > < b > City: Bangalore < /b></td > < /
tr> < /table> < /asp:Content>

Output
9. Create a Webpage to implement Application and
Session States in ASP.Net.
PROGRAM
Using System;
using System.collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;

public partial class _Default : System.Web.UI.Page{

protected void Page_Load(object sender, EventArgs e)


{

if(Session["user"] != null){
Response.Redirect("~/Welcome.aspx");
}

protected void btnLogin_Click(object sender, EventArgs e)

{
SqlConnection con = new
SqlConnection(ConfigurationManager.ConnectionString["connect"].ToString());

con.Open();

string query = "select count(*) from tblUsers where user_name = '"+txtUser.Text+"' and
user_pass = '"+txtPass.Text+"'";

SqlCommand cmd = new SqlCommand(query,con);

string output = cmd.ExecuteScalar().ToString();

if(output == "1")
{
Session["user"] = txtUser.Text; // Creating a Session | Naming a session | Adding
Value in session variable
Response.Redirect("~/Welcome.aspx");
}
else
{
Response.Write("Login Failed");
}
}
}
10. Create a Website for Online Movie Ticket
Reservation System Using Session Concept.
PROGRAM

// Java skeleton code to design an online movie

// booking system.

Enums :

public enum SeatStatus {

SEAT_BOOKED,

SEAT_NOT_BOOKED;

public enum MovieStatus {

Movie_Available,

Movie_NotAvailable;

public enum MovieType {

ENGLISH,

HINDI;

public enum SeatType {

NORMAL,

EXECUTIVE,

PREMIUM,

VIP;

public enum PaymentStatus {

PAID,

UNPAID;
}

class User {

int userId;

String name;

Date dateOfBirth;

String mobNo;

String emailId;

String sex;

class Movie {

int movieId;

int theaterId;

MovieType movieType;

MovieStatus movieStatus;

class Theater {

int theaterId;

String theaterName;

Address address;

List<Movie> movies;

float rating;

class Booking {

int bookingId;

int userId;

int movieId;

List<Movie> bookedSeats;
int amount;

PaymentStatus status_of_payment;

Date booked_date;

Time movie_timing;

class Address {

String city;

String pinCode;

String state;

String streetNo;

String landmark;

You might also like