In: Computer Science
This is Python programming
Focus
1. Classes and Objects
2. Creating objects
3. Manipulating objects
This lab maps to learning the following objectives: Write a working
program that creates a Class, Instances of Objects from that Class,
and Functions that use Objects as parameters.
For this portion of the lab, you will create a new program for
your Professor. Create a class named Student that holds the
following data about a student:
1. Name
2. Student ID number
3. GPA
4. Expected grade in this course
5. Full time or part time
Create five student objects from this class and pass data to fill
in the class data above. Besides creating the objects, you will
write a menu-driven program that performs the following
tasks:
1. Look up and print the student GPA
2. Add a new student to the class
3. Change the GPA of a student
4. Change the expected grade of a student
5. Print the data of all the students in a tabular format
6. Quit the program Save all the files that you create in this program with appropriate file names.
Solution:
This problem is solved using two python files
student.py:
class Student: #student class definition
name=''
studentID=0 #variables declaration
GPA=0
expected_grade=''
type_fp=''
def __init__(self,name,studentID,GPA,expected_grade,type_fp):
#constructor of class
self.name=name #set all the class variables
self.studentID=studentID
self.GPA=GPA
self.expected_grade=expected_grade
self.type_fp=type_fp
def setGPA(self,GPA): #method to set gpa
self.GPA=GPA
def set_expected_grade(self,grade): #method to set grade
self.expected_grade=expected_grade
s1=Student("name1",1,8.5,'A',"full time") #create sample student
class objects
s2=Student("name2",2,7.0,'B',"part time")
s3=Student("name3",3,8.9,'A',"part time")
s4=Student("name4",4,8.5,'A',"full time")
s5=Student("name5",5,6.0,'C',"full time")
student_objects=[s1,s2,s3,s4,s5] #Use list that stores all the
student objects
menu.py:
from student import * #import student module
while(1):
print("1. Lookup and print student GPA") #print menu options
print("2. Add a new student to the class")
print("3. Change the GPA of a student")
print("4. Change the expected grade of a student")
print("5. Print the data of all the students in a tabular
format.")
print("6.Quit")
ch=int(input("Enter choice")) #read choice
if ch==1:
sname=input("Enter name") #If ch is 1,read name
for i in student_objects:
if i.name==sname: #check names of students
print("The GPA is "+str(i.GPA)) #print GPA if name matches
break
elif ch==2: #IF ch is 2
name=input("Enter name") #read details of student
studentID=int(input("Enter ID"))
GPA=float(input("Enter GPA"))
expected_grade=input("Enter expected grade")
type_fp=input("Enter full time or part time")
student_objects.append(Student(name,studentID,GPA,expected_grade,type_fp))#add
student object to list
print("Student added to the class")
elif ch==3:#If ch is 3
n=int(input("Enter student ID")) #Get student id
for i in student_objects:
if i.studentID==n: #If student id is found, update the gpa
gpa=int(input("Enter gpa")) #read gpa
i.setGPA(gpa)
elif ch==4:
n=int(input("Enter student ID")) #if ch is 4,read is from
user
for i in student_objects:
if i.studentID==n:
grade=int(input("Enter grade")) #If id is found read grade
i.set_expected_grade(grade) #update the grade
elif ch==5:
for i in student_objects: #If ch is 5,print data
print("%5s \t %d \t %1.1f \t %s \t
%s"%(i.name,i.studentID,i.GPA,i.expected_grade,i.type_fp))
elif ch==6: #If ch is 6,break loop
break
Screenshots:
The Screenshots are attached below for reference.
Please follow them for proper indentation.
Please create two python files with names that i mentioned above.
Make sure that the both files are present in same folder.