Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Note that throughout this question, you can assume that a String representing a date has the following format: dd/mm/yyyy. So, for instance, Strings such as

Note that throughout this question, you can assume that a String representing a date has the following

format: "dd/mm/yyyy". So, for instance, Strings such as "04/09/2034" and "30/01/1234" correctly

represent a date, while "03/23/2018" or "1998/09/10" do not. Throughout this question, when we

refer to a String that represents a date you can assume that such String is correctly formatted.

Note that you are free to write more methods if they help the design or readability of your code.

1a. A method to get a substring from a speci_ed String

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 _rst 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 don't have to do anything about it)

throws a StringIndexOutOfBoundsException.

1b. Three di_erent methods to extract day, month, and year from a String

Write the following methods:

_ getDay()

_ getMonth()

_ getYear()

Page 3

all the above methods take a String as input. You can assume that such String represents a date and

it is properly formatted (see de_nition in the preamble). All three methods return an integer: getDay()

returns the integer representing the day of the speci_ed date, getMonth() the integer representing the

month of the speci_ed date, and getYear() the integer representing the year of the speci_ed date. For

example:

_ getDay("04/09/2034") returns 4.

_ getMonth("04/09/2034") returns 9.

_ getYear("04/09/2034") returns 2034.

To get full marks, your methods must call getSubstring().

1c. A method to check if a year is a leap year

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.

1d. A method that returns the number of days in a month

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.1 For example:

_ getDaysInAMonth(4, 2018) returns 30.

_ getDaysInAMonth(2, 1992) returns 29.

_ getDaysInAMonth(1, 1853) returns 31.

To get full marks your method must call isLeapYear().

1e. A method to check whether the due date has passed

Write a method dueDateHasPassed() which takes two Strings as input. You can assume that the two

Strings correctly represent a date. The _rst 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.

1f. A method to count the number of days left before the due date

Write a method countDaysLeft() which takes two Strings as input. You can assume that the two

Strings correctly represent a date. The _rst input represents the current date, while the second one

1For reference, you can check the following link https://www.timeanddate.com/calendar/months/.

Page 4

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 _nally the days separating the

current date to the due date. Note that years might have a di_erent amount of days depending on

whether or not they are leap years. More over, months too have di_erent 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 de_ned 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.

1g. A method to diplay the countdown required

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 _nally displays the following information:

_ Today's 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("08/05/2024") should display

Today is: 13/10/2018

Due date: 08/05/2024

You have 2034 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!

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

Advances In Databases And Information Systems 23rd European Conference Adbis 2019 Bled Slovenia September 8 11 2019 Proceedings Lncs 11695

Authors: Tatjana Welzer ,Johann Eder ,Vili Podgorelec ,Aida Kamisalic Latific

1st Edition

3030287297, 978-3030287290

More Books

Students also viewed these Databases questions

Question

=+development and make the product, should you go ahead and do so?

Answered: 1 week ago