0% found this document useful (0 votes)
27 views

Tutorial

The document discusses an employee class with methods to calculate HRA, DA, and total pay for different employees. It also discusses single level inheritance with examples of setting and getting data from parent and child classes.

Uploaded by

Abhay Shukla
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
27 views

Tutorial

The document discusses an employee class with methods to calculate HRA, DA, and total pay for different employees. It also discusses single level inheritance with examples of setting and getting data from parent and child classes.

Uploaded by

Abhay Shukla
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 22

Tutorial – 4

1. The employee list for a company contains employee code, name, designation and
basic pay. The employee is given a house rent allowance (HRA) of 10% of the
basic pay and dearness allowance (DA) of 45% of the basic pay. The total pay of
the employee is calculated as Basic Pay + HRA + DA. Write a class to define the
details of the employee. Write a constructor to assign the required initial values.
Add a method to calculate HRA, DA and total pay and print them. Write
another class with main method. Create objects for three different employees
and calculate HRA, DA and total pay.
Code:-
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace t4
{
internal class p1
{
class Employee
{
// Employee details
private int employeeCode;
private string name;
private string designation;
private double basicPay;

// Constructor to assign initial values


public Employee(int code, string empName, string empDesignation, double
empBasicPay)
{
employeeCode = code;
name = empName;
designation = empDesignation;
basicPay = empBasicPay;
}

// Method to calculate HRA


private double CalculateHRA()
{
return 0.10 * basicPay;
}

// Method to calculate DA
private double CalculateDA()
{
return 0.45 * basicPay;
}

// Method to calculate total pay


private double CalculateTotalPay()
{
return basicPay + CalculateHRA() + CalculateDA();
}

// Method to print details


public void PrintDetails()
{
Console.WriteLine("Employee Code: " + employeeCode);
Console.WriteLine("Name: " + name);
Console.WriteLine("Designation: " + designation);
Console.WriteLine("Basic Pay: " + basicPay);
Console.WriteLine("HRA: " + CalculateHRA());
Console.WriteLine("DA: " + CalculateDA());
Console.WriteLine("Total Pay: " + CalculateTotalPay());
Console.WriteLine();
}
}
static void Main(string[] args)
{
Console.WriteLine("abhay");
// Create objects for three employees
Employee employee1 = new Employee(101, "abhay", "Software Engineer",
500000);
Employee employee2 = new Employee(102, "shubham", "HR Manager",
60000);
Employee employee3 = new Employee(103, "kandarp", "Sales Executive",
150);

// Calculate and print details


Console.WriteLine("Employee Details:");
Console.WriteLine("=================");
employee1.PrintDetails();
employee2.PrintDetails();
employee3.PrintDetails();
Console.ReadLine();
}
}
}

2. From the following code and given output, complete missing statements and find
out error code and correct it.

Code:-
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace t4
{
internal class p2
{
class Shape
{
public double Width;
public double Height;

public void ShowDim()


{
Console.WriteLine("Width and height are " + Width + " and " + Height);
}
}

class Triangle : Shape


{
public string Style;

public Triangle(double width, double height, string style)


{
Width = width;
Height = height;
Style = style;
}

public double Area()


{
return Width * Height / 2;
}

public void ShowStyle()


{
Console.WriteLine("Triangle is " + Style);
}
}

static void Main(string[] args)


{
Triangle t1 = new Triangle(4.0, 4.0, "isosceles");
Triangle t2 = new Triangle(8.0, 12.0, "right");

Console.WriteLine("Info for t1: ");


t1.ShowStyle();
t1.ShowDim();
Console.WriteLine("Area is " + t1.Area());

Console.WriteLine();

Console.WriteLine("Info for t2: ");


t2.ShowStyle();
t2.ShowDim();
Console.WriteLine("Area is " + t2.Area());
Console.ReadLine();
}
}
}
3. Draw a real picture for single level inheritance.
Perform following tasks.
Task 1: Create a class
Task 2: Add few data members as private, protected and public
Task 3: Add few methods as public to work on defined data members
Task 4: Create another applicable class which inherits members from above class
Task 5: Add few data members as private, protected and public into second class
Task 6: Add few methods as public to work on defined data members into second class
Task 7: Create a Demo class with main method.
Task 8: Create at least two objects of a second class defined in Task 4 into main method
and call all methods using that object.
Task 9: Write comment for each important portion of code like data members’
declaration, methods, some important logic etc.
Task 10: Summarize above solution in your own few words to visualize the solution to
the end user.

Code:-
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace t4
{
internal class p3
{
// Task 1:
class ParentClass
{
// Task 2:
private int privateData;
protected int protectedData;
public int publicData;

// Task 3:
public void SetPrivateData(int value)
{
privateData = value;
}

public void SetProtectedData(int value)


{
protectedData = value;
}

public void SetPublicData(int value)


{
publicData = value;
}
public void PrintData()
{
Console.WriteLine("Private Data: " + privateData);
Console.WriteLine("Protected Data: " + protectedData);
Console.WriteLine("Public Data: " + publicData);
}
}

// Task 4:
class ChildClass : ParentClass
{
// Task 5:
private string privateInfo;
protected string protectedInfo;
public string publicInfo;

// Task 6:
public void SetPrivateInfo(string info)
{
privateInfo = info;
}

public void SetProtectedInfo(string info)


{
protectedInfo = info;
}
public void SetPublicInfo(string info)
{
publicInfo = info;
}

public void PrintInfo()


{
Console.WriteLine("Private Info: " + privateInfo);
Console.WriteLine("Protected Info: " + protectedInfo);
Console.WriteLine("Public Info: " + publicInfo);
}
}

static void Main(string[] args)


{
Console.WriteLine("Abhay");

// Task 8:
ChildClass obj1 = new ChildClass();
ChildClass obj2 = new ChildClass();

// Call methods using object obj1


obj1.SetPrivateData(10);
obj1.SetProtectedData(20);
obj1.SetPublicData(30);
obj1.SetPrivateInfo("Private Information 1");
obj1.SetProtectedInfo("Protected Information 1");
obj1.SetPublicInfo("Public Information 1");

Console.WriteLine("Data of obj1:");
obj1.PrintData();
obj1.PrintInfo();

Console.WriteLine();

// Call methods using object obj2


obj2.SetPrivateData(100);
obj2.SetProtectedData(200);
obj2.SetPublicData(300);
obj2.SetPrivateInfo("Private Information 2");
obj2.SetProtectedInfo("Protected Information 2");
obj2.SetPublicInfo("Public Information 2");

Console.WriteLine("Data of obj2:");
obj2.PrintData();
obj2.PrintInfo();
Console.ReadLine();
}
}
}
4. From the following code and given output complete missing statements and
find out error code and correct it.
Code:-
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace t4
{
internal class p4
{
class StaticVar
{
public static int num;
public void counting()
{
num++;
}

public static int getNum()


{
return num;
}
}
static void Main(string[] args)
{
Console.WriteLine("abhay");

StaticVar s = new StaticVar();


s.counting();
s.counting();
s.counting();

Console.WriteLine("Variable num: {0}", StaticVar.getNum());


Console.ReadLine();

}
}
}
5. Find out error code and correct it. Print appropriate output as desired.
Code:-
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace t4
{
internal class p5
{
public class A
{
public A(int value)
{

Console.WriteLine("Base constructor A()");


}
}

public class B : A
{
public B(int value) : base(value)
{
Console.WriteLine("Derived constructor B()");
}
}
static void Main(string[] args)
{
Console.WriteLine("abhay");

A a = new A(0);
B b = new B(1);

Console.ReadLine();
}
}
}

6. Find out error code and correct it. Print appropriate output as desired.
Code:-
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace t4
{
internal class p6
{
abstract class Test
{
protected int a;
public abstract void A();
}

class Example1 : Test


{
public override void A()
{
Console.WriteLine("Example1.A");
a++;
}
}

class Example2 : Test


{
public override void A()
{
Console.WriteLine("Example2.A");
a--;
}
}
static void Main(string[] args)
{
Console.WriteLine("abhay");
Test test1 = new Example1();
test1.A();

Test test2 = new Example2();


test2.A();
Console.ReadLine();
}
}
}

7. Refer given output and find out error code and correct it.
Code:-

namespace t4
{
internal class p7
{
class A
{
public int x;
public int y;
}

class B : A
{
public int z;
}
static void Main(string[] args)
{
Console.WriteLine("abhay");

A sc = new A();
sc.x = 110;
sc.y = 150;
Console.WriteLine("x = {0}, y = {1}", sc.x, sc.y);
Console.ReadLine();
}
}
}

8. Find out error code and correct it. Print appropriate output as desired.
Code:-
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace t4
{
class X
{
public virtual void F() { Console.WriteLine("X.F"); }
public virtual void F2() { Console.WriteLine("X.F2"); }
}
class Y : X
{
public override void F() { Console.WriteLine("Y.F"); }
public override void F2() { Console.WriteLine("Y.F2"); }
}
class Z : Y
{
}

class p8
{
static void Main()
{
Console.WriteLine("abhay");
Y Obj2 = new Y();
Obj2.F();
Obj2.F2();
Z Obj3 = new Z();
Obj3.F();
Obj3.F2();

Console.ReadLine();
}
}
}

9. This program will throw an exception. Add try, catch and finally blocks to
handle this exception.
Code:-
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace t4
{
internal class p9
{
static void Main(string[] args)
{
Console.WriteLine("abhay");

int x = 0;
try
{
int div = 100 / x;
Console.WriteLine(div);
}
catch (DivideByZeroException ex)
{
Console.WriteLine("Error: " + ex.Message);
}
finally
{
Console.WriteLine("Finally block executed.");
}
Console.ReadLine();
}
}
}

10. Arrange the code to get desirable output


Code:-
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace t4
{
internal class p10
{
class MyException : Exception
{
public MyException(string str, Exception e) : base(str, e)
{
}
}
static void Main(string[] args)
{
Console.WriteLine("abhay");

try
{
throw new MyException("my exception generated.", new Exception());
}
catch (Exception e)
{
Console.WriteLine("Exception caught here: " + e.Message);
}
Console.WriteLine("LAST STATEMENT");
Console.ReadLine();
}
}
}

You might also like