2. Calculate the Student’s Performance

In this challenge, you need to implement a method that squares passing variables and returns their sum.
Problem statement
In this exercise, you have to calculate a student’s total marks using the concept of Classes.
Problem statement
Implement a class – Student – that has four properties and two methods. All these attributes (properties and methods) should be public. This problem can be broken down into three tasks.
Task 1
Implement a constructor to initialize the values of four properties: name, phy, chem, and bio.
Task 2
Implement a method – totalObtained – in the Student class that calculates total marks of a student.
Sample properties:

1
2
3
4
name = Mark
phy  = 80
chem = 90
bio  = 40

Sample method output: obj1.Total()=210
Task 3
Using the totalObtained method, implement another method, percentage, in the Student class that calculates the percentage of students marks. Assume that the total marks of each subject are 100. The combined marks of three subjects are 300.
The formula for calculating the percentage:

Sample input:

1
2
3
phy  = 80
chem = 90
bio  = 40

Sample output: 70
Coding exercise
Design a step-by-step algorithm before jumping to the implementation.

1
2
3
4
5
6
7
8
9
class Student:
def __init__(self):
pass

def totalObtained(self):
pass

def percentage(self):
pass
Solution
1
2
3
4
5
6
7
8
9
10
11
12
class Student:
def __init__(self, name, phy, chem, bio):
self.name = name
self.phy = phy
self.chem = chem
self.bio = bio

def totalObtained(self):
return(self.phy + self.chem + self.bio)

def percentage(self):
return((self.totalObtained() / 300) * 100)

Explanation
In line 3 – 6, we have initialized name, phy, chem, and bio as public properties in the class initializer.
In line 9, we have then defined the totalObtained() method. In this method, we added individual marks of all the subjects and returned the sum.
At last in line 12, we defined percentage(). In this method, we called totalObtained() and used its value to calculate the percentage.
300
300
is the total number of marks for a student.