Question

In: Computer Science

Python: What are the defintions of the three method below for a class Date? class Date(object):...

Python: What are the defintions of the three method below for a class Date?

class Date(object):
"Represents a Calendar date"
  
def __init__(self, day=0, month=0, year=0):
"Initialize"
pass


def __str__(self):
"Return a printable string representing the date: m/d/y"
pass
  

def before(self, other):
"Is this date before that?"

Solutions

Expert Solution

Please find the Python code for the following:

Code:

class Date(object):
"Represents a Calendar date"
Day=0
Month=0
Year=0
  
"Initialize"
def __init__(self, day=0, month=0, year=0):
self.Day=day
self.Month=month
self.Year=year
  
"Function to return a printable string representing the date: m/d/y"
def __str__(self):
  
return str(self.Month)+"/"+str(self.Day)+"/"+str(self.Year)
  
"Function to check - Is this date before that?"
def before(self, other):
#First compare the year, if it is greater than the other, return true
if self.Year>other.Year:
return True
#Else then return False which indicates- this date is not before that
elif self.Year<other.Year:
return False
#Both Year's matches, then check for other
else:
#Secondly compare the month and then return True or False
if self.Month>other.Month:
return True
elif self.Month<other.Month:
return False
#If both are equal, then check for the last parameter Day
else:
#return True, if the day is greater than the other day
if self.Day>other.Day:
return True
#Else returns false
else:
return False
  
  
#Validating the above class
d1=Date (12,1,2020)
print("m/d/y:",d1)
print(d1.before(Date(13,1,2020))) #It returns False
print(d1.before(Date(11,1,2020))) #It returns True

Please check the compiled program and its output for your reference:

Output:

(I believe that I made the code simple and understandable. If you still have any query, Feel free to drop me a comment)


Hope this Helps!!!
Please upvote as well, If you got the answer?
If not please comment, I will Help you with that...


Related Solutions

Python: How would I modify the class below that takes a string and returns an object...
Python: How would I modify the class below that takes a string and returns an object holding a valid NANP phone number. I am asked to filll in the three methods listed, but underfined, below: __str__(), area_code(), and normalize(). My task is to clean up differently formatted telephone numbers by removing punctuation, such as '(', '-', and the like, and removing and the country code (1) if present. I am asked to start by stripping non-digits, and then see if...
The assignment is to write a class called data. A Date object is intented to represent...
The assignment is to write a class called data. A Date object is intented to represent a particuar date's month, day and year. It should be represented as an int. -- write a method called earlier_date. The method should return True or False, depending on whether or not one date is earlier than another. Keep in mind that a method is called using the "dot" syntax. Therefore, assuming that d1 and d2 are Date objects, a valid method called to...
In Python What is a class? What is a property as it relates to a class?...
In Python What is a class? What is a property as it relates to a class? What is a method as it relates to a class? What is an object? How do you create an object? What is an instance variable versus a class level variable? What is a constructor? Is a constructor always required? Does it depend on the language? What is and what is the benefit of inheritance? What is and what is the benefit of encapsulation? What...
-What is object-oriented programming? -What is a class? -What is an object? -A contractor uses a...
-What is object-oriented programming? -What is a class? -What is an object? -A contractor uses a blueprint to build a set of identical houses. Are classes analogous to the blueprint or the houses? Explain. -What is a class diagram? How is it used in object-oriented programming? -What is an attribute in OOP? What is a data member? -What is a method in OOP? What is a member function? -What is the difference between private members and public members of a...
The Date Class This class will function similarly in spirit to the Date class in the...
The Date Class This class will function similarly in spirit to the Date class in the text, and behaves very much like a “standard” class should. It should store a month, day, and year, in addition to providing useful functions like date reporting (toString()), date setting (constructors and getters/setters), as well as some basic error detection (for example, all month values should be between 1-12, if represented as an integer). Start this by defining a new class called “Date” or...
Which of this method of class String is used to obtain a length of String object?...
Which of this method of class String is used to obtain a length of String object? What is the output of the below Java program with WHILE, BREAK and CONTINUE? int cnt=0; while(true) { if(cnt > 4)    break;    if(cnt==0) {     cnt++; continue; }   System.out.print(cnt + ",");   cnt++; } 1,2,3,4 Compiler error 0,1,2,3,4, 1,2,3,4,
(Using Date Class) The following UML Class Diagram describes the java Date class: java.util.Date +Date() +Date(elapseTime:...
(Using Date Class) The following UML Class Diagram describes the java Date class: java.util.Date +Date() +Date(elapseTime: long) +toString(): String +getTime(): long +setTime(elapseTime: long): void Constructs a Date object for the current time. Constructs a Date object for a given time in milliseconds elapsed since January 1, 1970, GMT. Returns a string representing the date and time. Returns the number of milliseconds since January 1, 1970, GMT. Sets a new elapse time in the object. The + sign indicates public modifer...
In Python There are three seating categories at a stadium. Class A seats cost $20, Class...
In Python There are three seating categories at a stadium. Class A seats cost $20, Class B seats cost $15, and Class C seats cost $10. Write a program that asks how many tickets for each class of seats were sold, then display the amount of income generated from ticket sales. \ your program MUST contain a main function, a calcIncome function, and a showIncome function. Your main function should get the number of seats sold for each category. The...
The Pool class of the multiprocessing Python module. There is a defined run method to perform...
The Pool class of the multiprocessing Python module. There is a defined run method to perform the tasks. Create a pool object of the Pool class of a specific number of CPUs your system has by passing a number of tasks you have. Start each task within the pool object by calling the map instance method, and pass the run function and the list of tasks as an argument.Hint: os.walk() generates the file names in a directory tree by walking...
1. A constructor is a special Class member method. It is automatically called when an object...
1. A constructor is a special Class member method. It is automatically called when an object of the class is created. It can also be called more than once after the object is created. 2. In Java, the new operator is used to create an instance/object of a class in the heap. 3. A reference variable is a memory location contains an address of an object stored in the stack. 4. Encapsulation and abstraction are both the pillars of Object-Oriented...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT