This assignment gives you practice with writing functions whose bodies have assignment statements, conditional statements, and blocks.
You may work with one partner. If you are going to work with a partner, use the course management system to form a group BEFORE you submit the assignment. And we strongly suggest making the group at least three days before the assignment is due so that if you have problems, you can email us and get them solved. Contact Ashley Lin <acl20@cornell.edu> if you have problems.
This is (very, very roughly) a 10-hour assignment. Plan accordingly. Start early. Let's see how many people can subimit the proejct 3 days before the deadline.
Plan to spend as much as an hour reading this handout so that you thoroughly understand what we are asking for. Do this with your partner before you start programming!
We strongly, strongly suggest that you and your partner alternate writing the classes. You will both benefit from this, even if you find it feels like it is taking longer.
You must use the capitalization that we specify. You can ensure this by copying identifiers from this handout, rather than typing them yourself. You should also write precise specifications of your functions. The simplest way to do this is to copy-and-paste the specifications from this handout.
ProjectDates
You will create several functions in class ProjectDates
. For
each, you should write a short javadoc comment just before it that describes
what the method is does. As a reminder, javadoc comments look like these:
>/** This class contains only static functions. There is no need to instantiate it.*/
public class X {
/** Do ... (Write a COMMAND to do something --it is equivalent to a call on it*/
public void m() {
...
}/** =... (an English statement or formula that is equivalent to a function call on f*/
public int f() {
...
}
}
First, download file ProjectDates.java
from the Course
Management System and study its methods month, longerMonth, and
numberOfdays.
This assignment will be presented as a series of tasks. Finish each task completely before proceeding to the next. Test it carefully and thoroughly, using the Interactions Pane!
Here are the tasks:
This program does not take leap years into account. Introduce new functions longerMonth and numberOfDays so that they have the specifications shown below. Do not change the old functions! Define new ones. The, the functions names will be "overloaded". Write the specification of the function before writing the function! You can copy our specification.
For this project, define a leap year to be a year that is divisible by 4, and in a leap year, February has 29 days instead of 28.
/** =
the number of days in month m of year y
(Precondition:
1 <= m <= 12) */
public static int numberOfDays(int
m, int y)
/** =
the month (m or n) of year y) that has more days
(= m if they have the
same number of days).
Precondition
1 <= m <= 12, 1 <= n <= 12 */
public static int longerMonth(int
m, int n,
int y)
In the Interactions Pane, write calls on ProjectDates.numberOfDays to test it. Remember, it is a static method. You will need at least 24 test cases to do a thorough job: 12 different months in a nonleap year and 12 different months in a leap year. Test it at least 24 times!!
In the Interactions Pane, write calls on ProjectDates.numberOfDays to test it. Your test cases should ensure that each statement or expression in the body of longerMonth is executed (or evaluated) in at least one call on the functions. How many test cases do you need to check this method, given that numberOfDays is correct?
Write a function with the following specification.
/** =
the date for day d of month m of year y
For example, date(2,
3, 200) = "2 March 2000". Note that precisely one
blank separates
the day from the month and the month from the year.
Precondition: day d,
month m, year y is a valid date */
public static String date(int
d, int
m, int y)
Test the function thoroughly before proceeding to the next task.
Write function theMonth, which is specified below. Be sure to use calls on the functions of task 1 where appropriate. A good programmer uses already written and checked out methods to simplify later tasks. For example, the body of this function should NOT contain the constant 31 for the number of days in January, and it should NOT worry about leap years.
/** =
the month in which day d of year y occurs.
E.g. day 32 occurs
in month 2 (February) and
day 360 occurs in month
12 (December).
Precondition: 1 <=
d <= (no. of days in year y) */
public static int theMonth(int
d, int
y)
The body of theMonth will probably consist of repetitive code --11 or 12 copies of the same thing, with minor changes. The best way to do this is to get it working for January and February (only). Then, copy and paste to get it working for the rest of the months.
Here's a hint on writing this function. Use two local variables m1 and daysBefore with the following meaning:
m1 is a month,
daysBefore is
the number of days in the year that precede month m1,
and
daysBefore <
d
Truthify this relation initially with the assignments m1= 1; daysBefore= 0;. The idea is then to repeatedly inccrease m1--keeping the above meaning true-- until d <= daysBefore + (number of days in month m1). Then you know that d occurs in month m1.
This is a tough function to write, and you should test it thoroughly before proceeding to task 6.
Write function dayOfYear, specified below. For example, dayOfYear(2,2,2000) = 33.
/** =
the day number within the year of day d, month
m, year y.
Precond.: day d, month
m, year y is a valid date */
public static int dayOfYear(int
d, int m,
int y)
The body of dayOfYear will probably consist of repetitive code --11 or 12 copies of the same thing, with minor changes-- so copy-paste-change is a good way develop the body. Look at the hint for the previous function. Test your function thoroughly before going on to the next one.
Write a function with the following specification
/** =
day of the month in which day d of year y occurs.
E.g. theDay(32,2000)
= 1, since day 32 is 1 Feb., and theDay(366,2000) = 31.
Precondition: 1 <=
d <= (number of days in year y) */
public static int theDay(int
d, int
y)
This one can also be done with a sequence of repetitive code. However, it can be implemented using at most three statements, by relying on the results of previously written functions. Test the function throughly.
ProjectDates.java
. Remember
that, as always, the name of a file is derived from the class it contains
and that correct capitalization is required.Note: If you still need to be added to the CMS, email Ashley Lin <acl20@cornell.edu>.