146-2022 Riya Vechiot - Assignment1-Console Application C#

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 9

Name: Riya Premarajan Vechiot Roll no: 146

Assignment - 1 Basics of C#

1. Write a program to create a class department which has department


information and also create an employee class which inherits the
department and displays all the information.

Aim:To create a class department and display its information.

Objective:To understand the functionality of class and inheritance

Theory :
A class is a data structure in C# that combines data variables and functions into a single unit.
Instances of the class are known as objects. While a class is just a blueprint, the object is an
actual instantiation of the class and contains data.
n C#, inheritance is a process in which one object acquires all the properties and behaviors of
its parent object automatically. In such way, you can reuse, extend or modify the attributes and
behaviors which is defined in other class.
In C#, the class which inherits the members of another class is called derived class and the
class whose members are inherited is called base class.
Syntax: class DerivedClass : BaseClass
Code :

Department.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace project_146
{
class Department
{
public void printDept()
{
Console.WriteLine("Welcome to the department");
}

}
}
Employee.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace project_146
{
class Employee : Department
{
public void getEmp(string name)
{
Console.WriteLine("Employee name is Riya");
Console.WriteLine("Roll no is 146");
}

}
}

Program.cs

using System;
using System.Collections.Generic;
using System.Diagnostics.Metrics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml.Linq;

namespace project_146
{
internal class Program
{
static void Main(String[] args)
{
Employee emp = new Employee();
String empname;
Console.WriteLine("Enter the name for employee") ;
empname = Convert.ToString(Console.ReadLine());
emp.getEmp(empname);
emp.printDept();
}
}
}

Output :

2. Write a program to create an abstract class named plan which has an abstract
method that sets rates of plan and also a method which calculates total bill by
taking a number of seats for different plans. Create two different plans domestic
and international which have different rates.

Aim:To write a program to create and implement abstract class

Objective : Create an abstract class for hiding some data

Theory : Abstract classes are the way to achieve abstraction in C#. Abstraction in C# is the
process to hide the internal details and showing functionality only.
Abstract class can have abstract and non-abstract methods. It cannot be instantiated. Its
implementation must be provided by derived classes.
Syntax: abstract class classname{
//Class body
}

Code :

Plane.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace AWT146
{
abstract class Plane
{
public abstract void getRate();
protected double rate;
public void calculate(int seats)
{
Console.WriteLine("Bill amount for " + seats + " seats is Rs " + rate);
Console.WriteLine(rate * seats);
}
}
}

International.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace AWT146
{
internal class International:Plane
{
public override void getRate()
{
Console.WriteLine("Enter the rate: ");
rate = Convert.ToDouble(Console.ReadLine());
}
}
}

Domestic.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace AWT146
{
internal class Domestic:Plane
{
public override void getRate()
{
Console.WriteLine("Enter the rate: ");
rate = Convert.ToDouble(Console.ReadLine());
}
}
}

Program.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace AWT146
{
internal class Program
{
static void Main(string[] args)
{
Console.WriteLine("Riya Vechiot - 146");
Console.WriteLine("International Connection");
International i = new International();
i.getRate();
i.calculate(3);
Console.WriteLine("Domestic Connection");
Domestic d = new Domestic();
d.getRate();
d.calculate(3);
}
}
}
Output :

3. Write a program to create an interface iControl which has two


properties Height and Width. Create a class ListBox and CheckBox to
implement the interface

Aim:To write a program to create interface and then implement it


Objective : Create an interface iControl which has two properties and
implement the interface

Theory : Interface in C# is a blueprint of a class. It is like abstract class because all the methods
which are declared inside the interface are abstract methods. It cannot have method body and
cannot be instantiated
Syntax: interface InterfaceName { }.

Code :

IControl.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Program2
{
interface iControl
{
int height
{
get;
set;
}
int width
{
get;
set;
}
}
}

CheckBox.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Program2
{
internal class CheckBox
{
int h, w;
public int height
{
get
{
return h;
}
set
{
h = value;
}
}
public int width
{
get
{
return w;
}
set
{
w = value;
}
}
}
}
}

ListBox.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Program2
{
internal class ListBox:iControl
{
int h, w;
public int height
{
get
{
return h;
}
set
{
h = value;
}
}
public int width
{
get
{
return w;
}
set
{
w = value;
}
}
}
}
Program.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Program2
{
internal class Program
{
static void Main(string[] args)
{
Console.WriteLine("Riya Vechiot - 146");
int lh, lw, ch, cw;
ListBox l = new ListBox();
CheckBox c = new CheckBox();
lh = Convert.ToInt32(Console.ReadLine());
lw = Convert.ToInt32(Console.ReadLine());
ch = Convert.ToInt32(Console.ReadLine());
cw = Convert.ToInt32(Console.ReadLine());
l.height = lh;
l.width = lw;
c.height = ch;
c.width = cw;
Console.WriteLine("Height of listbox is: "+l.height);
Console.WriteLine("Width of listbox is: " + l.width);
Console.WriteLine("Height of checkbox is: " + c.height);
Console.WriteLine("Width of checkbox is: " + c.width);
}
}
}

Output :

You might also like