0% found this document useful (0 votes)
11 views27 pages

Assignment

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views27 pages

Assignment

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 27

Ch 3 Classes and Object

Classes and Object –

Create Console application to do this Assignment.

1) Write a class. Name it as “Program” class. Follow below steps


a) Define variables (inside a class, not inside a Main () method) – Id-int, name-String, IsActive –
bool, salary – double, gender – char(M/F).
b) Create an object of this class in Main () method.
c) Access above variables using an object and assign values to them as per the given data type.
d) Print each variable value with some meaning full message.

2) Write a class. Name it as “Student” class. Follow below steps


e) Define variables (inside a Student class) – StudentId-int, StudentName-String, IsActive –
bool, Marks – double, gender – char(M/F).
f) Create 4 objects of this class in Main ()(Inside program class) method.
g) Access above variables using each object and assign unique values to each object and it’s
variable as per the given data type.
h) Print each object variable value with some meaning full message.
i) Refer below visualization for this assignment.
Default values-

1) Write a class. Name it as “Program” class. Follow below steps


a) Define variables (inside a class, not inside a Main () method) – Id-int, name-String, IsActive –
bool, salary – double, gender – char(M/F).
b) Create an object of this class in Main () method.
c) Access above variables using an object and do assign values to them.
d) Print each variable value with some meaning full message.
e) Refer below visualization for this assignment.
f) Define all variables for below data types.

Methods –
1) Write a class. Name it as “Program” class. Follow below steps.
a) Write below methods
Public Void Print() – it will print some message.
Public int Add(int a, int b) – it should add 2 values and return sum.
Public int Sub(int a, int b) – it should subs 2 values and return subs.
Public int Div(int a, int b) – it should divide 2 values and return div.
Public int Mult(int a, int b) – it should Mult 2 values and return Mult.
b) Create an object of the Program class in the Main() method.
c) Call above method using an object and print an output for each method with some meaning
full message.

Access Modifiers-
1) Write a class. Name it as “Student” class. Follow below steps.
2) Define below variables
Public int I=1;
Private int j=1;
Protected int k =1;
3) Create an object of the “Student” class in the Program class in Main() method
4) Access below variables using an object and print an output. Also observer behavior of the
variable access. Notify and analyze if any error occurs.

Properties-

1) Write a class. Name it as “Student” class. Follow below steps.


a) Define below properties
Public int ID – get access
Private string name - set access
Protected double Marks - get and set access
b) Create an object of the “Student” class in the Program class in Main() method
c) Access properties using an object and assign value to get,set and get/set properties.
d) Try to set value to get properties and observer issue.
e) Try to get – read/print value from set property and observer issue.
f) Read/Write Marks property.
Method Overloading-

1) Write A “Maths” with below some overloaded method


a) Write below method
Public int Add(int a, int b) – it should add 2 values and return sum.
Public int Add(int a, int b, int c) – it should add 3 values and return sum.
Public int Add(int a, int b, int c , int d) – it should add 4 values and return sum.

Public int Sub(int a, int b) – it should subs 2 values and return subs.
Public int Sub(int a, int b, int c) – it should subs 3 values and return subs.
Public int Sub(int a, int b, int c, int d) – it should subs 4 values and return subs.

Public int Div(int a, int b) – it should divide 2 values and return div.
Public int Div(int a, int b, int c) – it should divide 3 values and return div.
Public int Div(int a, int b, int c, int d) – it should divide 4 values and return div.

Public int Mult(int a, int b) – it should Mult 2 values and return Mult.
Public int Mult(int a, int b, int c) – it should Mult 3 values and return Mult.
Public int Mult(int a, int b, int c, int d) – it should Mult 4 values and return Mult.
d) Create an object of the “Maths” class in Program class in the Main() method.
e) Call above method using an object and print an output for each method with some meaning
full message.

Constructor Overloading-

1) Write a class. Name it as “Program” class. Follow below steps.


a) Define default constructor (without parameter) and inside it prints some message.
b) Create an object of the “Program” class and debug the program and write observation.
2) Write a class. Name it as “Program” class. Follow below steps.
c) Define default constructor (without parameter) and inside it prints some message.
d) Define Parameterized constructor. Provide string parameter as a name and inside it prints
name.
e) Define Parameterized constructor. Provide string parameter as a name, Id parameter as int
and inside it prints Name and Id.
f) Create an object of the “Program” class for default object, parameterized object with string
parameter and parameterized object with 2 param – string and int. and debug the program
and write observation.

------------------------------Oct Batch – date of compl – 1june 24 Evening --------------


------------------------------
CH4 – Inheritance and Polymorphism

Inheritance –

Single Inheritance -

1) Write a class. Name it as “Program” class. Follow below steps


a) Define a class named “BaseClass”. Define variables (inside a class) – Id-int, name-String,
IsActive – bool, salary – double, gender – char(M/F).
b) Inherit “BaseClass” into Program class.
c) Create an object of the Program class and access id,name,isactiveand salry, gender
variables. Assign some values and print these variable values.

Multi-Level Inheritance -

2) Write a class. Name it as “Program” class. Follow below steps


a) Define a class named “StudentMaster”. Define variables (inside a class) – Id-int, name-
String, IsActive – bool, marks – double, gender – char(M/F).
b) Define a class named “SubjectMaster”. Define variables (inside a class SubjectMaster) –
SubjectId-int, SubjectName-String, IsActive – bool, gender – char(M/F).
c) Inherit “StudentMaster” into SubjectMaster class.
d) Inherit “SubjectMaster” into “Program” class.
e) Create an object of the “Program” class and access both classes’ members. Assign some
values and print these variable values.

Hierarchical Inheritance -

3) Write a class. Name it as “Program” class. Follow below steps


a) Define a class named “Shape”. Define variables (inside a class) – x-axis - int, y-axis.
b) Define a method named int Draw (int x, int y) inside “Shape” class.
c) Define a class named “Circle”.
d) Define a class named “Rectangular”.
e) Define a class named “Diamond”.
f) Inherit Shape class into Circle, Rectangular and Diamond class.
g) Create an object of Circle class inside program (main method).. Access x-axis and y-axis
variables and assign values and call Draw method and pass x-axis and y-axis variables to it.
h) Create an object of rectangular class inside program (main method).. Access x-axis and y-
axis variables and assign values and call Draw method and pass x-axis and y-axis variables to
it.
i) Create an object of Diamond class inside program (main method). Access x-axis and y-axis
variables and assign values and call Draw method and pass x-axis and y-axis variables to it.

Constructor calls in Inheritance -

1) Write a class. Name it as “Program” class. Follow below steps


a) Define class Named BaseClass.
b) Define default constructor in the BaseClass and print some message inside constructor.
c) Inherit BaseClass into Program class
d) Define default constructor in Program class and print some message inside constructor.
e) Create an object of Program class in the main method
f) Execute program and observe an ouput and debug the program and write execution.
g) Create an object of the BaseClass and observe an ouput and debug the program and write
execution.

2) Write a class. Name it as “Program” class. Follow below steps


a) Define class Named BaseClass.
b) Define default constructor in the BaseClass and print some message inside constructor.
c) Define parameterized (int param) constructor in the BaseClass and print some message with
parameter value inside constructor.
d) Inherit BaseClass into Program class
e) Define default constructor in Program class and print some message inside constructor.
f) Define parameterized (int param) constructor in the Program and print some message with
parameter value inside constructor.
g) Create an object of Program class in the main method.
h) Create an object of Program class in the main method and pass int parameter.
i) Execute program and observe an ouput and debug the program and write execution.
j) Create an object of the BaseClass and observe an ouput and debug the program and write
execution.
k) Create an object of the BaseClass and pass int parameter and observe an ouput and debug
the program and write execution.

3) Write and program where define BaseClass and inherit this in the program class. Define default
parameterized constructor in the BaseClass. In the Program class use “base” keyword to call
base class default and parameterized constructor. Create an object of the program class in main
method and write execution and output.
4) Write a program as given in the below screen shot. Compil,run and debug the program and
write an output.

5) Write BaseClass with sealed keyword and inherit this in the program class. Compile the program
and write an output.
6) What is an object class? Write methods provided by it.
7) Create an array of object class with size 5. Add diff datatype of values in the array and print
values from the created array.
8) Define 1 Base class and inherit this in the program class. Define Public Void Draw() method in
the Base and program class with same definition. Write warning message in the assignment file.
9) Define 1 Base class and inherit this in the program class. Define Public Void Draw() method in
the Base and program class with same definition only use “new” keyword in the program class
method. Compile the program and check if any warning show there.
10) Write a program as given below. Compile and debug the program and write output and
obeservatoins.

11) Write a program as given below in the screen shot. Write some meaning full message in every
method method/function given below. Write and output. Also debug the program and write
observations.
12) What is method overriding? And how to do it. Explain with an example.
13) What is virtual and override keyword?
14) Write below program. Compile and run the program and write output. Also debug the program
write obeservations.
15) Write a program as given in the below screen shot.

16) What is boxing and unboxing? Explain it with example.


17) Write a program as given in the below image. It’s determine the up and down casting. Compile,
debug and run the program and write observations and output.
------------------------------June-Aug Batch – date of compl – 24th Aug Evening ----------
----------------------------------

CH6 – Abstract Class


Abstract class –

Description -
1) abstract class defined with abstract keyword
2) cannot create an instance of abstract class
3) Must inherit to sub call to use functionality
4) abstract class can have abstract as well as not abstract members(prop/method)
5) abstract method/prop must override in derived class
6) abstract method/prod does not have a body in abstract class
7) syntax
8) example
9) cannot inherit multiple classed in one class
10) can be used access modifiers
11) can be used static method/member
12) static member can be defined in the abstract class
13) cannot define static abstract method
14) abstract class cannot be sealed.
Questions

1) Write a below program which describe an abstract class.

2) Define an Abstract class named AbsBaseClass. Inherit this in the program class. In the main
method create an object of the abstract class. Compile and run the program and write an
output.?
3) Define an Abstract class named AbsBaseClass1. Inherit this in the program class. In the
AbsBaseClass1 class define abstract method: AbsMethod(). Compile and run the program and
write an output.?
4) Define an Abstract class named AbsBaseClass2. Inherit this in the program class. In the
AbsBaseClass2 class define instance(normal) method: InsMethod(). Compile and run the
program and write an output.?
5) Define an Abstract class named AbsBaseClass3. Inherit this in the program class. In the
AbsBaseClass3 class define abstract property: string Name. Compile and run the program and
write an output.?
6) Define an Abstract class named AbsBaseClass4. Inherit this in the program class. In the
AbsBaseClass4 class define instance property: string Name. Compile and run the program and
write an output.?
7) Define an Abstract class named AbsBaseClass5. Inherit this in the program class. In the
AbsBaseClass4 class define abstract method: AbsMethodOver() and one normal/instance
method named: NormalMethod(). Override AbsMethodOver() method in the Program class.
Create an object of the program class and call AbsMethodOver() and NormalMethod() method.
Compile and run the program and write an output.?
8) Define an abstract class with sealed keyword and compile and write an output?
9) Define an abstract class with static keyword and compile and write an output?
10) Define an abstract class with abstract static method named: AbsStaticMethod() and compile and
write an output?
11) Define an abstract class with abstract method named: AbsMethod() and define method body
as well and compile and write an output?
12) Below is the given program with some business logic. Write a below program in c# and compile,
debug and run the program. Write an output and also write detailed explanation.

abstract class OfferLetter


{
public abstract string Name(string name);
public abstract string Designation(string
designation);
public abstract int Payment(int pay);
public abstract string DOJ(string doj);
public string CompanyName()
{
return "XYZ Corporation Pvt. Ltd.";
}
public string OfficeAddress()
{
return "512, Manhattan, NY";
}
public string CompanyBranding()
{
return this.CompanyName() + " is a privately owned
regsitered corporation operating \n under the license
provided by the state of New York.";
}
public string Disclaimer()
{
return "This letter and its contents are confidential
in nature and are intended only \n for the
recipient."+
"\nIf you are not the correct recipient, kindly
return it immediately \n to " + this.CompanyName() +
" " + this.OfficeAddress() + ".";
}
}
class PrintOfferLetter : OfferLetter
{
public override string Name(string name)
{
return name;
}
public override string Designation(string
designation)
{
return designation;
}
public override int Payment(int pay)
{
return pay;
}
public override string DOJ(string doj)
{
return doj;
}
}
public class Program
{
public static void Main()
{
PrintOfferLetter ltr = new PrintOfferLetter();
string empName = "Mr. ABC", designation = "Senior
Consultant", doj = "20-02-2020";
int pay = 50000;
Console.WriteLine(ltr.CompanyName() + " is very happy
to extend this offer letter to \n" +
ltr.Name(empName)
+ " at the designation of " +
ltr.Designation(designation) + " with an annual pay
of " + ltr.Payment(pay) + "$.");
Console.WriteLine("\nYou are required to report at "
+ ltr.OfficeAddress() + " from " + ltr.DOJ(doj) + "
(dd-mm-yyyy).");
Console.WriteLine("\n\n" + ltr.CompanyBranding());
Console.WriteLine("\n\n" + ltr.Disclaimer());
}
}

13) Write a below program in c#. It demonstrates inheritance in Abstract. Compile,debug and write
an output and detailed description.

using System;
namespace AbstractClass {

abstract class Language {

// non-abstract method
public void display() {
Console.WriteLine("Non abstract method");
}
}

// inheriting from abstract class


class Program : Language {

static void Main (string [] args) {

// object of Program class


Program obj = new Program ();
// access method of an abstract class
obj.display();

Console.ReadLine();
}
}
}
-------------------------------------------------------------------------------------------
14) Write below program in c#, debug, compile and run the program. It’s Implementation of the
abstract method. Write an output and explanation?

using System;

namespace AbstractClass {

abstract class Animal {

// abstract method

public abstract void makeSound();

// inheriting from abstract class

class Dog : Animal {

// provide implementation of abstract method

public override void makeSound() {

Console.WriteLine("Bark Bark");

class Program {

static void Main (string [] args) {

// create an object of Dog class


Dog obj = new Dog();

obj.makeSound();

Console.ReadLine();

-------------------------------------------------------------------------------------------

15) Define Abstract class with get and set accessors? write below program. Compile,debug and run,

and explain the execution ?

using System;

namespace AbstractClass {

abstract class Animal {

protected string name;

// abstract method

public abstract string Name {

get;

set;

// inheriting from abstract class

class Dog : Animal {

// provide implementation of abstract method

public override string Name {


get {

return name;

set {

name = value;

class Program {

static void Main (string [] args) {

// create an object of Dog class

Dog obj = new Dog();

obj.Name = "Tom";

Console.WriteLine("Name: " + obj.Name);

Console.ReadLine();

16) Define an abstract class to demonstrate constructor. Debug, compile and run the program, write
an output and write and explanation.
using System;
namespace AbstractClass {
abstract class Animal {

public Animal() {
Console.WriteLine("Animal Constructor");
}
}

class Dog : Animal {


public Dog() {
Console.WriteLine("Dog Constructor");
}
}

class Program {
static void Main (string [] args) {
// create an object of Dog class
Dog d1 = new Dog();

Console.ReadLine();
}
}
}

C# Abstraction
The abstract classes are used to achieve abstraction in C#.

Abstraction is one of the important concepts of object-oriented programming.


It allows us to hide unnecessary details and only show the needed
information.

This helps us to manage complexity by hiding details with a simpler, higher-


level idea.

A practical example of abstraction can be motorbike brakes. We know what a


brake does. When we apply the brake, the motorbike will stop. However, the
working of the brake is kept hidden from us.

The major advantage of hiding the working of the brake is that now the
manufacturer can implement brakes differently for different motorbikes.
However, what brake does will be the same.
17) C# Abstraction – write below program to understand abstraction concept in c# abstract class.

using System;

namespace AbstractClass {
abstract class MotorBike {

public abstract void brake();

class SportsBike : MotorBike {

// provide implementation of abstract method

public override void brake() {

Console.WriteLine("Sports Bike Brake");

class MountainBike : MotorBike {

// provide implementation of abstract method

public override void brake() {

Console.WriteLine("Mountain Bike Brake");

class Program {

static void Main (string [] args) {

// create an object of SportsBike class

SportsBike s1 = new SportsBike();

s1.brake();

// create an object of MountainBike class


MountainBike m1 = new MountainBike();

m1.brake();

Console.ReadLine();

CH6 – Interfaces
1) Interface defined with interface keyword
2) cannot create an instance of Interface
3) Must implements to sub call to use functionality
4) Interface has all members public and abstract bydefault
5) interface method/prop must override in derived class
6) Interface method/prod does not have a body in Interface
7) syntax
8) example
9) Can implement multiple interfaces in one class
10) cannot use access modifiers inside interface
11) cannot static method/member
12) interface to interface connection called Inheritance
13) interface to class connection called Implementation
14) only prop - indexer - method can be defined in the interface

Assign:

1) Write a below program and describe an output.

1. interface Dem
2. {
3. void call ();
4. void show ();
5. }
6. class Drived : Dem
7. {
8. public void call()
9. {
10. Console.WriteLine("You are in Call Method");
11. }
12. public void show()
13. {
14. Console.WriteLine("You are in show Method");
15. }
16. }
17. class Demo
18. {
19. static void Main(string[] args)
20. {
21. Drived dr = new Drived();
22. dr.call();
23. dr.show();
24. Console.ReadLine();
25. }}

2) Write a below program. Define interface with the structure. Describe a an output.

1. interface Dem
2. {
3. void call();
4. void show();
5. }
6. struct Str : Dem
7. {
8. public void call()
9. {
10. Console.WriteLine("Call Method of Strcut");
11. }
12.
13. public void show()
14. {
15. Console.WriteLine("Show Method of Struct");
16. }}
17. class Demo
18. {
19. static void Main(string[] args)
20. {
21. Str st = new Str();
22. st.call();
23. st.show();
24. Console.ReadLine();
25. }}

3) Write a below program using multilevel Inheritance using Interface

1. interface Dem
2. {
3. void call();
4. }

5. interface Dem2: Dem


6. {
7. void show();
8. }

9. Class Str : Dem2


10. {
11. public void call()
12. {
13. Console.WriteLine("Call Method of Strcut");
14. }

15. public void show()


16. {
17. Console.WriteLine("Show Method of Struct");
18. }}
19. class Demo
20. {
21. static void Main(string[] args)
22. {
23. Str st = new Str();
24. st.call();
25. st.show();
26. Console.ReadLine();
27. }}

4) Write a below program using multiple Inheritance using Interface

1. interface Dem
2. {
3. void call();
4. }
5. interface Dem2
6. {
7. void show();
8. }
9. class Str : Dem,Dem2
10. {
11. public void call()
12. {
13. Console.WriteLine("Call Method of Strcut");
14. }
15. public void show()
16. {
17. Console.WriteLine("Show Method of Struct");
18. }}
19. class Demo
20. {
21. static void Main(string[] args)
22. {
23. Str st = new Str();
24. st.call();
25. st.show();
26. Console.ReadLine();
27. }}

5) Write below program using interface With Abstract Class


1. interface Dem
2. {
3. void call();
4. }
5. abstract class Abs : Dem
6. {
7. abstract public void Method();
8. public void call()
9. {
10. Console.WriteLine("Call Method of Dem Interface")
;
11. }
12. }
13. class Str:Abs
14. {
15. public override void Method()
16. {
17. Console.WriteLine("Now I am in Str Class");
18. }
19. public void show()
20. {
21. Console.WriteLine("Show Method of Struct");
22. }}
23. class Demo
24. {
25. static void Main(string[] args)
26. {
27. Str st = new Str();
28. st.call();
29. st.show();
30. st.Method();
31. Console.ReadLine();
32. }}

6) Write a program using interface. Access methods of interfaces using a reference


of a class:

1. interface Intr
2. {
3. void Method1();
4. void Method2();
5. }
6. class Demo2 : Intr
7. {
8. public void Method1()
9. {
10. Console.WriteLine("Method1");
11. }
12. public void Method2()
13. {
14. Console.WriteLine("Method1");
15. }
16. }
17. class Demo
18. {
19. static void Main(string[] args)
20. {
21. Demo2 DmRef = new Demo2();
22. Intr Intrf = DmRef;
23. Intrf.Method1();
24. Intrf.Method2();
25. Console.ReadLine();
26. }}

7) Access a method of an explicit interface using an instance of an interface.

1. interface Intr
2. {
3. void Method1();
4. void Method2();
5. }
6. class Demo2 : Intr
7. {
8. void Intr.Method1()
9. {
10. Console.WriteLine("Successfully Call Method1");
11. }
12. void Intr.Method2()
13. {
14. Console.WriteLine(" Successfully Call Method2");

15. }
16. }
17. class Demo
18. {
19. static void Main(string[] args)
20. {
21. Demo2 DmRef = new Demo2();
22. Intr Itr = DmRef;
23. Itr.Method1();
24. Itr.Method2();
25. Console.ReadLine();
26. }}

Chapter No 7 -Assignment…

You might also like