82.3k views
3 votes
1)Create a class Student according to the following requirements: a) A student has four attributes: id, name, GPA and major. b) Add the default constructor (no parameters). c) Add a constructor with four parameters to initialize all the attributes by specific values. d) Add all the required setter methods e) Add all the required getter methods f) Add the method changeMajor 2) Create a Driver class. that has the main method, in which: a) Create a student object (obj1), provide all values for the class attributes. b) Create another student object (obj2), provide all values for the class attributes. c) Print the values of obj1 and obj2 fields. d) Change the major for obj1. e) Change the major for obj2. f) Print the values of obj1 and obj2 fields.

User Bballant
by
7.5k points

1 Answer

4 votes

Below is the code for the Student and Driver classes:

Python

class Student:

# Default constructor

def __init__(self):

self.id = 0

self.name = ""

self.gpa = 0.0

self.major = ""

# Constructor with parameters

def __init__(self, id, name, gpa, major):

self.id = id

self.name = name

self.gpa = gpa

self.major = major

# Setter methods

def set_id(self, id):

self.id = id

def set_name(self, name):

self.name = name

def set_gpa(self, gpa):

self.gpa = gpa

def set_major(self, major):

self.major = major

# Getter methods

def get_id(self):

return self.id

def get_name(self):

return self.name

def get_gpa(self):

return self.gpa

def get_major(self):

return self.major

# Change major method

def change_major(self, new_major):

self.major = new_major

class Driver:

def main():

# Create student objects

obj1 = Student(1, "Alice", 3.8, "Computer Science")

obj2 = Student(2, "Bob", 3.5, "Mathematics")

# Print the values of obj1 and obj2 fields

print("Obj1:")

print("ID:", obj1.get_id())

print("Name:", obj1.get_name())

print("GPA:", obj1.get_gpa())

print("Major:", obj1.get_major())

print("\\Obj2:")

print("ID:", obj2.get_id())

print("Name:", obj2.get_name())

print("GPA:", obj2.get_gpa())

print("Major:", obj2.get_major())

# Change the major for obj1 and obj2

obj1.change_major("Software Engineering")

obj2.change_major("Physics")

# Print the values of obj1 and obj2 fields after changing the major

print("\\Obj1 after changing major:")

print("ID:", obj1.get_id())

print("Name:", obj1.get_name())

print("GPA:", obj1.get_gpa())

print("Major:", obj1.get_major())

print("\\Obj2 after changing major:")

print("ID:", obj2.get_id())

print("Name:", obj2.get_name())

print("GPA:", obj2.get_gpa())

print("Major:", obj2.get_major())

if __name__ == "__main__":

Driver.main()

User Manotheshark
by
7.7k points