Question

For Python 3.7+.

In this assignment you are asked to write a Python program to define a class to model the characteristics of a university stuCode that prints a student object that implicitly uses the_str_method looks similar to this, with the output immediately foll

TEST THE CODE WITH THE FOLLOWING GLOBAL CODE AFTER YOU'RE DONE:

print('\nStart of A2 Student class demo ')

s1 = Student('David Miller', major = 'Hist',enrolled = 'y', credits = 0, qpoints = 0)
s2 = Student('Sonia Fillmore', major = 'Math',enrolled = 'y', credits = 90, qpoints = 315)
s3 = Student('A. Einstein', major = 'Phys',enrolled = 'y', credits = 0, qpoints = 0)         
s4 = Student('W. A. Mozart', major = 'Mus',enrolled = 'n', credits = 29, qpoints = 105)
s5 = Student('Sonia Fillmore', major = 'CSci',enrolled = 'y', credits = 60, qpoints = 130)
s5.g_num = s2.g_num
s6 = Student('Pierre Renoir', major = 'Art',enrolled = 'y', credits = 120, qpoints = 315)
print('s1 = ', s1)
print('s2 = ', s2)
print('s3 = ', s3)
print('s4 = ', s4)
print('s5 = ', s5)
print('s6 = ', s6)
print('\nTotal number of students: ', Student.totalEnrollment)
print('The gpa of ', s1.name, ' is ', s2.gpa())
print('Class standing of ', s4.name, ' is ', s4.status())
print('Class standing of ', s2.name, ' is ', s2.status())
if s1.sameStudent(s2):
   print (s1.name, ' and ', s2.name, ' are the same student')
else:
   print (s1.name, ' and ', s2.name, ' are not the same student')
if s2.sameStudent(s5):
   print (s2.name, ' and ', s5.name, ' are the same student')
else:
   print (s2.name, ' and ', s5.name, ' are not the same student')
if s1.isEnrolled():
   print (s1.name, ' is currently enrolled')
else:
   print (s1.name, ' is not currently enrolled')
if s4.isEnrolled():
   print (s4.name, ' is currently enrolled')
else:
   print (s4.name, ' is not currently enrolled')
print('\nEnd of A2 Student class demo')

0 0
Add a comment Improve this question Transcribed image text
Answer #1

PYTHON CODE:

class Student:

# class variable to track the number of student object created
totalEnrollment=0

# constructor to initialize the object
def __init__(self,name,major='IST',enrolled='y',credits=0,qpoints=0):

# incrementing the class variable
Student.totalEnrollment+=1
self.name=name
self.major=major
self.enrolled=enrolled
self.credits=credits
self.qpoints=qpoints
self.g_num="G0000"+str(Student.totalEnrollment)

# method to calculature GPA
def gpa(self):

try:

return self.qpoints/self.credits
  
except ZeroDivisionError :
  
return 0

# method to provide the string representation of the object
def __str__(self):

return self.name+", "+self.g_num+", "+self.status()+", "+\
self.major+", active: "+self.enrolled+" , credits = "+\
str(self.credits)+", "+'%.2f' % self.gpa()

  
# method to check whether the student is enrolled or not
def isEnrolled(self):

if self.enrolled == 'y':
return True
elif self.enrolled == 'n':
return False
else:
pass

# method to get the student status based on the credits
def status(self):

if self.credits <= 30:
return "Freshman"
elif self.credits > 30 and self.credits <=59:
return "Sophomore"
elif self.credits >=60 and self.credits <=89:
return "Junior"
elif self.credits >=90:
return "Senior"
else:
pass

# method to check the whether current student object is
# same as the other student object by comparing the g_num
def sameStudent(self,other):

if self.g_num == other.g_num:
return True
else:
return False

# testing
print('\nStart of A2 Student class demo ')
s1 = Student('David Miller', major = 'Hist',enrolled = 'y', credits = 0, qpoints = 0)
s2 = Student('Sonia Fillmore', major = 'Math',enrolled = 'y', credits = 90, qpoints = 315)
s3 = Student('A. Einstein', major = 'Phys',enrolled = 'y', credits = 0, qpoints = 0)
s4 = Student('W. A. Mozart', major = 'Mus',enrolled = 'n', credits = 29, qpoints = 105)
s5 = Student('Sonia Fillmore', major = 'CSci',enrolled = 'y', credits = 60, qpoints = 130)
s5.g_num = s2.g_num
s6 = Student('Pierre Renoir', major = 'Art',enrolled = 'y', credits = 120, qpoints = 315)
print('s1 = ', s1)
print('s2 = ', s2)
print('s3 = ', s3)
print('s4 = ', s4)
print('s5 = ', s5)
print('s6 = ', s6)
print('\nTotal number of students: ', Student.totalEnrollment)
print('The gpa of ', s1.name, ' is ', s2.gpa())
print('Class standing of ', s4.name, ' is ', s4.status())
print('Class standing of ', s2.name, ' is ', s2.status())

if s1.sameStudent(s2):
print (s1.name, ' and ', s2.name, ' are the same student')
else:
print (s1.name, ' and ', s2.name, ' are not the same student')
  
if s2.sameStudent(s5):
print (s2.name, ' and ', s5.name, ' are the same student')
else:
print (s2.name, ' and ', s5.name, ' are not the same student')
  
if s1.isEnrolled():
print (s1.name, ' is currently enrolled')
else:
print (s1.name, ' is not currently enrolled')
  
if s4.isEnrolled():
print (s4.name, ' is currently enrolled')
else:
print (s4.name, ' is not currently enrolled')

print('\nEnd of A2 Student class demo')

Screenshot:-

class Student: # class variable to track the number of student object created totalEnrollment=0 #constructor to initialize thOutput:-

Start of A2 Student class demo s1 = David Miller, G00001, Freshman, Hist, active: Y ,credits = 0, 0.00 s2 = Sonia Fillmore, G

Add a comment
Know the answer?
Add Answer to:
For Python 3.7+. TEST THE CODE WITH THE FOLLOWING GLOBAL CODE AFTER YOU'RE DONE: print('\nStart of...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Python : Need help with output of table Code: # Number of students print('How many students...

    Python : Need help with output of table Code: # Number of students print('How many students are in the class?') student_Number = int(input()) # Store list of students who are valid student_List = [] for i in range(0, student_Number): # To store current student data student_Data = [] print('Enter the name of student:') # Name of Students student_Name = input() if len(student_Name)> 24: print('Name of student cannot exceed 24 characters.') continue print('Enter the gpa of student:') # GPA of Student...

  • Can you please help me print columns for my C++ project? I'd like the print-out to...

    Can you please help me print columns for my C++ project? I'd like the print-out to have 3 columns to line up with of 1) First Name & Last Name 2) ID # and 3) Sales total I've attached the code below. Thank you in advance for your help. Seller::Seller() { setFirstName("None"); setLastName("None"); setID("ZZZ000"); setSalesTotal(0); }Seller::Seller(char first[],char last[],char id[],double sales) { setFirstName(first); setLastName(last); setID(id); setSalesTotal(sales); } /* Method: void setSalesTotal( double newSalesTotal ) Use: Changes a Seller's sales total Arguments:...

  • // Client application class and service class Create a NetBeans project named StudentClient following the instructions...

    // Client application class and service class Create a NetBeans project named StudentClient following the instructions provided in the Starting a NetBeans Project instructions in the Programming Exercises/Projects Menu on Blackboard. Add a class named Student to the StudentClient project following the instructions provided in the Starting a NetBeans Project instructions in the Programming Exercises/Projects Menu on Blackboard. After you have created your NetBeans Project your application class StudentC lient should contain the following executable code: package studentclient; public class...

  • I need code in java The Student class: CODE IN JAVA: Student.java file: public class Student...

    I need code in java The Student class: CODE IN JAVA: Student.java file: public class Student {    private String name;    private double gpa;    private int idNumber;    public Student() {        this.name = "";        this.gpa = 0;        this.idNumber = 0;    }    public Student(String name, double gpa, int idNumber) {        this.name = name;        this.gpa = gpa;        this.idNumber = idNumber;    }    public Student(Student s)...

  • Please help, provide source code in c++(photos attached) develop a class called Student with the following protected...

    Please help, provide source code in c++(photos attached) develop a class called Student with the following protected data: name (string), id (string), units (int), gpa (double). Public functions include: default constructor, copy constructor, overloaded = operator, destructor, read() to read data into it, print() to print its data, get_name() which returns name and get_gpa() which returns the gpa. Derive a class called StuWorker (for student worker) from Student with the following added data: hours (int for hours assigned per week),...

  • Purpose This assignment is an exercise in implementing the Stack ADT using a dynamically-allocated array, as...

    Purpose This assignment is an exercise in implementing the Stack ADT using a dynamically-allocated array, as well as techniques for managing dynamically-allocated storage in C++. Assignment In this assignment, you will write a class called Stack that will encapsulate a dynamically-allocated array of elements of a generic data type. A driver program is provided for this assignment to test your implementation. You don't have to write the tests. Program You will need to write a single template class for this...

  • 11p Python Language Read the following code for oofraction. import oofraction class OOFraction: def main(): fl...

    11p Python Language Read the following code for oofraction. import oofraction class OOFraction: def main(): fl = oofraction.o0Fraction( 2, 5) f2 = oofraction.o0Fraction ( 1, 3) f3 = f1 + f2 print (str(3)) main() def __init__(self, Num, Den): self.mNum = Num self.mDen = Den return def_add_(self,other): num = self.mNumother.mDen + other.mNum*self.mDen den = self.mDen*other.mDen f = OOFraction(num,den) return f 1. Write the output below. def_str_(self): s = str( self.mNum )+"/" + str( self.mDen) returns 2. Write a class called wholeNum...

  • JAVA program Note: you can't change anything is already written Student.java public class Student {   ...

    JAVA program Note: you can't change anything is already written Student.java public class Student {    private String name;    private String major;    private double gpa;    public Student(String name, String major, double gpa) {        // TO-DO: Assign the given parameters to the data fields. Use the this keyword.    }    public double getGPA() {        // TO-DO: return this.gpa    }    public String getName() {        // TO-DO: return this.name    }...

  • PYTHON The provided code in the ATM program is incomplete. Complete the run method of the...

    PYTHON The provided code in the ATM program is incomplete. Complete the run method of the ATM class. The program should display a message that the police will be called after a user has had three successive failures. The program should also shut down the bank when this happens. [comment]: <> (The ATM program allows a user an indefinite number of attempts to log in. Fix the program so that it displays a message that the police will be called...

  • I need help for part B and C 1) Create a new project in NetBeans called...

    I need help for part B and C 1) Create a new project in NetBeans called Lab6Inheritance. Add a new Java class to the project called Person. 2) The UML class diagram for Person is as follows: Person - name: String - id: int + Person( String name, int id) + getName(): String + getido: int + display(): void 3) Add fields to Person class. 4) Add the constructor and the getters to person class. 5) Add the display() method,...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT