Question

There are design imperfections with this code. relating to encapsulation (in terms of the class), and...

There are design imperfections with this code. relating to encapsulation (in terms of the class), and something to do with how age is calculated being rigid (in terms of it only works for 2019, and will break next year). Improve the design of this code, and add a short comment or two up the top explaining what you've done and why.

code:

class Student:
def __init__(self, firstName, lastName, birth_year):
self.name = firstName + " " + lastName
self.birth_year = birth_year

if __name__ == '__main__':
s = Student("Rob", "Everest", 1961)
years_old = 2019 - s.birth_year
print(f"{s.name} is {years_old} old")

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

thanks for the question, here is the updated code with comments.

=======================================================================

# datetime module to get the current year
from datetime import datetime


class Student():

    def __init__(self,firstName,lastName,birth_Year):
        # make the instance variables as private
        # so that they cannot be accessed directly
        # we make them private by appending with two underscores
        # for accessing the values of the isntance variable we provide accessor functions
       
self.__name = firstName+" "+lastName
        self.__birth_year = birth_Year

    def getName(self):
        return self.__name

    def getBirthYear(self):
        return self.__birth_year

if __name__ == '__main__':

    s = Student('Rob','Everest',1961)
    # find the current year using the below line of code
   
current_year =datetime.now().year

    # use accessor functions to get the value of birth year
   
years_old = current_year-s.getBirthYear()
    # use accessor functions to get the value of name
   
print('{} is {} old'.format(s.getName(),years_old))

=================================================================

Add a comment
Know the answer?
Add Answer to:
There are design imperfections with this code. relating to encapsulation (in terms of the class), and...
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
  • Must be in Python 3 exercise_2.py # Define the Student class class Student():    # Ini...

    Must be in Python 3 exercise_2.py # Define the Student class class Student():    # Initialize the object properties def __init__(self, id, name, mark): # TODO # Print the object as an string def __str__(self): return ' - {}, {}, {}'.format(self.id, self.name, self.mark) # Check if the mark of the input student is greater than the student object # The output is either True or False def is_greater_than(self, another_student): # TODO # Sort the student_list # The output is the sorted...

  • PYTHON. Continues off another code(other code is below). I don't understand this. Someone please help! Comment...

    PYTHON. Continues off another code(other code is below). I don't understand this. Someone please help! Comment the lines please so I can understand. There are short and med files lengths for each the list of names/ids and then search id file. These are the input files: https://codeshare.io/aVQd46 https://codeshare.io/5M3XnR https://codeshare.io/2W684E https://codeshare.io/5RJwZ4 LinkedList ADT to store student records(code is below). Using LinkedList ADT instead of the Python List. You will need to use the Student ADT(code is below) Imports the Student class...

  • PYTHON. Continues off another code. I don't understand this. Someone please help! Comment the lines please...

    PYTHON. Continues off another code. I don't understand this. Someone please help! Comment the lines please so I can understand LinkedList ADT: class myLinkedList:     def __init__(self):         self.__head = None         self.__tail = None         self.__size = 0     def insert(self, i, data):         if self.isEmpty():             self.__head = listNode(data)             self.__tail = self.__head         elif i <= 0:             self.__head = listNode(data, self.__head)         elif i >= self.__size:             self.__tail.setNext(listNode(data))             self.__tail = self.__tail.getNext()         else:             current = self.__getIthNode(i - 1)             current.setNext(listNode(data,...

  • Please help. I need a very simple code. For a CS 1 class. Write a program...

    Please help. I need a very simple code. For a CS 1 class. Write a program in Java and run it in BlueJ according to the following specifications: The program reads a text file with student records (first name, last name and grade). Then it prompts the user to enter a command, executes the command and loops. The commands are the following: "printall" - prints all student records (first name, last name, grade). "firstname name" - prints all students with...

  • PYTHON 3 Object Oriented Programming ***a9q3.py file below*** class GradeItem(object): # A Grade Item is anything...

    PYTHON 3 Object Oriented Programming ***a9q3.py file below*** class GradeItem(object): # A Grade Item is anything a course uses in a grading scheme, # like a test or an assignment. It has a score, which is assessed by # an instructor, and a maximum value, set by the instructor, and a weight, # which defines how much the item counts towards a final grade. def __init__(self, weight, scored=None, out_of=None): """ Purpose: Initialize the GradeItem object. Preconditions: :param weight: the weight...

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