Question: 1. a) Write a method called getSubstring that takes a String s and two integers i and j as input. The method should throw an
1. a) Write a method called getSubstring that takes a String s and two integers i and j as input. The method should throw an IllegalArgumentException (with an appropriate message) if the second integer is smaller than the first one. Otherwise, the method returns the String composed by all the characters from the input String s that are within the positions i and j, including both of them. For example,
getSubstring("strawberry", 0, 4) returns "straw"
getSubstring("cats and dogs", 3, 6) returns "s an"
getSubstring("monday", 2, 2) returns "n"
getSubstring("another string", 6, 3) throws an IllegalArgumentException.
getSubstring("more cats", -4, 3) naturally (i.e. you dont have to do anything about it) throws a StringIndexOutOfBoundsException.
b) Write the following methods: getDay(), getMonth(), getYear(). All the above methods take a String as input. You can assume that such String represents a date and it is properly formatted (see definition in the preamble). All three methods return an integer: getDay() returns the integer representing the day of the specified date, getMonth() the integer representing the month of the specified date, and getYear() the integer representing the year of the specified date. To get full marks, your methods must call getSubstring(). For example:
getDay("04/09/2034") returns 4.
getMonth("04/09/2034") returns 9.
getYear("04/09/2034") returns 2034.
c) Write a method isLeapYear() which takes one integer as input representing a year. You can assume that the input represents a valid year (i.e. it is a positive integer). The method returns true if the year is a leap year, false otherwise. Note that, a year is a leap year if it is divisible by 4, except for century years which must be divisible by 400 in order to be leap. For example 1988, 1992, 2000, 2096 are leap years and 1985, 2002, 2100 are not leap years.
d) Write a method getDaysInAMonth() which takes two integer as input, one representing a month and the other representing a year, respectively. You can assume that the inputs are valid (i.e. they are both positive integers and the one representing the month is greater than or equal to 1 and less than or equal to 12). Note that for the months, the number 1 represents January, 2 February, 3 March, and so on. The method returns the number of days in the given month. Remember that the month of February in a leap year has 29 days, instead of the usual 28. To get full marks your method must call isLeapYear(). For example:
getDaysInAMonth(4, 2018) returns 30.
getDaysInAMonth(2, 1992) returns 29.
getDaysInAMonth(1, 1853) returns 31.
e) Write a method dueDateHasPassed() which takes two Strings as input. You can assume that the two Strings correctly represent a date. The first input represents the current date, while the second one represents the due date. The method should return true if the due date represents a date that comes earlier or is equal to the current date. The method returns false otherwise. To get full marks your method must call the three methods, getDay(), getMonth(), and getYear() described above. Hint: when comparing two dates you should start by comparing the years, followed by the months, followed by the days. For example:
dueDateHasPassed("12/04/1998", "23/01/2002") returns false.
dueDateHasPassed("23/01/2002", "12/04/1998") returns true.
dueDateHasPassed("23/01/2002", "23/01/2002") returns true.
f) Write a method countDaysLeft() which takes two Strings as input. You can assume that the two Strings correctly represent a date. The first input represents the current date, while the second one represents the due date. The method returns an integer representing the number of days between the current date and the due date. If the due date has passed, the method should return 0, otherwise it should count exactly how many days are there between the current date and the due date. When doing so, it is easier to go through the all the years, then the months, and finally the days separating the current date to the due date. Note that years might have a different amount of days depending on whether or not they are leap years. More over, months too have different number of days depending on the actual month and whether or not it is part of a leap year. To get full marks your method must use all of the methods defined above beside getSubstring() (which you are free to use, but it is not necessary to do so). For example:
countDaysLeft("13/10/2018", "23/10/2018") returns 10.
countDaysLeft("13/10/2018", "08/05/2024") returns 2034.
countDaysLeft("13/10/2018", "15/09/2018") returns 0.
countDaysLeft("13/10/2018", "18/09/2019") returns 340.
countDaysLeft("13/10/2018", "26/02/2019") returns 136
g) Write a method displayCountdown() which takes one String as input that represents a due date. As always throughout this question, you can assume that the String is correctly formatted. The method should retrieve the current date using the method getCurrentDate() provided to you. It should then obtain the number of days left before the due date using the method countDaysLeft() described above. The method finally displays the following information:
Todays date (the current date).
The due date.
An encouraging message displaying the number of days left until the due date if such number is greater than 0. A message informing the user the due date has passed otherwise.
For example, if you ran your program on October 13th, 2018, then
displayCountdown("23/10/2018") should display
Today is: 13/10/2018
Due date: 23/10/2018
You have 10 days left. You can do it!
displayCountdown("15/09/2018") should display
Today is: 13/10/2018
Due date: 15/09/2018
The due date has passed! :( Better luck next time!
h) Set up the main method so that the program receives one input through command line argument. The input should be a String representing a date in its correct format. You can then call displayCountdown() with the appropriate input and see the countdown displayed by your method (as in the examples above).
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
