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

Lab_Assignment lab 3

The document outlines a lab assignment focused on Object-Oriented Programming, requiring the creation of two classes: Numbers and Complex. The Numbers class performs basic arithmetic operations on two user-input numbers, while the Complex class handles addition, subtraction, and multiplication of complex numbers. Sample code and output are provided for both classes to demonstrate their functionality.

Uploaded by

Abdul Basit Butt
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

Lab_Assignment lab 3

The document outlines a lab assignment focused on Object-Oriented Programming, requiring the creation of two classes: Numbers and Complex. The Numbers class performs basic arithmetic operations on two user-input numbers, while the Complex class handles addition, subtraction, and multiplication of complex numbers. Sample code and output are provided for both classes to demonstrate their functionality.

Uploaded by

Abdul Basit Butt
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

Lab Assignment - Object-Oriented

Programming
Q1: Class Numbers
Create a class named Numbers. The class should have two member variables a and b. Define
methods to:
• Initialize a and b from user input.
• Add two numbers and display the result.
• Multiply two numbers and display the result.
• Subtract two numbers and display the result.
• Call these functions in the main program using an object of the Numbers class.

Code:

class Numbers:
def __init__(self, a, b):
self.a = a
self.b = b

def add(self):
return self.a + self.b

def multiply(self):
return self.a * self.b

def subtract(self):
return self.a - self.b

# Main program
a = float(input("Enter first number (a): "))
b = float(input("Enter second number (b): "))

num = Numbers(a, b)
print(f"Addition: {num.add()}")
print(f"Multiplication: {num.multiply()}")
print(f"Subtraction: {num.subtract()}")
Sample Output:

Enter first number (a): 10


Enter second number (b): 5
Addition: 15.0
Multiplication: 50.0
Subtraction: 5.0

Q2: Class Complex


Modify the above program to deal with complex numbers. The class Complex should have
two member variables r and i for real and imaginary parts. Implement methods for:
• Addition: (a + ib) + (x + iy) = (a + x) + i(b + y).
• Subtraction: (a + ib) - (x + iy) = (a - x) + i(b - y).
• Multiplication: (a + ib) * (x + iy) = ax − by + i(ay + bx).
• Displaying the complex number in the format a + ib.

Code:

class Complex:
def __init__(self, real, imag):
self.real = real
self.imag = imag

def add(self, other):


real_part = self.real + other.real
imag_part = self.imag + other.imag
return Complex(real_part, imag_part)

def subtract(self, other):


real_part = self.real - other.real
imag_part = self.imag - other.imag
return Complex(real_part, imag_part)

def multiply(self, other):


real_part = self.real * other.real - self.imag * other.imag
imag_part = self.real * other.imag + self.imag * other.real
return Complex(real_part, imag_part)

def display(self):
sign = '+' if self.imag >= 0 else ''
return f"{self.real} {sign} {self.imag}i"

# Main program
r1 = float(input("Enter real part of first complex number: "))
i1 = float(input("Enter imaginary part of first complex number: "))
r2 = float(input("Enter real part of second complex number: "))
i2 = float(input("Enter imaginary part of second complex number:
"))

c1 = Complex(r1, i1)
c2 = Complex(r2, i2)

print(f"Addition: {c1.add(c2).display()}")
print(f"Subtraction: {c1.subtract(c2).display()}")
print(f"Multiplication: {c1.multiply(c2).display()}")

Sample Output:

Enter real part of first complex number: 3


Enter imaginary part of first complex number: 2
Enter real part of second complex number: 1
Enter imaginary part of second complex number: 4
Addition: 4 + 6i
Subtraction: 2 - 2i
Multiplication: -5 + 14i

You might also like