Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 14

Table of Contents

Lab Report Lab Report Contents


Number
1 Write a program to demonstrate the differences between Console
ReadLine() and Console.Read(), Console.Write() and
Console.WriteLine() in C#.
2 Concept on Polymorphism
Write a program to add numbers x, y and z. One function to add x and
y another function to add all three numbers. Print on screen as given
output.
Sum of 5 and 6 = 11
Sum of 5, 6 and 7 = 18
3 Write a program to implement the concept of default constructor,
parameterized constructorand private constructor in C#.
4 Write a program to implement the concept of operatot (+) overloading.
5 Write a concept of multilevel inheritance and multiple inheritance in
C#.
6 Write a program on Method overloading and Method Overriding in C#.
Lab 1

Write a program to demonstrate the differences between Console ReadLine()


and Console.Read(), Console.Write() and Console.WriteLine() in C#.

 Console.ReadLine()

Source Code:

using System;

namespace Lab1
{
class Program
{
static void Main(string[] args)
{
Console.Write("Input: ");
string input = Console.ReadLine();
Console.WriteLine($"Output: {input}");
Console.ReadKey();
}
}
}

Output:
 Console.Read()

Source Code:

using System;
namespace lab1
{
class Program
{
static void Main(string[] args)
{
Console.Write("Input: ");
Console.Write("Output: " + Convert.ToChar(Console.Read()));
Console.ReadKey();
}
}
}

Output:

 Console.Write()

Source Code:

using System;
namespace lab1
{
class Program
{
static void Main(string[] args)
{
Console.Write("I am students ");
Console.Write("of ");
Console.Write("JMC COllege");
Console.ReadKey();
}
}
}

Output:
 Console.WriteLine()

Source Code:

using System;
namespace lab1
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("I am Students ");
Console.WriteLine("of ");
Console.WriteLine("JMC College");
Console.ReadKey();
Console.ReadKey();
}
}
}

Output:
Lab 2
Write a program to add numbers x, y and z. One function to add x and y another
function to add all three numbers. Print on screen as given output.
Sum of 5 and 6 = 11
Sum of 5, 6 and 7 = 18

Source Code:

using System;
namespace lab2
{
class Program
{
public void Add(int x, int y)
{
Console.WriteLine("Sum of {0} and {1} = {2}", x, y, (x + y));
}

public void Add(int x, int y, int z)


{
Console.WriteLine("Sum of {0}, {1} and {2} = {3}", x, y, z, (x + y + z));
}

static void Main(string[] args)


{
Program ob = new Program();
ob.Add(5, 6);
ob.Add(5, 6, 7);
Console.ReadKey();
Console.ReadKey();
}
}
}

Output:
Lab 3
Write a program to implement the concept of default constructor, parameterized
constructorand private constructor in C#.

 Default Constructor in C#

Source Code:

using System;
namespace DefaultConstractor
{
class addition
{
int a, b;

public addition() {
a = 100;
b = 175;
}

public static void Main()


{
addition obj = new addition();
Console.WriteLine(obj.a);
Console.WriteLine(obj.b);
Console.Read();
}
}
}

Output:
 Parameterized Constructor in C#

Source Code:

using System;
namespace Constructor
{
class paraconstrctor
{
public int a, b;

public paraconstrctor(int x, int y) {


a = x;
b = y;
}
}

class MainClass
{
static void Main()
{
paraconstrctor v = new paraconstrctor(100, 175);
Console.WriteLine("-----------parameterized constructor example by
vithal wadje---------------");
Console.WriteLine("\t");
Console.WriteLine("value of a=" + v.a);
Console.WriteLine("value of b=" + v.b);
Console.Read();
}
}
}

Output:
 Private Constructor in C#

Source Code:

using System;
namespace defaultConstractor
{
public class Counter
{
private Counter() {
}

public static int currentview;

public static int visitedCount()


{
return ++currentview;
}
}

class viewCountedetails
{
static void Main()
{
Console.WriteLine("-------Private constructor example by vithal
wadje----------");
Console.WriteLine();
Counter.currentview = 500;
Counter.visitedCount();
Console.WriteLine("Now the view count is: {0}",
Counter.currentview);
Console.ReadLine();
}
}
}

Output:
Lab 4

Write a program to implement the concept of operatot (+) overloading.

Source Code:

using System;
namespace lab4
{
namespace BinaryOverload
{
class Calculator
{
public int number = 0;

public Calculator() { }

public Calculator(int n)
{
number = n;
}

public static Calculator operator +(Calculator Calc1, Calculator Calc2)


{
Calculator Calc3 = new Calculator(0);
Calc3.number = Calc2.number + Calc1.number;
return Calc3;
}

public void display()


{
Console.WriteLine("{0}", number);
}

static void Main(string[] args)


{
Calculator num1 = new Calculator(200);
Calculator num2 = new Calculator(40);
Calculator num3 = new Calculator(); Output:
num3 = num1 + num2;
num1.display(); // Displays 200
num2.display(); // Displays 40
num3.display(); // Displays 240
Console.ReadKey();
}
}
}
}
Lab 5

Write a concept of multilevel inheritance and multiple inheritance in C#.

[Note: Multiple inheritance doesnot support in Class but use in interface]

 Multiple Inheritance

Source Code:

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

namespace MultipleInheritApplication
{
interface calc1
{
int add(int a, int b);
}

interface calc2
{
int sub(int x, int y);
}

interface calc3
{
int mul(int r, int s);
}

interface calc4
{
int div(int c, int d);
}

class Calculation : calc1, calc2, calc3, calc4


{
public int result1;
public int add(int a, int b)
{
return result1 = a + b;
}

public int result2;


public int sub(int x, int y)
{
return result2 = x - y;
}

public int result3;


public int mul(int r, int s)
{
return result3 = r * s;
}

public int result4;


public int div(int c, int d)
{
return result4 = c / d;
}
}

class Program
{
static void Main(string[] args)
{
Calculation c = new Calculation();
c.add(8, 2);
c.sub(20, 10);
c.mul(5, 2);
c.div(20, 10);

Console.WriteLine("Multiple Inheritance concept Using Interfaces :\n ");


Console.WriteLine("Addition: " + c.result1);
Console.WriteLine("Subtraction: " + c.result2);
Console.WriteLine("Multiplication :" + c.result3);
Console.WriteLine("Division: " + c.result4);
Console.ReadKey();
}
}
}

Output:
 Multilevel Inheritance

Source Code:

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

namespace Demo
{
class Son : Father
{
public void DisplayTwo()
{
Console.WriteLine("Son.. ");
}

static void Main(string[] args)


{
Son s = new Son();
s.Display();
s.DisplayOne();
s.DisplayTwo();
Console.Read();
}
}

class Grandfather
{
public void Display()
{
Console.WriteLine("Grandfather...");
}
}

class Father : Grandfather


{
public void DisplayOne() Output:
{
Console.WriteLine("Father...");
}
}
}
Lab 6

Write a program on Method overloading and Method Overriding in C#.

 Overiding

Source Code:

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

namespace OverridingExample
{
class Subject // Base class
{
public virtual void study() // Base class method
{
Console.WriteLine("Study all the subjects");
}
}

class Mathematics : Subject // Derived class


{
public override void study() // Derived class method
{
base.study(); // Calls the base class method
Console.WriteLine("Study Mathematics");
}
}

class Program
{
// Main method
static void Main(string[] args)
{
Mathematics m = new Mathematics();
m.study();
Console.ReadLine();
}
}
} Output:
 Overloading

Source Code:

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

namespace OverloadingExample
{
class Demo
{
public int Sum(int x, int y)
{
int value = x + y;
return value;
}

public int Sum(int x, int y, int z)


{
int value = x + y + z;
return value;
}

public static void Main(string[] args) // Main method


{
Demo d = new Demo();
int sum1 = d.Sum(24, 28);
Console.WriteLine("Sum of the two integer values: " + sum1);

int sum2 = d.Sum(10, 20, 30);


Console.WriteLine("Sum of the three integer values: " + sum2);

Console.ReadLine();
}
}
}

Output:

You might also like