In: Computer Science
Question 1 (10)
Create a class Student with public member variables: Student name,
student number, contact number, ID number. The following
specifications are required:
Add init() method of the class that initializes string member
variables to empty strings and numeric values to 0. (2)
Add the method populate() to the class. The method is used to
assign values to the member variables of the class. (2)
Add the method display() to the class. The method is used to
display the member variables of the class. (2)
Create an instance StudentObj of the class Student in a main
program. (2)
The main program should prompt the user to enter values for five
students. The attributes should be assigned to the instance of
Student using its populate() method, and must be displayed using
the display() method of the instance of Student. (2)
Please make the answer simple and straightforward for a beginner intro to python . Thank you
Here is the completed code for this problem. Comments are included, go through it, learn how things work and let me know if you have any doubts or if you need anything to change. If you are satisfied with the solution, please rate the answer. Thanks
EDIT: Screenshots are added.
#code
#Student class
class Student:
#constructor initializing student fields to
default values
def __init__(self):
self.__name='' #string name, ex: John
self.__number=0
#int number, ex: 100
self.__contact='' #string contact number, ex:
123-457-9991
self.__id='' #string id number, ex:
ABC112
#method to read data from user and fill the
fields of this Student
def populate(self):
self.__name=input('Enter name:
')
self.__number=int(input('Enter student number:
'))
self.__contact=input('Enter contact number:
')
self.__id=input('Enter ID number: ')
#method to display student's data to
console
def display(self):
print('Name:',self.__name)
print('Student
number:',self.__number)
print('Contact
number:',self.__contact)
print('ID
number:',self.__id)
#end of Student class
#main method
def main():
#creating a Student instance
studentObj=Student()
#looping for 5 times
for i in
range(5):
#populating
student's fields
studentObj.populate()
#displaying it
studentObj.display()
print() #line
break
main()
#output
Enter name: Oliver
Enter student number: 1234
Enter contact number: 999-112-123
Enter ID number: ABC1234
Name: Oliver
Student number: 1234
Contact number: 999-112-123
ID number: ABC1234
Enter name: John
Enter student number: 101
Enter contact number: 999-123-101
Enter ID number: ABC1235
Name: John
Student number: 101
Contact number: 999-123-101
ID number: ABC1235
Enter name: Barry
Enter student number: 102
Enter contact number: 999-125-102
Enter ID number: ABC0101
Name: Barry
Student number: 102
Contact number: 999-125-102
ID number: ABC0101
Enter name: Kevin
Enter student number: 103
Enter contact number: 112-112-110
Enter ID number: ABC1236
Name: Kevin
Student number: 103
Contact number: 112-112-110
ID number: ABC1236
Enter name: Jane
Enter student number: 104
Enter contact number: 110-8585-120
Enter ID number: XCV0012
Name: Jane
Student number: 104
Contact number: 110-8585-120
ID number: XCV0012
CODE SCREENSHOT
OUTPUT SCREENSHOT