C# Practical 7-13
C# Practical 7-13
C# Practical 7-13
namespace ConsoleApplication1
{
class myException : Exception
{
public myException(String message)
: base(message)
{
}
}
class testmyException
{
static void Main(string[] args)
{
int mark1, mark2, mark3;
Console.WriteLine("Enter mark1,mark2,mark3:");
mark1 = Int32.Parse(Console.ReadLine());
mark2 = Int32.Parse(Console.ReadLine());
mark3 = Int32.Parse(Console.ReadLine());
try
{
if ((mark1 > 100) || (mark2 > 100) || (mark3 >
100))
{
throw new myException("Mark out of Range:");
}
}
catch (myException e)
{
Console.WriteLine("Caught Exception");
Console.WriteLine(e.Message);
}
finally
{
Console.WriteLine("Bye");
Console.ReadLine();
}
Console.ReadKey();
}
}
}
Output:
8.Implement Inheritance
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication1
{
class a
{
protected string name;
public void Name()
{
Console.WriteLine("Enter your Name:");
name = Console.ReadLine();
}
}
class b : a
{
protected string regno;
public void RegNo()
{
Console.WriteLine("Enter Your RegNo:");
regno = Console.ReadLine();
}
}
class c : b
{
protected string major;
public void Major()
{
Console.WriteLine("Enter Your Major:");
major = Console.ReadLine();
}
}
class d : c
{
protected string college;
public void College()
{
Console.WriteLine("Enter Your College:");
college = Console.ReadLine();
}
public void show()
{
Console.WriteLine("\n\n Data Entered \n\
n");
Console.WriteLine(name);
Console.WriteLine(regno);
Console.WriteLine(major);
Console.WriteLine(college);
}
}
class Program
{
public static void Main(string[] args)
{
d z = new d();
z.Name();
z.RegNo();
z.Major();
z.College();
z.show();
Console.ReadKey();
}
}
}
Output:
9.Implement Operator Overloading
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace overloading
{
class Calculate
{
public int n1, n2;
public Calculate(int no1, int no2)
{
n1 = no1;
n2 = no2;
}
namespace ConsoleApplication1
{
class Shape
{
protected int width, height;
public Shape(int a, int b)
{
width = a;
height = b;
}
public virtual int area()
{
Console.WriteLine("Parent Class");
return 0;
}
}
class Rectangle : Shape
{
public Rectangle(int a, int b)
: base(a, b)
{
}
public override int area()
{
Console.WriteLine("Rectangle class area:");
return (width * height);
}
}
class Triangle:Shape
{
public Triangle(int a,int b):base(a,b)
{
}
public override int area()
{
Console.WriteLine("Triangle class area:");
return (width*height/2);
}
}
class Caller
{
public void CallArea(Shape s)
{
int a;
a=s.area();
Console.WriteLine("Area {0}",a);
}
}
class DyPoly
{
static void Main(string[] args)
{
Caller c = new Caller();
Rectangle r = new Rectangle(10, 7);
Triangle t = new Triangle(10, 5);
c.CallArea(r);
c.CallArea(t);
Console.ReadKey();
}
}
}
Output:
11. Implement Interfaces
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace InterfaceApplication
{
public interface ITransaction
{
void showTransaction();
double getAmount();
Transaction(string c, string d, double a);
}
public class Transaction : ITransaction
{
private string tcode;
private string date;
private double amount;
public Transaction(string c, string d, double a)
{
tcode = c;
date = d;
amount = a;
}
public double getAmount()
{
return amount;
}
public void showTransaction()
{
Console.WriteLine("Transaction: {0}",tcode);
Console.WriteLine("Date: {0}", date);
Console.WriteLine("Amount: {0}", getAmount());
}
}
class Tester
{
static void Main(string[] args)
{
Transaction t1 = new
Transaction("001","8/2/2019",78900.00);
Transaction t2 = new Transaction ("002", "9/2/2019" ,
451900.00);
t1.showTransaction();
t2.showTransaction();
Console.ReadKey();
}
}
}
Output:
12(a). Implement overriding methods
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication4
{
class A1
{
public virtual void abc()
{
Console.WriteLine("In A");
}
}
class B1 : A1
{
public override void abc()
{
Console.WriteLine("In B");
}
}
class C1 : B1
{
public override void abc()
{
Console.WriteLine("In C");
}
}
class Ride
{
static void Main(string[] args)
{
A1 z1 = new A1();
B1 z2 = new B1();
C1 z3 = new C1();
z1.abc();
z2.abc();
z3.abc();
Console.ReadKey();
}
}
}
Output:
12(b). Implement hiding methods.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication5
{
class A1
{
public new void abc()
{
Console.WriteLine("In A");
}
}
class B1 : A1
{
public new void abc()
{
Console.WriteLine("In B");
}
}
class C1 : B1
{
public new void abc()
{
Console.WriteLine("In C");
}
}
class Ride
{
static void Main(string[] args)
{
A1 z1 = new A1();
B1 z2 = new B1();
C1 z3 = new C1();
z1.abc();
z2.abc();
z3.abc();
Console.ReadKey();
}
}
}
Output:
13.Copy Contents File
using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
namespace file
{
class Program
{
static void Main(string[] args)
{
File.Copy("E:\\sam.txt", "E:\\sam3.txt");
File.Copy("E:\\sam.txt", "E:\\sam4.txt");
Console.WriteLine(File.ReadAllText("E:\\sam.txt"));
Console.WriteLine(File.ReadAllText("E:\\sam3.txt"));
Console.WriteLine(File.ReadAllText("E:\\sam4.txt"));
Console.ReadKey();
}
}
}
Output: