Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Write an algorithm for this assignment. No code in any programming language. 2) We have provided a main method in the Date class that tests

Write an algorithm for this assignment. No code in any programming language.

2) We have provided a main method in the Date class that tests some of your methods. You are welcome to modify the main method as you please, perhaps to add further tests of your own. We will not be grading the main method in this assignment. (It does, of course, need to compile.)

3) You are welcome to add new methods to the Date class. Since they will presumably be "helping" methods, declare them "private", not "public".

4) Do not not change the prototype (interface) of any method. If you change the arguments or the return type, or you change a method from static to non-static, your program will not compile with our test cases, and will not receive credit.

5) Do not have any extraneous print statements in your program, including error messages. Your program should print out exactly what is specified and nothing else. (If the comment prefixing a method does not mention printing, the method should not print anything.) The only exception here is the main method, which can do anything you like.

Your job is to fill in the implementations of the methods.

We have specified most or all of the methods you'll need, including some helper methods.

Part I

Implement the basic helper methods listed below. These methods, like the main method, are declared "static." They are also declared "public" so we can test them from another class. February contains 28 days most years, but 29 days during a leap year. A leap year is any year divisible by 4, except that a year divisible by 100 is not a leap year, except that a year divisible by 400 is a leap year after all. Hence, 1800 and 1900 are not leap years, but 1600 and 2000 are. (Implement this rule in your program even if you know information to the contrary.)

/** Checks whether the given year is a leap year. * Return true if and only if the input year is a leap year. */ public static boolean isLeapYear(int year) { ... } /** Returns the number of days in a given month. * Parameter month is a month, numbered in the range 1...12. * Parameter year is the year in question. */ public static int daysInMonth(int month, int year) { ... } /** Checks whether the given date is valid. * Return true if and only if month/day/year constitute a valid date. * Years prior to A.D. 1 are NOT valid. */ public static boolean isValidDate(int month, int day, int year) { ... } 

Part II

Define the internal state that a "Date" object needs to have by declaring some data fields (all private) within the Date class. Define the basic constructor specified below. If a caller attempts to construct an invalid date, the program should construct a date of "0/0/0".

/** Constructs a date with the given month, day and year. If the date is * not valid, the entire program will halt with an error message. * Parameter month is a month, numbered in the range 1...12. * Parameter day is between 1 and the number of days in the given month. * Parameter year is the year in question, with no digits omitted. */ public Date(int month, int day, int year) { ... } /** Returns a string representation of this date in the form month/day/year. * The month, day, and year are printed in full as integers; for example, * 12/7/2006 or 3/21/407. * Return a String representation of this date. */ public String toString() { ... } 

Part III

Implement the following methods:

/** Determines whether this Date is before the Date d. * Return true if and only if this Date is before d. */ public boolean isBefore(Date d) { ... } /** Determines whether this Date is after the Date d. * Return true if and only if this Date is after d. */ public boolean isAfter(Date d) { ... } /** Returns the number of this Date in the year. * Return a number n in the range 1...366, inclusive, such that this Date * is the nth day of its year. (366 is only used for December 31 in a leap * year.) */ public int dayInYear() { ... } /** Determines the difference in days between d and this Date. For example, * if this Date is 12/15/1997 and d is 12/14/1997, the difference is 1. * Return the difference in days between d and this date. */ public int difference(Date d) { ... } 

Hint 1: Once you've implemented isBefore(), it's possible to implement isAfter() with just one line of code. You need to think carefully, though: "return !isBefore(d)" is incorrect. Can you see why?

Hint 2: All the methods in the Date class can read all the private fields in _any_ Date object (not just "this" Date object).

Part IV

Implement the final missing piece of your class, a second constructor that takes a String argument.

/** Constructs a Date object corresponding to the given string. * Parameter s should be a string of the form "month/day/year". If s is not * a valid date, the program should construct a date of (0, 0, 0). */ public Date(String s) { ... }

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

PC Magazine Guide To Client Server Databases

Authors: Joe Salemi

1st Edition

156276070X, 978-1562760700

More Books

Students also viewed these Databases questions

Question

LO3 Name the seven categories of HR functions.

Answered: 1 week ago