C#updated

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 15

Experiment No.

7
1. Program to implement the following multiple inheritance using interface
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApp27
{
public interface Gross
{
int TA { get; set; }
int DA { get; set; }
void Gross_sal();
}
internal class Employee
{
public string Name;
public int bsal;
public void Basic_sal()
{
Console.Write("Enter Name:");
Name = Console.ReadLine();
Console.Write("Enter basic salary:");
bsal = int.Parse(Console.ReadLine());
}
}

class Salary : Employee, Gross


{
public int HRA { get; set; }
private int ta, da;
public int TA
{
get { return ta; }
set { ta = value; }
}
public int DA
{
get { return da; }
set { da = value; }
}

void Gross.Gross_sal()
{
Console.Write("Enter TA:");
TA = int.Parse(Console.ReadLine());
Console.Write("Enter DA:");
DA = int.Parse(Console.ReadLine());
Console.Write("Enter HRA:");
HRA = int.Parse(Console.ReadLine());
}

void Display_sal()
{
Console.WriteLine($"Name:{Name}");
Console.WriteLine($"Basic salary:{bsal} Gross Salary:{TA + DA +HRA}
Total Salary:{bsal + TA + DA + HRA}");
}

static void Main(string[] args)


{
Console.Write("Enter Number of Employees:");
Salary[] s = new Salary[int.Parse(Console.ReadLine())];
Gross[] gross = s;

for(int i=0;i<gross.Length;i++)
{
s[i] = new Salary();
s[i].Basic_sal();

gross[i] = s[i];
gross[i].Gross_sal();
s[i].Display_sal();
}
Console.ReadLine();
}
}
}

Output:
2. Program to implement default implementation and Explicit implementation using interface

2.1 Default implementation

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

namespace ConsoleApp28
{
public interface Intf1
{
void Display();
}

internal class defImple : Intf1


{
public void Display()
{
string name;
Console.Write("Enter name:");
name = Console.ReadLine();
Console.WriteLine("This is implemented method from the interface");
Console.WriteLine($"Hello....!{name}");
}
}

internal class Program


{
private static void Main(string[] args)
{
defImple def = new defImple();
Intf1 intf1 = def;
intf1.Display();
Console.ReadLine();
}
}
}
Output:
3. Program to calculate the area of square using abstract class and abstract method

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

namespace ConsoleApp30
{
public abstract class Squares
{
public abstract void Area();
}

class Calc : Squares


{
public override void Area()
{
double side;
Console.WriteLine("Enter Value of side:");
side = double.Parse(Console.ReadLine());
Console.WriteLine($"Area of Square is:{side * side}");
}

static void Main(string[] args)


{
Calc cal = new Calc();
cal.Area();
Console.ReadLine();

}
}
}

Output:

4. Program to implement the following non-abstract and abstract method using abstract class

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

namespace ConsoleApp31
{
public abstract class AbstractClass
{
public void AddTwoNumbers(int a,int b)
{
Console.WriteLine($"Addition of {a}&{b} is:{a + b}");
}
public abstract void MultiplyTwoNumbers(int a, int b);
}
class Calc : AbstractClass
{
public override void MultiplyTwoNumbers(int a, int b)
{
Console.WriteLine($"Multiplication of {a}&{b} is:{a * b}");
}

static void Main(string[] args)


{
Calc cal = new Calc();
Console.Write("Enter First Number:");
int num1 = int.Parse(Console.ReadLine());
Console.WriteLine("Enter second number:");
int num2 = int.Parse(Console.ReadLine());
cal.AddTwoNumbers(num1, num2);
cal.MultiplyTwoNumbers(num1, num2);
Console.ReadLine();
}
}
}

Output:
Experiment No.8
1. Write a program to create an enum . here an enum with name month is created and its
data members are the name of months like Jan,Feb,Mar,Apr,May,etc. Now lets try to
print the default integer values of these enums.

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

namespace ConsoleApp32
{
public class Months
{
public enum months { Jan = 1, Feb, March, April, May, June, July, Aug, Sep,
Oct, Nov, Dec };
static void Main(string[] args)
{
Console.WriteLine($"Jan:{(int)months.Jan}");
Console.WriteLine($"Feb:{(int)months.Feb}");
Console.WriteLine($"March:{(int)months.March}");
Console.WriteLine($"Aprtil:{(int)months.April}");
Console.WriteLine($"May:{(int)months.May}");
Console.WriteLine($"June:{(int)months.June}");
Console.WriteLine($"July:{(int)months.July}");
Console.WriteLine($"Aug:{(int)months.Aug}");
Console.WriteLine($"Sep:{(int)months.Sep}");
Console.WriteLine($"Oct:{(int)months.Oct}");
Console.WriteLine($"Nov:{(int)months.Nov}");
Console.WriteLine($"Dec:{(int)months.Dec}");

Console.ReadLine();
}
}
}
Output:
2. Write a program to create an enum . Here an enum with name, gender is created and
its data members are the name of Male, Female, Unknown. Now lets try to print the
Name and Gender(Using Switch case)
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices.WindowsRuntime;
using System.Text;
using System.Threading.Tasks;

namespace Enumpack
{
enum Gender { Male=1,Female=2,Unknown=3};
public class Info
{
string name;
int genderid;
public static string Getgender(int gender)
{
switch (gender)
{
case 1:
if ((int)Gender.Male == gender)
{
return "Male";
}
retun null;

case 2:
if ((int)Gender.Female == gender)
{
return "Female";
}
return null;
case 3:
if ((int)Gender.Unknown == gender)
{
return "Unkwown";
}
return null;
default return "Invalid Data for Gender";
}
}
public static void main(string[] args)
{
Console.Write("Enter Number of students:");
int size = int.Parse(Console.ReadLine());
Info[] infos = new Info[size];
Console.WriteLine("gender: 1-Male, 2-Female, 3-Unknown");
for (int i = 0; i < size; i++)
{
infos[i] = new Info();
Console.Write("Enter Name:");
infos[i].name = Console.ReadLine();
Console.Write("Enter Gender Value:");
infos[i].genderid = int.Parse(Console.ReadLine());
}
Console.WriteLine("\n_____Entered Details_____");
for (int i = 0; i < size; i++)
{

Console.WriteLine($"Name={infos[i].name}&&Gender={Getgender(infos[i].gende
rid)}");
}
Console.ReadLine();
}
}
}

Output:
Experiment No.9
1. Write a program to accept a number form the user and throw an exception if the number is
not an even number

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

namespace ConsoleApp34
{
internal class Excep
{
static void Main(string[] args)
{
Console.Write("Enter Even Number:");
int num = int.Parse(Console.ReadLine());
if(num%2!=0)
{
throw new Exception("Number is not an even number");
}
Console.WriteLine("Even Number");
Console.ReadLine();
}
}
}
Output:
2. Write a program to accept a two number form the user and divide the number and multiple
try catch block exception.(Format Exception, Overflow Exception,DivideBy Zero
Exception,Exception).

using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Remoting.Channels;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApp35
{
internal class Excep
{
static void Main(string[] args)
{
Console.Write("Enter 1st number:");
int num = int.Parse(Console.ReadLine());
Console.Write("Enter end number:");
int num1 = int.Parse(Console.ReadLine());

try
{
Console.WriteLine($"Division is:{(num / num1)}");
}
catch (DivideByZeroException e)
{
Console.WriteLine($"\nError:{e}");
}
try
{
num = int.Parse("v");
Console.WriteLine($"Conversion is:{(num * 999999999999)}");
}

catch (FormatException e)
{
Console.WriteLine($"\nError:{e}");

}
try
{
char b = Convert.ToChar(num);
Console.WriteLine($"Conversion is:{b}");
}

catch (OverflowException e)
{
Console.WriteLine($"\nError:{e}");
}
try
{
num = int.Parse("1.5");
}

catch (Exception e)
{
Console.WriteLine($"\nError:{e}");
}
Console.ReadLine();
}
}
}
Output:
3. Write a program to user-Defined Exception

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

namespace ConsoleApp36
{
internal class InvalidMarksExcep:Exception
{
public InvalidMarksExcep(String msg)
: base(msg)
{
Console.WriteLine("Exception Occured");
}
}

class UserDefinedExcep
{
public static void Main(string[] args)
{
try
{
Console.Write("Enter your marks:");
int marks = int.Parse(Console.ReadLine());
if (marks>100 || marks<1)
{
throw new InvalidMarksExcep("Marks must be between 0-100");

}
}
catch(InvalidMarksExcep e)
{
Console.WriteLine(e);
}
Console.ReadLine();
}
}
}

Output:
Experiment No.10
1. Study of boxing and Unboxing

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

namespace ConsoleApp37
{
internal class Boxing
{
static void Main(string[] args)
{
Console.Write("Enter Number:");
int myVal = int.Parse(Console.ReadLine());
//boxing
object myBoxed = myVal;
//unboxing
int myUnBoxed = (int)myBoxed;

Console.WriteLine($"Value from variable:{myVal}");


Console.WriteLine($"Value from object passed from variable:{myBoxed}");
Console.WriteLine($"Value from variable passed from
object:{myUnBoxed}");

int[] arra = new int[5];


//boxing
arra[0] = myVal;
//unboxing
int val = (int)arra[0];
Console.WriteLine("Value Passed to Array from variables:" + arra[0]);
Console.WriteLine("Value Passed from Array to variable:" + val);
Console.ReadLine();
}
}
}
Output:
2. If you have two integers stored in variable var1 & var2, what Boolean test can you perform
to see if not both numbers are greater than 10?

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

namespace ConsoleApp38
{
internal class Grt10
{
static void Main(string[] args)
{
Console.Write("Enter var1 value:");
int var1 = int.Parse(Console.ReadLine());
Console.Write("Enter var2 value:");
int var2 = int.Parse(Console.ReadLine());

if(var1>=10 && var2>=10)


{
Console.WriteLine("Both values cannot be greater than or equal to
10");
Console.ReadLine();
}
else if(var1>=10)
{
Console.WriteLine("Variable 1 greater tahn or equal to 10");
Console.ReadLine();
}
else if(var2>=10)
{
Console.WriteLine("Variable 2 greater than or equal to 10");

Console.ReadLine();
}
else
{
Console.WriteLine("Both Values are less than 10");
Console.ReadLine();
}
}
}
}

Output:

You might also like