Lab Tutorial - 4
- Due No Due Date
- Points 10
- Submitting a file upload
Sample Format for file upload.docx
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.
2 : From the following code and given output, complete missing statements and find out error code and correct it.
Solution:
//Base class or Parent class.
class Shape
{
public double Width;
public double Height;
public void ShowDim()
{
Console.WriteLine("Width and height are " + Width + " and " + Height);
}
}
// Triangle is derived from Shape.
//Drived class or Child class.
class Triangle : Shape
{
public string Style; // style of triangle
// Return area of triangle.
//………………………………Missing statement-1……………………………….//
{
return Width * Height / 2;
}
// Display a triangle's style.
public void ShowStyle()
{
//………………………………Missing statement-2……………………………….//
}
}
//Driver class which runs the program.
class Driver
{
static void Main()
{
Triangle t1 new Triangle();
Triangle t2 new Triangle();
t1.Width =4.0;
t1.Height =4.0;
t1.Style ="isosceles";
t2.Width =8.0;
t2.Height =12.0;
t2.Style ="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());
}
}
Output
Info for t1:
Triangle is isosceles
Width and height are 4 and 4
Area is 8
Info for t2:
Triangle is right
Width and height are 8 and 12
Area is 48
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.
4 : From the following code and given output complete missing statements and find out error code and correct it.
using System;
namespace StaticVarApplication
{
class StaticVar
{
public static int num;
public counting()
{
num++;
}
//………………………………Missing statement……………………………….//
{
return num;
}
}
class StaticTester
{
static void Main(string[] args)
{
StaticVar s = new StaticVar();
s.count();
s.count();
s.count();
Console.WriteLine("Variable num: {0}", StaticVar.getNum());
Console.ReadKey();
}
}
}
Output:
Variable num: 3
5 : Find out error code and correct it. Print appropriate output as desired.
using System;
public class A // This is the base class.
{
public A(int value)
{
// Executes some code in the constructor.
Console.WriteLine("Base constructor A()");
}
}
public class B : A // This class derives from the previous class.
{
public B(int value) : base(value)
{
// The base constructor is called first.
// ... Then this code is executed.
Console.WriteLine("Derived constructor B()");
}
}
class Program
{
static void Main()
{
// Create a new instance of class A, which is the base class.
// ... Then create an instance of B, which executes the base constructor.
A a = A(0);
B b = B(1);
}
}
6 : Find out error code and correct it. Print appropriate output as desired.
Solution:
using System;
abstract class Test
{
int a;
abstract void A();
}
class Example1 : Test
{
public override void A()
{
Console.WriteLine("Example1.A");
base.a++;
}
}
class Example2 : Test
{
public override void A()
{
Console.WriteLine("Example2.A");
base.a--;
}
}
class Program
{
static void Main()
{
// Reference Example1 through Test type.
Test test1 = new Example1();
test1.A();
// Reference Example2 through Test type.
Test test2 = new Example2();
test2.A();
}
}
7 : Refer given output and find out error code and correct it.
sealed class A
{
public int x;
public int y;
}
class B : A
{
public int z;
}
class SealedTest2
{
static void Main()
{
A sc = new A();
sc.x = 110;
sc.y = 150;
Console.WriteLine("x = {0}, y = {1}", sc.x, sc.y);
}
}
Output
x = 110, y = 150
8 : Find out error code and correct it. Print appropriate output as desired.
Solution:
class X
{
protected virtual void F() { Console.WriteLine("X.F"); }
protected virtual void F2() { Console.WriteLine("X.F2"); }
}
class Y : X
{
sealed protected override void F() { Console.WriteLine("Y.F"); }
protected override void F2() { Console.WriteLine("Y.F2"); }
}
class Z : Y
{
// Overriding F
protected override void F() { Console.WriteLine("Z.F"); }
// Overriding F2
protected override void F2() { Console.WriteLine("Z.F2"); }
}
class SealedMethodTest
{
static void Main()
{
X Obj1 = new X();
Obj1.F();
Obj2.F2();
Y Obj2 = new Y();
Obj2.F();
Obj2.F2();
Z Obj3 = new Z();
Obj3.F();
Obj3.F2();
}
}
9 : This program will throw an exception. Add try, catch and finally blocks to handle this exception.
using System;
class MyClient
{
public static void Main()
{
int x = 0;
int div = 100/x;
Console.WriteLine(div);
}
}
10: Arrange the code to get desirable output
class MyException : Exception
{
}
class MyClient
{
public static void Main()
{
try
{
throw new MyException("my exception generated.");
}
catch(Exception e)
{
}
Console.WriteLine("LAST STATEMENT");
}
using System;
public MyException(string str)
{
Console.WriteLine("User defined exception");
Console.WriteLine("Exception caught here: " + e.ToString());
}
}
Output:
Exception caught here: my exception generated.
LAST STATEMENT
Find Rubric