Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package Week1;
- public class ProgrammingAssignment1 {
- public static void main(String[] args) {
- MonthDays getDay = new MonthDays();
- getDay.getNumberOfDays(1, 1010);
- }
- class MonthDays {
- int Month;
- int Year;
- public MonthDays(){
- }
- public int getMonth(){
- return Month;
- }
- public int getYear(){
- return Year;
- }
- public void setMonth(int theMonth){
- Month = theMonth;
- }
- public void setYear(int theYear){
- Year = theYear;
- }
- public MonthDays(int theMonth, int theYear){
- Month = theMonth;
- Year = theYear;
- }
- public int getNumberOfDays(int theMonth, int theYear){
- //Requirement 1 If the month argument is less than 1, or greater than 12 getNumberOfDays should throw a IllegalArgumentException exception.
- if (theMonth <= 0){
- throw new IllegalArgumentException("the Month cannot be smaller than one");
- }
- if (theMonth >=13){
- throw new IllegalArgumentException("the Month cannot be larger than thirteen");
- }
- if (theYear <= 0){
- throw new IllegalArgumentException("the Year cannot be smaller than one");
- }
- //For months with 31 days
- if (theMonth == 1 || theMonth == 3 || theMonth == 5 || theMonth == 7 || theMonth == 8 || theMonth == 10 || theMonth == 12){
- return 31;
- }
- //For months with 30 days
- else if (theMonth == 4 || theMonth == 6 || theMonth == 9 || theMonth == 11){
- //For the Feburary, detemine which is leap year or not
- return 30;
- }
- else if ((theYear % 100) == 0 && ((theYear % 400 ) == 0))
- {
- //This is leap year
- return 29;
- }
- else if ((theYear % 100) != 0 && (theYear % 4) == 0){
- //This is also leap year
- return 29;
- }
- else {
- //It is not leap year
- return 28;
- }
- }
- }
- class DaysSoFar {
- int Month;
- int Day;
- int Year;
- public DaysSoFar(int theMonth, int theDay, int theYear){
- Month = theMonth;
- Day = theDay;
- Year = theYear;
- }
- public int numberOfDaysSinceNewYear(int theMonth, int theDay, int theYear){
- return -1;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement