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...
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...
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...
Write two classes Class A and Class B in class A you should read from the...
Write two classes Class A and Class B in class A you should read from the keyboard a number and you should call a public method of the class B that will print on the screen whether the number that was read from the keyboard in class A is multiple of 7 and has the last digit 5.
Classes and Objects Write a program that will create two classes; Services and Supplies. Class Services...
Classes and Objects Write a program that will create two classes; Services and Supplies. Class Services should have two private attributes numberOfHours and ratePerHour of type float. Class Supplies should also have two private attributes numberOfItems and pricePerItem of type float. For each class, provide its getter and setter functions, and a constructor that will take the two of its private attributes. Create method calculateSales() for each class that will calculate the cost accrued. For example, the cost accrued for...
Problem Write in drjava is fine. Using the classes from Assignment #2, do the following: Modify...
Problem Write in drjava is fine. Using the classes from Assignment #2, do the following: Modify the parent class (Plant) by adding the following abstract methods: a method to return the botanical (Latin) name of the plant a method that describes how the plant is used by humans (as food, to build houses, etc) Add a Vegetable class with a flavor variable (sweet, salty, tart, etc) and 2 methods that return the following information: list 2 dishes (meals) that the...
Problem Write in drjava is fine. Using the classes from Assignment #2, do the following: Modify...
Problem Write in drjava is fine. Using the classes from Assignment #2, do the following: Modify the parent class (Plant) by adding the following abstract methods: a method to return the botanical (Latin) name of the plant a method that describes how the plant is used by humans (as food, to build houses, etc) Add a Vegetable class with a flavor variable (sweet, salty, tart, etc) and 2 methods that return the following information: list 2 dishes (meals) that the...
1- Write a class called MedicalStaff that is a Person (the class that you wrote in...
1- Write a class called MedicalStaff that is a Person (the class that you wrote in last lab). A MedicalStaff has specialty (i.e. Orthopedic, cardiology, etc.). 2- Then write two classes: Doctor class has office visit fee. Nurse class has title (i.e. RN, NP, etc.) Both classes inherit from MedicalStaff. Be sure these are all complete classes, including toString method. 3- Write a tester to test these classes and their methods, by creating an array or ArrayList of Person and...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT