C#updated
C#updated
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());
}
}
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}");
}
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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApp28
{
public interface Intf1
{
void Display();
}
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();
}
}
}
}
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}");
}
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;
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());
Console.ReadLine();
}
else
{
Console.WriteLine("Both Values are less than 10");
Console.ReadLine();
}
}
}
}
Output: