04: Classes, Objects, Methods
Submit this homework using Gradescope. You can type up your answers, or write legibly and scan them. Do not attempt to submit a paper copy in class, as it will not be accepted.
A. (1 point) Write a minimal definition for a class named MonthDay
, about which you know nothing else (that is, don’t use the information in the following questions in this answer). Your answer to this question should be just the minimal definition – it will be really short!
B. (2 points) Add a constructor for MonthDay
that takes a month and day (of the appropriate primitive type), and stores these values in appropriately-named instance variables.
If the month or day is obviously invalid, then your constructor should throw an IllegalArgumentException
. Do not exhaustively check correctness. Instead, limit your checks to checking for a day greater than zero, and a month between 1 and 12 inclusive.
Your answer should be the entire class definition.
C. (1 point) Write a public toString()
method with an appropriate signature that converts the MonthDay
object to a String representation as exemplified by "31 January"
or "4 March"
.
Your answer should be just the toString()
method.