0% found this document useful (0 votes)
40 views11 pages

Lab NO: 7: Karachi Campus

The document discusses using object oriented programming in C# through 4 tasks. Task 1 demonstrates creating a Square class with a parameterized constructor and set/get properties. Task 2 creates a Bank class to store account details and allows depositing and withdrawing from an account instance. Task 3 creates a Car class with properties like color and speed, and methods to accelerate and check if moving. Task 4 creates a Calculator class to demonstrate addition of two numbers entered by the user.

Uploaded by

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

Lab NO: 7: Karachi Campus

The document discusses using object oriented programming in C# through 4 tasks. Task 1 demonstrates creating a Square class with a parameterized constructor and set/get properties. Task 2 creates a Bank class to store account details and allows depositing and withdrawing from an account instance. Task 3 creates a Car class with properties like color and speed, and methods to accelerate and check if moving. Task 4 creates a Calculator class to demonstrate addition of two numbers entered by the user.

Uploaded by

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

BAHRIA UNIVERSITY Lab

KARACHI CAMPUS
DEPARTMENT OF COMPUTER SCIENCES
NO: 7

Using Object Oriented Programming in C#


CSL-313: Visual Programming Lab
Semester BS (CS)– 5 C

Task 1:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ex1
{

//REmove comment for parameterised constructor


//class Program
//{
// static void Main(string[] args)
// {
// Square square = new Square(25);
// //square.setget = Convert.ToDouble(Console.ReadLine());
// Console.WriteLine(square.setget*square.setget);
// }
//}

class Program
{
static void Main(string[] args)
{
Square square = new Square();
square.setget = Convert.ToDouble(Console.ReadLine());
Console.WriteLine(square.setget * square.setget);
}
}
}

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

namespace ex1
{
public class Square
{
private double _size;

Lab 07: Using Object Oriented Programming in C# Page 39


public Square()
{ }
public Square(float _size)
{
this._size = _size;
}

public double setget


{
set
{
_size = value;
}

get
{
return _size;
}
}
}
}

Task 2:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace ex2
{
public partial class Form1 : Form
{
bank member = new bank();
public Form1()
{
InitializeComponent();
}

private void Form1_Load(object sender, EventArgs e)


{

private void button1_Click(object sender, EventArgs e)


{
member.SetGetaccNumber = Convert.ToInt32(textBox1.Text);
member.SetGetname = textBox2.Text;

Lab 07: Using Object Oriented Programming in C# Page 40


member.SetGetopeningBalance = Convert.ToDouble(textBox3.Text);
textBox5.Text = member.SetGetopeningBalance.ToString();
label1.Text = "Hello Mr." + member.SetGetname;
textBox1.Enabled = false;
textBox2.Enabled = false;
textBox3.Enabled = false;
button1.Enabled = false;
textBox4.Enabled = true;
button2.Enabled = true;
button3.Enabled = true;
}

private void label1_Click(object sender, EventArgs e)


{

private void button2_Click(object sender, EventArgs e)


{
textBox5.Text = (member.SetGetopeningBalance +
Convert.ToDouble(textBox4.Text)).ToString();
textBox4.Text = "0";
}

private void button3_Click(object sender, EventArgs e)


{
textBox5.Text = (member.SetGetopeningBalance -
Convert.ToDouble(textBox4.Text)).ToString();
textBox4.Text = "0";
}
}
}

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

namespace ex2
{
class bank
{
private int _accNumber;
private string _name;
private double _openingBalance;

public bank()
{ }

public int SetGetaccNumber


{
get
{
return _accNumber;
}

set
{
_accNumber = value;

Lab 07: Using Object Oriented Programming in C# Page 41


}
}

public string SetGetname


{
get
{
return _name;
}

set
{
_name = value;
}
}

public double SetGetopeningBalance


{
get
{
return _openingBalance;
}

set
{
_openingBalance = value;
}
}

}
}

Task 3:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

Lab 07: Using Object Oriented Programming in C# Page 42


using System.Threading.Tasks;
using ex2;
namespace ex3
{
class Program
{
static void Main(string[] args)
{
car a = new car();
car b = new car("Black");
car c = new car(4, 2,6,4,"Green");

Console.WriteLine(b.GetSetColor);

Console.WriteLine(c.GetSetgear);
Console.WriteLine(c.GetSetColor);
Console.WriteLine(c.GetSetHPower);
Console.WriteLine(c.GetSetSpeed);
Console.WriteLine(c.GetSetNumberOfDoors);

c.GetSetColor = "gray";
c.GetSetSpeed = 2;
c.GetSetNumberOfDoors = 2;
c.GetSetgear=5;
c.GetSetHPower = 104;

Console.WriteLine(c.GetSetgear);
Console.WriteLine(c.GetSetColor);
Console.WriteLine(c.GetSetHPower);
Console.WriteLine(c.GetSetSpeed+" Speed");
Console.WriteLine(c.GetSetNumberOfDoors);

c.Accelerate();
Console.WriteLine(c.GetSetSpeed);
c.Accelerate(5);
Console.WriteLine(c.GetSetSpeed);
c.GetSetSpeed = 0;
Console.WriteLine(c.GetSetSpeed);
bool check=c.IsMoving();
if (check==true)
Console.WriteLine("Movilg");
else
Console.WriteLine("stopped");

c.Accelerate();
check = c.IsMoving();
if (check == true)
Console.WriteLine("Movilg");
else
Console.WriteLine("stopped");

}
}
}

using System;
using System.Collections.Generic;
using System.Linq;

Lab 07: Using Object Oriented Programming in C# Page 43


using System.Text;
using System.Threading.Tasks;

namespace ex2
{
class car
{
const int engineNumner = 55640;
private double speed, hpower;
private string color;
private int numberOfDoors, gear;

public car()
{

}
public car(string color)
{
this.color = color;
}

public car(int gear, double speed)


{
this.gear = gear;
this.speed = speed;
}

public car(int gear,int numberOfDoors, double speed, double hpower, string


color)
{
this.gear = gear;
this.speed = speed;
this.numberOfDoors = numberOfDoors;
this.hpower = hpower;
this.color = color;
}

public double GetSetSpeed


{
set
{
speed = value;
}

get
{
return speed;
}
}
public double GetSetHPower
{
set
{
hpower = value;
}
get
{
return hpower;
}

Lab 07: Using Object Oriented Programming in C# Page 44


}

public string GetSetColor


{
set
{
color = value;
}
get
{
return color;
}
}

public int GetSetNumberOfDoors


{
set
{
numberOfDoors = value;
}
get
{
return numberOfDoors;
}
}

public int GetSetgear


{
set
{
gear = value;
}
get
{
return gear;
}
}

public void Accelerate()


{
GetSetSpeed++;
}

public void Accelerate(int inc)


{
GetSetSpeed += inc;
}

public bool IsMoving()


{
if (speed > 0)
return true;
else
return false;
}

Lab 07: Using Object Oriented Programming in C# Page 45


}
}

Task 4:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace ex2
{
public partial class task4 : Form
{
calculator calc = new calculator();
public task4()
{
InitializeComponent();
}

private void task4_Load(object sender, EventArgs e)


{

private void button1_Click(object sender, EventArgs e)


{
calc.SetGetNum1 = Convert.ToDouble(textBox1.Text);
calc.SetGetNum2 = Convert.ToDouble(textBox2.Text);
textBox3.Text = calc.add().ToString();
}

private void button2_Click(object sender, EventArgs e)


{
calc.SetGetNum1 = Convert.ToDouble(textBox1.Text);
calc.SetGetNum2 = Convert.ToDouble(textBox2.Text);
textBox3.Text = calc.sub().ToString();
}

Lab 07: Using Object Oriented Programming in C# Page 46


private void button3_Click(object sender, EventArgs e)
{
calc.SetGetNum1 = Convert.ToDouble(textBox1.Text);
calc.SetGetNum2 = Convert.ToDouble(textBox2.Text);
textBox3.Text = calc.Multiply().ToString();
}

private void button4_Click(object sender, EventArgs e)


{
calc.SetGetNum1 = Convert.ToDouble(textBox1.Text);
calc.SetGetNum2 = Convert.ToDouble(textBox2.Text);
textBox3.Text = calc.divide().ToString();
}
}
}

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

namespace ex2
{
class calculator
{

private double num1, num2;

public calculator()
{

public double SetGetNum1


{
set
{
num1 = value;
}
get
{
return num1;
}
}

public double SetGetNum2


{
set
{
num2 = value;
}
get
{
return num2;
}
}

public double add()

Lab 07: Using Object Oriented Programming in C# Page 47


{
return num1 + num2;
}
public double sub()
{
return num1 - num2;
}
public double Multiply()
{
return num1 * num2;
}
public double divide()
{
return num1 / num2;
}
}
}

Lab 07: Using Object Oriented Programming in C# Page 48

You might also like