Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
2 views

Python_OOP_Assignment lab 4

The document outlines the implementation of two Python classes: Rational and Student. The Rational class handles the creation and display of rational numbers, ensuring the denominator is not zero, while the Student class manages student information, including name and marks, and calculates the average of the best three marks. Usage examples for both classes demonstrate how to instantiate objects and call their methods.

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

Python_OOP_Assignment lab 4

The document outlines the implementation of two Python classes: Rational and Student. The Rational class handles the creation and display of rational numbers, ensuring the denominator is not zero, while the Student class manages student information, including name and marks, and calculates the average of the best three marks. Usage examples for both classes demonstrate how to instantiate objects and call their methods.

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/ 2

Assignment: Python OOP – Rational and

Student Classes
1. Rational Number Class

class Rational:
def __init__(self):
self.__num = 0
self.__den = 1

def take_input(self):
self.__num = int(input("Enter numerator: "))
self.__den = int(input("Enter denominator: "))
if self.__den == 0:
print("Denominator cannot be zero. Setting it to 1.")
self.__den = 1

def show_ratio(self):
result = self.__num / self.__den
print("The rational number is:", result)

Usage Example:

r = Rational()
r.take_input()
r.show_ratio()

2. Student Class

class Student:
def __init__(self):
self.name = ""
self.marks = []

def take_input(self):
self.name = input("Enter student name: ")
self.marks = list(map(int, input("Enter 4 marks separated by space: ").split()))
if len(self.marks) != 4:
print("Warning: Please enter exactly 4 marks.")
self.marks = self.marks[:4]

def display(self):
print("Name:", self.name)
print("Marks:", self.marks)

def best_three_avg(self):
top3 = sorted(self.marks, reverse=True)[:3]
avg = sum(top3) / 3
print("Best 3 Average for", self.name, "=", avg)

Usage Example:

s = Student()
s.take_input()
s.display()
s.best_three_avg()

You might also like