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

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...
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.
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...
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...
uppose you have a pre-existing class Dieter that models a person trying to monitor diet and...
uppose you have a pre-existing class Dieter that models a person trying to monitor diet and weight to improve his/her Body Mass Index (BMI) rating. The class has the following data and behavior: Field/Constructor/Method Description private String name the dieter's full name, such as "John Smith" private int height the dieter's height, in inches private int weight the dieter's weight, in pounds public Dieter(String name, int h, int w) makes a dieter with given name, height, weight public String getName()...
(JAVA) Create three classes, Vehicle, Truck, and Person. The Vehicle class has a make and model,...
(JAVA) Create three classes, Vehicle, Truck, and Person. The Vehicle class has a make and model, test emissions per mile, and a driver/owner (= Person object). (Each vehicle can only have one driver/owner.) The class Truck is a derived class from the Vehicle class and has additional properties load capacity in tons (type double since it can contain a fractional part) and a towing capacity in pounds (type int). Be sure that your classes have appropriate constructors, accessors, mutators, equals(),...
Write a Java program such that it consists of 2 classes: 1. a class that serves...
Write a Java program such that it consists of 2 classes: 1. a class that serves as the driver (contains main()) 2. a class that contains multiple private methods that compute and display a. area of a triangle (need base and height) b area of a circle (use named constant for PI) (need radius) c. area of rectangle (width and length) d. area of a square (side) e. surface area of a solid cylinder (height and radius of base) N.B....
Write these java classes: 1) DynArray.java: a class that models some of the functionality of the...
Write these java classes: 1) DynArray.java: a class that models some of the functionality of the Java ArrayList. This class is not complete and must be modified as such: Write the method body for the default constructor Write the method body for the methods: arraySize(), elements(), grow(), shrink(). The incomplete code is provided here: public class DynArray { private double[] array; private int size; private int nextIndex;    public int arraySize() { }    public int elements() { } public...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT