Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Overview of Program You will read dates from a file and identify which two dates have the fewest number of days between them and which

Overview of Program

You will read dates from a file and identify which two dates have the fewest number of days between them and which two dates have the largest number of days between them and output those pairs of dates and the number of days between each pair.

For this program, you will be provided with a Date class that you will use to store and process dates with. This should enable you to focus on the logic of the program itself rather than adding in details of storing and processing dates, also.

Format of the file

The file that you will read dates from will look like this:

5 1/1/1900 1/2/1900 12/5/1798 7/4/3001 8/1/2028

The first value of the file will be an int value indicating how many dates will be present in the file. The value could be any value from 2 and up. Each file will be guaranteed to have at least two dates in it.

Each date in the file will be on a line by itself and will be in the format of MM/DD/YYYY , where MM is the month, DD is the day, and YYYY is the year. Information about processing dates in this format is discussed later in this document.

For this program, all dates will be valid dates, but functionality is provided with the Date class to identify invalid dates.

There may be duplicate entries for the same date in the file. Dates can be in the range 1/1/1753 to 12/31/3000 .

What can objects of the Date class do? Objects of Date class have the following public methods available for you to use.

A Date object can store a date between 1/1/1753 and 12/31/3000 .

Please note that you do not need to write these methods; they are available for you to use in the Date class that is provided in the Date.class file in the directory you will copy to your account. You don't need to use any import statements or anything fancy to use the Date class. Just create a new Date object in your program using the constructor as described below and you can move forward from there.

Determining Closest and Furthest Dates Closest Dates

The closest dates will be determined by comparing each date to all of the other dates except for itself. The result may be 0 , but only if there are duplicate dates in the file.

Furthest Dates

The furthest dates will also be determined by comparing each date to all of the other dates except not itself. Given the storage range of dates in a Date object, the maximum number of days between dates could be 455821 .

Examples

See the sample runs at the end of the file for examples of different results.

Considerations for Entering and Validating Dates Entering Dates

Scanner get = new Scanner( System.in ); System.out.print("Enter your date: "); int x = get.nextInt(); int y = get.nextInt(); int z = get.nextInt();

Input to this code could be entered like this:

Enter your date: 11 27 2017

Java

or like this:

Enter your date: 11 27 2017

Either way, variable x would get a value of 11 , variable y would get a value of 27 and variable z would get a value of 2017 .

Changing Scanner behavior

In order to enter dates in MM/DD/YYYY format, using slashes, your code needs to behave differently. The easiest way to do this is to change the way the Scanner reads values from the keyboard. As mentioned in the last section, a Scanner uses whitespace as a delimiter between input by default. To change this behavior, you can use the .useDelimiter() method from the Scanner class. More information about the .useDelimiter() method can be found at http://bit.ly/scannerdelimiter.

To enable a Scanner to use, for instance, slashes / as delimiters in addition to spaces, you can use the following code in your program:

Java

Scanner get = new Scanner( System.in ); get.useDelimiter("[/\\s+]+"); System.out.print("Enter your date: "); int x = get.nextInt(); int y = get.nextInt(); int z = get.nextInt(); // notice two backslashes here!

Now, dates can be entered in any of the following ways:

Enter your date: 11 27 2017 Enter your date: 11 27 2017 Enter your date: 11/27/2017 Enter your date: 11 / 27 / 2017

The .useDelimiter() method changes the behavior of the Scanner by providing a list of items that can be seen as delimiters between pieces of information. [/\\s+]+ tells Java that it can use either a forward slash / or a whitespace character \s (such as space, return, etc) to identify different inputs.

The key is to apply the .useDelimiter() method to the Scanner you would like to change the behavior of. If you have a Scanner attached to a file, then you want to use the .useDelimiter() method on that Scanner .

Output

The output of your program should look like the following sample:

Enter filename to process: d1.txt The dates from the file are:

11/23/2015 11/24/2015 11/25/2015 11/26/2015 02/23/2016 11/28/2015 11/29/2015 11/30/2015 11/30/1955 12/02/2015

Of the 10 dates processed: Closest (1): 11/23/2015 and 11/24/2015 Furthest (22000): 02/23/2016 and 11/30/1955

Make sure to include the following items in the format seen above:

The program should output a list of the dates themselves, one per line. Output the number of dates that were read from the file. Then output the dates that are "closest" to each other, including the number of days between the two dates. Finally, output the dates that are the "furthest" from each other, including the number of days between the two dates.

Goals

Use a non-standard, predefined class to implement a solution to a program.

Testing Your Program

Check out the sample run at the end of the assignment as well as some other example data values to test.

Working With New Concepts / Developing This Program

Remember that you've already done file processing in a previous program. Go back to that program to refresh your memory about how to process files.

You will possibly want to store the dates you read from the file into an array. Working with arrays of objects is different from working with an array of primitives.

Notes

For the output, follow the format of the sample runs as closely as possible. The order of the dates for the closest and furthest dates can be in either order. Additionally, you must have a completed Collaboration document on file in order for the program to be graded.

Sample Run for file d2.txt

Enter filename to process: d2.txt The dates from the file are:

01/01/1753 11/27/2017

Of the 2 dates processed:

Closest (96754): 01/01/1753 and 11/27/2017 Furthest (96754): 11/27/2017 and 01/01/1753

image text in transcribed

image text in transcribed

image text in transcribed

please solve this without import.java.* of any kind

Please note that you do not need to write these methods; they are available for you to use in the Date class that is provided in the Date.class file in the directory you will copy to your account. You don't need to use any import statements or anything fancy to use the Date class. Just create a new Date object in your program using the constructor as described below and you can move forward from there Methoc Description and Example Constructor, used to create a new Date object. Defaults the date to 1/1/1753 Date0) Date d1 = new Date(); Specific constructor, used to create a new Date object with a given datee Date d2 = new Date(11,27,2017 Attempts to set the date to the given month m , day d, and year y. Returns true if the date was valid. Returns false and set setDate(int m, int d, int y) the date to 1/1/1753 if the attempted date was invalid

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

Advanced Database Systems

Authors: Carlo Zaniolo, Stefano Ceri, Christos Faloutsos, Richard T. Snodgrass, V.S. Subrahmanian, Roberto Zicari

1st Edition

155860443X, 978-1558604438

More Books

Students also viewed these Databases questions

Question

=+Are you legally able to work in the United States?

Answered: 1 week ago

Question

Differentiate the function. r(z) = 2-8 - 21/2 r'(z) =

Answered: 1 week ago

Question

What is the purpose of the Salary Structure Table?

Answered: 1 week ago

Question

What is the scope and use of a Job Family Table?

Answered: 1 week ago