Question

reate a class called Person with the following attributes: name - A string representing the person's...

reate a class called Person with the following attributes:

  • name - A string representing the person's name
  • age - An int representing the person's age in years

As well as appropriate __init__ and __str__ methods, include the following methods:

  • get_name(self) - returns the name of the person
  • get_age(self) - returns the age of the person
  • set_name(self, new_name) - sets the name for the person
  • set_age(self, new_age) - sets the age for the person
  • is_older_than(self, other) - returns True if this person is older than the one passed as the other parameter, False otherwise

class Person:
# TODO: Implement this class properly
def __init__(self, name, age):
pass

def __str__(self):
pass

def get_name(self):
pass

def get_age(self):
pass

TESTS:

Alice and Bob

Expected Output

Bob is older than Alice

You should calculate the output

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

class person:
def __init__(self, name, age):
self.name=name
self.age=age

def set_name(self, new_name):
self.name=new_name

def set_age(self, new_age):
self.age=new_age

def __str__(self):
return("Name of the person is {} and age is {}".format(self.name,self.age))

def get_name(self):
return self.name

def get_age(self):
return self.age

def is_older_than(self, other):
return(self.get_age()>other.get_age())

p1=person("Bob",30)#creating first object of the class person with name bob and age 30
p2=person("Alice",25)#creating first object of the class person with name alice and age 25
print(p1)#print the values of object p1
print(p2)#print the values of object p2

if(p1.is_older_than(p2)):
print("Bob is older than Alice")
else:
print("Alice is older than Bob")

output

code snaps

If you have any query regarding the code please ask me in the comment i am here for help you. Please do not direct thumbs down just ask if you have any query. And if you like my work then please appreciates with up vote. Thank You.

Add a comment
Know the answer?
Add Answer to:
reate a class called Person with the following attributes: name - A string representing the person's...
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 code? 1. Create a class called Person that has 4 attributes, the name, the age,...

    python code? 1. Create a class called Person that has 4 attributes, the name, the age, the weight and the height [5 points] 2. Write 3 methods for this class with the following specifications. a. A constructor for the class which initializes the attributes [2.5 points] b. Displays information about the Person (attributes) [2.5 points] c. Takes a parameter Xage and returns true if the age of the person is older than Xage, false otherwise [5 points] 3. Create another...

  • Using the following code: store.py from abc import ABC,abstractmethod class Store(ABC): #creating a abstract class name=''...

    Using the following code: store.py from abc import ABC,abstractmethod class Store(ABC): #creating a abstract class name='' #declaring attributes in class address='' status='' sales_tax_percentage=0.0 def __init__(self,name,address,status,sales_tax_percentage): #init method to initialize the class attributes self.name=name self.address=address self.status=status self.sales_tax_percentage=sales_tax_percentage def get_name(self): #Getter and setter methods for variables return self.name def set_name(self,name): self.name=name def get_address(self): return self.address def set_address(self,address): self.address=address def set_status(self,status): self.status=status def get_status(self): return self.status def set_sales_tax_percentage(self,sales_tax_percentage): self.sales_tax_percentage=sales_tax_percentage def get_sales_tax_percentage(self): return self.sales_tax_percentage def is_store_open(self): #check store status or availability if self.status=="open": #Return...

  • Create the Employee class: The Employee class has three attributes: private: string firstName; string lastName; string...

    Create the Employee class: The Employee class has three attributes: private: string firstName; string lastName; string SSN; The Employee class has ▪ a no-arg constructor (a no-arg contructor is a contructor with an empty parameter list): In this no-arg constructor, use “unknown” to initialize all private attributes. ▪ a constructor with parameters for each of the attributes ▪ getter and setter methods for each of the private attributes ▪ a method getFullName() that returns the last name, followed by a...

  • 9p This is for Python I need help. Pet #pet.py mName mAge class Pet: + __init__(name,...

    9p This is for Python I need help. Pet #pet.py mName mAge class Pet: + __init__(name, age) + getName + getAge0 def init (self, name, age): self.mName = name self.mAge = age Dog Cat def getName(self): return self.mName mSize mColor + __init__(name, age,size) + getSize() + momCommento + getDog Years() +_init__(name, age,color) + getColor + pretty Factor + getCatYears def getAge(self): return self.mAge #dog.py import pet #cat.py import pet class Dog (pet. Pet): class Cat (pet. Pet): def init (self,...

  • Task 2: SecretWord class: Download and save a copy of LinkedLists.py (found at the bottom of...

    Task 2: SecretWord class: Download and save a copy of LinkedLists.py (found at the bottom of this assignment page on eClass). In this file, you have been given the complete code for a LinkedList class. Familiarize yourself with this class, noticing that it uses the Node class from Task 1 and is almost identical to the SLinkedList class given in the lectures. However, it also has a complete insert(pos, item) method (which should be similar to the insert method you...

  • Python 3> Make a class called BMW: Parameterized constructor with three instance variables (attributes): Hint: def...

    Python 3> Make a class called BMW: Parameterized constructor with three instance variables (attributes): Hint: def __init__(self, make, model, year) Methods in BMW parent class (can leave as method stubs for now): startEngine() -must print "The engine is now on." stopEngine() -must print "The engine is now off." Make another class called ThreeSeries -Must inherit BMW Hint: class ThreeSeries(BMW): -Also passes the three attributes of the parent class and invoke parent Hint: BMW.__init__(self, cruiseControlEnabled, make, model, year) -Constructor must have...

  • Q1. Write a program to simulate a grocery waiting queue. Your program should ask the user...

    Q1. Write a program to simulate a grocery waiting queue. Your program should ask the user if they want to add a customer to the queue, serve the next customer in the queue, or exit. When a customer is served or added to the queue, the program should print out the name of that customer and the remaining customers in the queue. The store has two queues: one is for normal customers, another is for VIP customers. Normal customers can...

  • class WorkoutClass: """A workout class that can be offered at a gym. === Private Attributes ===...

    class WorkoutClass: """A workout class that can be offered at a gym. === Private Attributes === _name: The name of this WorkoutClass. _required_certificates: The certificates that an instructor must hold to teach this WorkoutClass. """ _name: str _required_certificates: List[str] def __init__(self, name: str, required_certificates: List[str]) -> None: """Initialize a new WorkoutClass called <name> and with the <required_certificates>. >>> workout_class = WorkoutClass('Kickboxing', ['Strength Training']) >>> workout_class.get_name() 'Kickboxing' """ def get_name(self) -> str: """Return the name of this WorkoutClass. >>> workout_class =...

  • 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...

  • Write a program that does the following in Python Code: Write a new Class called Famous_Day_Born which is a subclass of Famous_Person. Add a method to calculate the day of the week the person was born...

    Write a program that does the following in Python Code: Write a new Class called Famous_Day_Born which is a subclass of Famous_Person. Add a method to calculate the day of the week the person was born and print out all of the corresponding information using the overridden print method. Use the following code below as a starting point. ////////////////////////////////////////////////////// from datetime import datetime class Famous_Person(object): def __init__(self, last, first, month,day, year): self.last = last self.first = first self.month = month...

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
ADVERTISEMENT