Out lab 3
Due March 1rst
To Do
You are to write a program that takes in a date (take it in as a string from the user) and then prints out what day of the year it is. For example:
Give a date in the format xx/xx/xxxx (12/31/2005): <= program output
02/05/2005 <= user input
The date : 02/05/2005 is the 36 day of the year <= program output
The program should have the following methods in a DateCalculator class:
- Constructor
- A method to take the input from the user and validate that the input was given correctly.
- To validate you will need to use the string methods length and charAt.
- A method to parse the String to 3 integer values for day, month, year and validate that all numbers are within limits.
- To parse you will need to use the String method substring, and then to cast from a String to an int you will need to use the Integer.parseInt() method.
- After the variables are ints you can use an if with greaterThan and lessThan's to check the validity.
- A method to calculate which day of the year it is.
- Don't forget leap year, use the modulus to figure out if you need to add a day for leap year.
- Then check the month, add the days up to that month + the day of the current month to get your total.
- Then a method to print out the results.
Output
Here are five sample outputs of my program running. The first was improper format from the user, second as a bad day from the user, the third was an error because the user swapped the month and the day around, the fourth Worked perfectly, and the fifth didn't do the month in two digits.
Give a date in the format xx/xx/xxxx (12/31/2005):
12311996
Invalid Date, needs to be in the form xx/xx/xxxx
Give a date in the format xx/xx/xxxx (12/31/2005):
05/45/2005
Invalid day, must be between 1-31
Give a date in the format xx/xx/xxxx (12/31/2005):
31/12/1999
Invalid month, must be between 1-12
Give a date in the format xx/xx/xxxx (12/31/2005):
05/15/2005
The date : 05/15/2005 is the 135 day of the year
Give a date in the format xx/xx/xxxx (12/31/2005):
3/15/2006
Invalid Date, needs to be in the form xx/xx/xxxx
|
Hint:
My program also had a begin method, my main was only two lines long.
public static void main(String [] args)
{
DateInterp di = new DateInterp();
di.begin();
}
In my begin method I called each method I listed above and the methods that were checking for errors returned an int that interpreted the error. Here is my begin method:
public void begin()
{
int ok = getDate();
if(ok!=0){
ok = parseDate();
if(ok==4){
calculateDate();
printResults();
}
else if(ok==1) System.out.println("Invalid year");
else if(ok==2) System.out.println("Invalid month, must be between 1-12");
else System.out.println("Invalid day, must be between 1-31");
}
}
Assignment due the beginning of lab class March 1rst