Question
In Python: Define a class called Distance, for representing a distance (with one attribute that represents the distance in inches). The Distance class must have
In Python: Define a class called Distance, for representing a distance (with one attribute that represents the distance in inches). The Distance class must have a constructor that takes two optional numerical arguments, representing a number of inches and a number of feet. If there are no arguments, they are all 0. If there is only one argument, assume that this is the number of inches, and that the number of feet is 0. If there are two arguments, assume that the first is a number of inches and the second is a number of feet. Commands Distance(3,2) and Distance(27) should both create Distance objects with 27 inches. A .__repr__() method that returns a string that represents the given distance.
A method called .to_inches() that takes no extra arguments and returns the distance as a number of inches. A method called .to_feet() that takes no extra arguments and returns the distance as a number of feet. A method called .to_inches_and_feet() that takes no extra arguments and returns a tuple of two numbers representing the Distance in inches and feet (with the inches first).
A method called .__str__() which takes no extra arguments and returns a string in the format feet'inches". The .__str__() method is very similar to the .__repr__() method, only it is used to create strings that are meant to be displayed to the user instead of to another programmer. That's why the format is different. This method will automatically get used when you try to print() a Distanceobject.
Overload the == operator so that you can use it compare two Distance objects. So for example, Distance(3,1) == Distance(15) should return True. Overload the >, <, >=, and <= operators so that you can use them compare two Distance objects. So for example, Distance(3,1) >= Distance(15), Distance(10) > Distance(9), Distance(9) <= Distance(10) should all return True. You are expected to know what method(s) you'll need to define to do this. Overload the + operator so that you can use it add two Distance objects and get a new Distanceobject representing their sum.
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started