Question: Python 3 Problem: Please help with this as well as an explanation. Thanks! Create a program that contains one class called Person. The Person class
Python 3 Problem: Please help with this as well as an explanation. Thanks!
Create a program that contains one class called Person. The Person class should contain an __init__ method that sets the variables for the first name, last name, age, and height (in cm).
Overload the following operators to do as instructed:
| + | Return a new instance of Person where The first name is the first objects name + Jr. The last name is the first and second objects last name with a hyphen in between The age is 0 The height is the first and second objects height added together mod 10 plus 30 EXP: Person(John, Doe, 30, 180) + Person(Jane, Smith, 30, 160) = Person(John Jr., Doe-Smith, 0, 30) |
| -= | If possible, remove the hyphenated last name from the object EXP: Person(John Jr., Doe-Smith, 0, 30) - Person(John, Doe, 30, 180) = Person(John Jr., Smith, 0, 30) If not possible, change nothing and print out Not Possible (Ensure that None isnt returned and breaks your program) |
| += | Allow a tuple containing two pieces of data (age and height) to be passed to and added to the objects current age and height. If a single number is passed instead of a tuple, add it only to the age. EXP: Object.age = 10 Object += 10 Object.age == 20 Object.age = 10 Object.height = 100 Object += (10, 20) Object.age == 20 Object.height == 120 |
| == | If all variables in the object are the same, return True, else return False EXP: Object == Object True |
| != | If any of the variables are different, return True, else return False EXP: Object != OtherObject True |
| > | If the age of the first object is larger that the age of the second object, return True, else return False |
| >= | If the age of the first object is larger or equal to the age of the second object, return True, else return False |
| < | If the age of the first object is smaller than the age of the second object, return True, else return False |
| <= | If the age of the first object is smaller or equal to the age of the second object, return True, else return False |
| str() | Return the first and last name and age and height of the object formatted like so: John Doe 30 180 |
Create two objects of type Person with the following attributes:
| First Name: Bob Last Name: Robert Age: 40 Height: 167 | First Name: Alice Last Name: Wonder Age: 38 Height: 170 |
Use the + first to create a third object of Person.
Use all overloaded operators with these three objects, make sure to have print statements for each operator to make sure they are working.
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
