Question

In: Computer Science

Using Classes This problem relates to the pre-loaded class Person. Using the Person class, write a...

Using Classes

This problem relates to the pre-loaded class Person.

Using the Person class, write a function print_friend_info(person) which accepts a single argument, of type Person, and:

  • prints out their name
  • prints out their age
  • if the person has any friends, prints 'Friends with {name}'

Write a function create_fry() which returns a Person instance representing Fry. Fry is 25 and his full name is 'Philip J. Fry'

Write a function make_friends(person_one, person_two) which sets each argument as the friend of the other.

class Person(object):
def __init__(self, name, age, gender):
"""Construct a person object given their name, age and gender

Parameters:
name(str): The name of the person
age(int): The age of the person
gender(str): Either 'M' or 'F' for male or female
  
"""

self._name = name
self._age = age
self._gender = gender
self._friend = None

def __eq__(self, person):
return str(self) == str(person)

def __str__(self):
if self._gender == 'M':
title = 'Mr'
elif self._gender == 'F':
title = 'Miss'
else:
title = 'Ms'

return title + ' ' + self._name + ' ' + str(self._age)

def __repr__(self):
return 'Person: ' + str(self)

def get_name(self):
"""
(str) Return the name
  
"""
return self._name

def get_age(self):
"""
(int) Return the age
  
"""
return self._age

def get_gender(self):
"""
(str) Return the gender
  
"""
return self._gender

def set_friend(self, friend):
self._friend = friend

def get_friend(self):
"""
(Person) Return the friend
  
"""
return self._friend

Solutions

Expert Solution

class Person(object):
def __init__(self, name, age, gender):
"""Construct a person object given their name, age and gender

Parameters:
name(str): The name of the person
age(int): The age of the person
gender(str): Either 'M' or 'F' for male or female
  
"""

self._name = name
self._age = age
self._gender = gender
self._friend = None

def __eq__(self, person):
return str(self) == str(person)

def __str__(self):
if self._gender == 'M':
title = 'Mr'
elif self._gender == 'F':
title = 'Miss'
else:
title = 'Ms'

return title + ' ' + self._name + ' ' + str(self._age)

def __repr__(self):
return 'Person: ' + str(self)

def get_name(self):
"""
(str) Return the name
  
"""
return self._name

def get_age(self):
"""
(int) Return the age
  
"""
return self._age

def get_gender(self):
"""
(str) Return the gender
  
"""
return self._gender

def set_friend(self, friend):
self._friend = friend

def get_friend(self):
"""
(Person) Return the friend
  
"""
return self._friend

def print_friend_info(person):
print(person.get_name());
print(person.get_age());
print('Friends with '+str(person.get_friend().get_name()));
  
def create_fry():
fry=Person('Philip J. Fry',25,'M');
return fry;
  
def make_friends(person_one,person_two):
person_one.set_friend(person_two);
person_two.set_friend(person_one);
  
  
  
fry=create_fry();
friend_fry=Person('John doe',26,'M');
make_friends(fry,friend_fry);
print_friend_info(fry);
print("-----------------------------");
print_friend_info(friend_fry);

Expected output:


Related Solutions

I need to see a working example using inheritance and the classes (below) The person class...
I need to see a working example using inheritance and the classes (below) The person class has first name last name age hometown a method called getInfo() that returns all attributes from person as a String The student class is a person with an id and a major and a GPA student has to call super passing the parameters for the super class constructor a method called getInfo() that returns all attributes from student as a String this methods has...
Refactor the following classes so they are both derived from a base class called Person. Write...
Refactor the following classes so they are both derived from a base class called Person. Write the code for the new base class as well. Try to avoid repeating code as much as possible. Write the classes so that any common methods can be invoked through a pointer or reference to the base class. #include <string> #include <cmath> using namespace std; class Student { private: string name;    int age;    int studyYear; public:    Student(string,int,int); void study();    void...
USING R ---locate the pre-loaded MASS package, then load the data frame cats within that packag....
USING R ---locate the pre-loaded MASS package, then load the data frame cats within that packag. This provides data on sex, body weight (in kgs), and heart weight (in grams) for 144 household cats. Load the MASS package with a call to library("MASS"), and access the object directly by entering cats at the console prompt. 1. Fit a least-squares multiple linear regression model using heart weight as the response variable and the other two variables as predictors, and view a...
Shapes2D Write the following four classes to practice using an abstract class and polymorphism. Submit all...
Shapes2D Write the following four classes to practice using an abstract class and polymorphism. Submit all four classes. Shape2D class For this class, include just an abstract method name get2DArea() that returns a double. Rectangle2D class Make this class inherit from the Shape2D class. Have it store a length and a width as fields. Provide a constructor that takes two double arguments and uses them to set the fields. Note, the area of a rectangle is the length times the...
Shapes2D Write the following four classes to practice using an abstract class and polymorphism. Submit all...
Shapes2D Write the following four classes to practice using an abstract class and polymorphism. Submit all four classes. Shape2D class For this class, include just an abstract method name get2DArea() that returns a double. Rectangle2D class Make this class inherit from the Shape2D class. Have it store a length and a width as fields. Provide a constructor that takes two double arguments and uses them to set the fields. Note, the area of a rectangle is the length times the...
Shapes2D Write the following four classes to practice using an abstract class and polymorphism. Submit all...
Shapes2D Write the following four classes to practice using an abstract class and polymorphism. Submit all four classes. Shape2D class For this class, include just an abstract method name get2DArea() that returns a double. Rectangle2D class Make this class inherit from the Shape2D class. Have it store a length and a width as fields. Provide a constructor that takes two double arguments and uses them to set the fields. Note, the area of a rectangle is the length times the...
Shapes2D Write the following four classes to practice using an abstract class and polymorphism. Submit all...
Shapes2D Write the following four classes to practice using an abstract class and polymorphism. Submit all four classes. Shape2D class For this class, include just an abstract method name get2DArea() that returns a double. Rectangle2D class Make this class inherit from the Shape2D class. Have it store a length and a width as fields. Provide a constructor that takes two double arguments and uses them to set the fields. Note, the area of a rectangle is the length times the...
Shapes2D Assignment Write the following four classes to practice using an abstract class and polymorphism. Submit...
Shapes2D Assignment Write the following four classes to practice using an abstract class and polymorphism. Submit all four classes. Shape2D class For this class, include just an abstract method name get2DArea() that returns a double. Rectangle2D class Make this class inherit from the Shape2D class. Have it store a length and a width as fields. Provide a constructor that takes two double arguments and uses them to set the fields. Note, the area of a rectangle is the length times...
write Java program has two classes ,( using Inheritance ) first class set ( id ,...
write Java program has two classes ,( using Inheritance ) first class set ( id , name ) and method output second class ( id , name , Mark 1 , Mark 2 , Mark 3 ) method total , average , grade , results ( if the student pass or not ) , and output method
Shapes2D Write the following four classes to practice using an abstract class and polymorphism. Submit all...
Shapes2D Write the following four classes to practice using an abstract class and polymorphism. Submit all four classes. Shape2D class For this class, include just an abstract method name get2DArea() that returns a double. Rectangle2D class Make this class inherit from the Shape2D class. Have it store a length and a width as fields. Provide a constructor that takes two double arguments and uses them to set the fields. Note, the area of a rectangle is the length times the...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT