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

Assignment No 2(Visual Programming)

The document contains multiple programming assignments related to object-oriented concepts in C#. It includes implementations for student grade classification, bank account transactions, employee salary calculations, shape area calculations, method overloading and overriding, and preventing inheritance using sealed classes. Each assignment provides a class structure, methods, and examples demonstrating the respective concepts.

Uploaded by

Fareeha Butt
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)
4 views

Assignment No 2(Visual Programming)

The document contains multiple programming assignments related to object-oriented concepts in C#. It includes implementations for student grade classification, bank account transactions, employee salary calculations, shape area calculations, method overloading and overriding, and preventing inheritance using sealed classes. Each assignment provides a class structure, methods, and examples demonstrating the respective concepts.

Uploaded by

Fareeha Butt
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/ 10

Name: Danish Masoom

Roll No: 21101003-012


Department: BS(IT) Blue
Subject: Visual Programming
Assignment No: 2
Submitted to: Mam Hina Farooq
Student Grades Classification
Create a Student class with properties Name, RollNumber, and Marks.
Implement a method GetGrade() that returns the grade based on marks:
90 and above: A
80-89: B
70-79: C
Below 70: Fail
In the Main() method, create multiple student objects and display their grades.
Answer:
using System;
class Student
{
public string Name;
public int RollNumber;
public int Marks;
public Student(string name, int roll, int marks)
{
Name = name;
RollNumber = roll;
Marks = marks;
}
public string GetGrade()
{
if (Marks >= 90)
return "A";
else if (Marks >= 80)
return "B";
else if (Marks >= 70)
return "C";
else
return "Fail";
}
}
class Program
{
static void Main()
{
Student s1 = new Student("Danish", 11, 88);
Student s2 = new Student("Abdullah", 92, 77);
Student s3 = new Student("Hamza", 34, 50);
Console.WriteLine(" Name: " + s1.Name + ", Roll No: " + s1.RollNumber + ", Grade: " +
s1.GetGrade());
Console.WriteLine(" Name: " + s2.Name + ", Roll No: " + s2.RollNumber + ", Grade: " +
s2.GetGrade());
Console.WriteLine(" Name: " + s3.Name + ", Roll No: " + s3.RollNumber +", Grade: " +
s3.GetGrade());
}
}
Output:

Bank Account Transactions


Create a base class BankAccount with properties AccountNumber, Balance, and methods Deposit()
and Withdraw(). Create a derived class SavingsAccount that adds an interest rate property and a
method ApplyInterest(). Ensure that withdrawal does not allow negative balance. Demonstrate
depositing, withdrawing, and applying interest using objects.
Answer:
using System;
class BankAccount
{
public int AccountNumber;
public double Balance;
public BankAccount(int acc, double bal)
{
AccountNumber = acc;
Balance = bal;
}
public void Deposit(double amount)
{
Balance += amount;
Console.WriteLine("Deposit amount:" + amount);
}
public void Withdraw(double amount)
{
if (Balance >= amount)
{
Balance -= amount;
Console.WriteLine("Withdraw amount:" + amount);
}
else
{
Console.WriteLine("Insufficient balance!");
}
}
}
class SavingsAccount : BankAccount
{
public double InterestRate;
public SavingsAccount(int acc, double bal, double rate) : base(acc, bal)
{
InterestRate = rate;
}
public void ApplyInterest()
{
double interest = Balance * InterestRate / 100;
Balance += interest;
}
}
class Program
{
static void Main()
{
SavingsAccount acc = new SavingsAccount(777888, 8000, 5);
acc.Deposit(4000);
acc.Withdraw(6000);
acc.ApplyInterest();
Console.WriteLine("Final Balance: " + acc.Balance);
}
}
Output:

Employee Salary Calculation


Implement a base class Employee with properties Name and BasicSalary. Create a derived class
Manager that adds a Bonus property and overrides a method CalculateSalary() to include bonus. Also,
create a Developer class that includes a ProjectAllowance and overrides CalculateSalary(). Use
polymorphism to display the salary details of both employee types.
Answer:
using System;
class Employee
{
public string Name;
public double BasicSalary;
public Employee(string name, double salary)
{
Name = name;
BasicSalary = salary;
}
public virtual double CalculateSalary()
{
return BasicSalary;
}
}
class Manager : Employee
{
public double Bonus;
public Manager(string name, double salary, double bonus) : base(name, salary)
{
Bonus = bonus;
}
public override double CalculateSalary()
{
return BasicSalary + Bonus;
}
}
class Developer : Employee
{
public double ProjectAllowance;
public Developer(string name, double salary, double allowance) : base(name, salary)
{
ProjectAllowance = allowance;
}
public override double CalculateSalary()
{
return BasicSalary + ProjectAllowance;
}
}
class Program
{
static void Main()
{
Employee emp1 = new Manager("Danish", 70000, 20000);
Employee emp2 = new Developer("Abdullah", 60000, 10000);
Console.WriteLine(emp1.Name + " Salary: " + emp1.CalculateSalary());
Console.WriteLine(emp2.Name + " Salary: " + emp2.CalculateSalary());
}
}
Output:

Shape Area Calculation


Using Abstract Class Create an abstract class Shape with an abstract method CalculateArea(). Derive
two classes, Rectangle and Circle, implementing the CalculateArea() method. The Rectangle class
should take Length and Width, and the Circle class should take Radius. Demonstrate polymorphism by
calling CalculateArea() through a Shape reference.
Answer:
using System;
abstract class Shape
{
public abstract double CalculateArea();
}
class Rectangle : Shape
{
public double Length, Width;
public Rectangle(double l, double w)
{
Length = l;
Width = w;
}
public override double CalculateArea()
{
return Length * Width;
}
}
class Circle : Shape
{
public double Radius;
public Circle(double r)
{
Radius = r;
}
public override double CalculateArea()
{
return 3.14 * Radius * Radius;
}
}
class Program
{
static void Main()
{
Shape s1 = new Rectangle(9, 5);
Shape s2 = new Circle(6);
Console.WriteLine("Rectangle Area: " + s1.CalculateArea());
Console.WriteLine("Circle Area: " + s2.CalculateArea());
}
}
Output:

Method Overloading and Overriding in Calculator


Create a class Calculator with overloaded methods Compute() to:
Add two integers
Multiply two doubles
Concatenate two strings
Now, create a derived class ScientificCalculator that overrides Compute() to calculate the power of a
number (a^b). Demonstrate early binding (overloading) and late binding (overriding).
Answer:
using System;
class Calculator
{
public void Compute(int a, int b)
{
Console.WriteLine("Sum: " + (a + b));
}
public void Compute(double a, double b)
{
Console.WriteLine("Product: " + (a * b));
}
public void Compute(string a, string b)
{
Console.WriteLine("Concatenation: " + a + b);
}
public virtual void Compute(double a, int b)
{
Console.WriteLine("Base Compute");
}
}
class ScientificCalculator : Calculator
{
public override void Compute(double a, int b)
{
Console.WriteLine("Power: " + Math.Pow(a, b));
}
}
class Program
{
static void Main()
{
Calculator calc = new Calculator();
calc.Compute(2, 3);
calc.Compute(8.2, 4.5);
calc.Compute("Hello ", "USKT");
Calculator sciCalc = new ScientificCalculator();
sciCalc.Compute(2.0, 3); // overridden
}
}

Output:

Preventing Inheritance using Sealed Class


Create a class Vehicle with properties Make, Model, and a method Start(). Create a derived class Car
that overrides Start(). Now, create a sealed class ElectricCar that extends Car and implements
ChargeBattery(). Try to create a derived class from ElectricCar and observe the compilation error.
Answer:
using System;
class Vehicle
{
public string Make, Model;
public Vehicle(string make, string model)
{
Make = make;
Model = model;
}
public virtual void Start()
{
Console.WriteLine("Vehicle started.");
}
}
class Car : Vehicle
{
public Car(string make, string model) : base(make, model) { }
public override void Start()
{
Console.WriteLine("Car started.");
}
}
sealed class ElectricCar : Car
{
public ElectricCar(string make, string model) : base(make, model) { }
public void ChargeBattery()
{
Console.WriteLine("Battery charging...");
}
}
class Program
{
static void Main()
{
ElectricCar ec = new ElectricCar("Tesla", "Model 3");
ec.Start();
ec.ChargeBattery();
}
}
Output:

You might also like