Q7

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

NAME:RUTUJA KANE SUBJECT: PPL ROLLON:48 DIV:C(AIML)

Q7)A- ) Write a python Program to display welcome to “JSPM University” by using classes and
objects?

print("RUTUJA KANE")

# Define a class named University

class University:

# Method to display the message

def display_message(self):

print("Welcome to JSPM University")

# Create an object of the University class

university_obj = University()

# Call the display_message method using the object

university_obj.display_message()

OUTPUT:-
NAME:RUTUJA KANE SUBJECT: PPL ROLLON:48 DIV:C(AIML)

B) Write a python Program to call data member and function using classes and objects?

print("RUTUJA KANE")

# Define a class

class Student:

# Data members

name = "" # A string to hold the name

age = 0 # An integer to hold the age

# Function (Method) to set the details

def set_details(self, name, age):

self.name = name

self.age = age

# Function (Method) to display the details

def display_details(self):

print(f"Student Name: {self.name}")

print(f"Student Age: {self.age}")

# Create an object of the Student class

student1 = Student()

# Call the set_details method to assign values to data members

student1.set_details("rutuja kane", 20)

# Call the display_details method to display the data members

student1.display_details()
NAME:RUTUJA KANE SUBJECT: PPL ROLLON:48 DIV:C(AIML)

OUTPUT:-
NAME:RUTUJA KANE SUBJECT: PPL ROLLON:48 DIV:C(AIML)

C) Write a program to find sum of two numbers using class and methods?

print("RUTUJA KANE")

# Define a class named Calculator

class Calculator:

# Method to calculate the sum of two numbers

def add(self, num1, num2):

return num1 + num2

# Create an object of the Calculator class

calc = Calculator()

# Input: Get two numbers from the user

num1 = float(input("Enter the first number: "))

num2 = float(input("Enter the second number: "))

# Call the add method to calculate the sum

result = calc.add(num1, num2)

# Display the result

print(f"The sum of {num1} and {num2} is: {result}")


NAME:RUTUJA KANE SUBJECT: PPL ROLLON:48 DIV:C(AIML)

OUTPUT:-
NAME:RUTUJA KANE SUBJECT: PPL ROLLON:48 DIV:C(AIML)

D) Write a program to read 3 subject marks and display pass or failed using class and object.?

print("RUTUJA KANE")

class Student:

def __init__(self, name):

self.name = name

self.marks = []

# Method to input marks for 3 subjects

def input_marks(self):

for i in range(3):

mark = float(input(f"Enter marks for subject {i + 1}: "))

self.marks.append(mark)

# Method to calculate whether the student passed or failed

def check_result(self):

if len(self.marks) == 3:

average = sum(self.marks) / 3

print(f"\nStudent: {self.name}")

print(f"Average Marks: {average}")

if average >= 40: # Assuming 40 is the passing average

print("Result: Passed")

else:

print("Result: Failed")

else:

print("Invalid number of marks. Please enter marks for 3 subjects.")

# Driver code

if __name__ == "__main__":

student_name = input("Enter the student's name: ")

student = Student(student_name)
NAME:RUTUJA KANE SUBJECT: PPL ROLLON:48 DIV:C(AIML)

student.input_marks() # Get marks for 3 subjects

student.check_result() # Check and display result

OUTPUT:-

You might also like