Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Python code The passage of time is measures in days, months, and years. In this assignment we are going to define a class Date to

Python code

The passage of time is measures in days, months, and years. In this assignment we are going to define a class Date to represent a calendar date.

All Date objects have the following data members:

  • month (an integer value between 1 and 12)
  • day (an integer value between 1 and 31)
  • year (a four-digit, non-negative integer)

For this class provide:

  • a constructor:
    • parameters for the constructor must be provided in the following order: month, day, and year.
    • use the default values technique so that if no data is provided for month, day, year the date is set to January 1, 1900.
  • inspectors/getters/accessors for all the data members. Use the get convention to name your getters.
  • a mutator/setter changeDate:
    • this setter modifies the Date object by adding a given amount of days.
    • this setter takes one parameter which is an integer that represents the number of days to add to a Date object.
    • Must be careful and make sure the resulting Date remains in the valid range. For example if you add 2 days to 01/31/2024 the resulting date should be 02/02/2024; and if you add 15 to 12/31/2024 the resulting date should be 01/15/2025
    • Note: Remember that January, March, May, July, August, October and December have 31 days. Assume February has always 28 days. April, June, September, and November have 30 days.

Then proceed to overload the following operators:

  • The __str__ special method, so the print function can be used with this class.
    • This method should return a Date object as a string in the format mm/dd/yyyy (for example 01/31/2024)
  • The comparison operators: ==, !=, <, <=, > and >=
    • These operators will allow us to compare two Date objects.
    • Note: a Date object is greater than another Date object if it's a later date. For example: 07/01/2024 is greater than 01/23/2024. Likewise 07/01/2024 is greater than 07/01/2020
  • The subscript operator ([]), as an accessor.
    • Accessing position 0 returns the value of the month data member.
    • Accessing position 1 returns the value of the day data member.
    • Accessing position 2 returns the value of the year data member.
    • For any other index value, raise an exception ("Index out of Bounds").

The code for the Date class should be saved in the date.py file.

In the main.py file you will have the main script for this assignment.

  • Must write a main script to make sure that your class and associated methods and operators work correctly.
  • At the very minimum you are expected to
    • create a few Date objects with the user providing data for the data members.
      • your main script should not accept invalid date for Date objects. For example 02/31/2009 is an invalid date because February only has 28 days.
      • for your convenience the module inputroutines.py is being provided to help with input validation.
    • demonstrate that all the getter and setter methods work correctly/
    • demonstrate that each one of the overloaded operators work:
      • Should explicitly compare two Date objects using the == != < <= > >=operators.
      • Should explicitly use the print() function to display a Date object.
      • Should explicitly use the subscript operator [ ] with a Date object to access each of the object data members.


Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image_2

Step: 3

blur-text-image_3

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Management Accounting Information for Decision-Making and Strategy Execution

Authors: Anthony A. Atkinson, Robert S. Kaplan, Ella Mae Matsumura, S. Mark Young

6th Edition

137024975, 978-0137024971

More Books

Students also viewed these Algorithms questions