Question

Take your MonthDay class from Assignment #6 and modify it to throw an IllegalArgumentException when the user of the class attempts to pass an invalid value for month and year. Make sure you have your...

Take your MonthDay class from Assignment #6 and modify it to throw an IllegalArgumentException when the user of the class attempts to pass an invalid value for month and year. Make sure you have your main method(function) in a separate source file demonstrating use of the try/catch block to catch the IllegalArgumentException that the MonthDay class may throw in the event of an invalid month, or year.

class MonthDays {
   private int month;
   private int year;

   public MonthDays(int aMonth, int aYear) {
       setMonth(aMonth);
       setYear(aYear);
   }

   public int getYear() {
       return year;
   }

   public void setYear(int aYear) {
       year = aYear;
   }

   public int getMonth() {
       return month;
   }

   public void setMonth(int aMonth) {
       if (aMonth >= 1 && aMonth <= 12)
           month = aMonth;
   }

   public int getNumberOfDays() {
       int res = -1;
       int mm = month;
       int aYy = year;
       switch (mm) {
       case 1:
           res = 31;
           break;
       case 2:
           if (isLeap(aYy))
               res = 29;
           else
               res = 28;
           break;
       case 3:
           res = 31;
           break;
       case 4:
           res = 30;
           break;
       case 5:
           res = 31;
           break;
       case 6:
           res = 30;
           break;
       case 7:
           res = 31;
           break;
       case 8:
           res = 31;
           break;
       case 9:
           res = 30;
           break;
       case 10:
           res = 31;
           break;
       case 11:
           res = 30;
           break;
       case 12:
           res = 31;
           break;

       }
       return res;
   }

   private boolean isLeap(int year) {
       boolean leap = false;
       // if any year is divisable by 4 than there are many chances for leap
       // year except few
       if (year % 4 == 0) {
           // if it is divisable by 100 than it shoud also divisable by 400
           // like 2000 etc
           if (year % 100 == 0) {
               // year is divisible by 400, so the year is a leap year
               if (year % 400 == 0)
                   leap = true;
               else
                   leap = false;
           } else
               leap = true;
       } else
           leap = false;
       return leap;
   }

}

public class TestMothDays {
   public static void main(String[] args) {
       MonthDays m1 = new MonthDays(2, 2000);
       MonthDays m2 = new MonthDays(2, 2015);
       System.out.println(m1.getNumberOfDays());
       System.out.println(m2.getNumberOfDays());

   }
}

0 0
Add a comment Improve this question Transcribed image text
Answer #1

class MonthDays {

   private int month;

   private int year;

   public MonthDays(int aMonth, int aYear) {

   setMonth(aMonth);

   setYear(aYear);

   }

   public int getYear() {

   return year;

   }

   public void setYear(int aYear) {

if(aYear <= 0)

throw new IllegalArgumentException("Invalid Year");

   year = aYear;

   }

   public int getMonth() {

   return month;

   }

   public void setMonth(int aMonth) {

   if (aMonth >= 1 && aMonth <= 12)

   month = aMonth;

   else

   throw new IllegalArgumentException("Invalid Month");

   }

   public int getNumberOfDays() {

   int res = -1;

   int mm = month;

   int aYy = year;

   switch (mm) {

   case 1:

   res = 31;

   break;

   case 2:

   if (isLeap(aYy))

   res = 29;

   else

   res = 28;

   break;

   case 3:

   res = 31;

   break;

   case 4:

   res = 30;

   break;

   case 5:

   res = 31;

   break;

   case 6:

   res = 30;

   break;

   case 7:

   res = 31;

   break;

   case 8:

   res = 31;

   break;

   case 9:

   res = 30;

   break;

   case 10:

   res = 31;

   break;

   case 11:

   res = 30;

   break;

   case 12:

   res = 31;

   break;

   }

   return res;

   }

   private boolean isLeap(int year) {

   boolean leap = false;

   // if any year is divisable by 4 than there are many chances for leap

   // year except few

   if (year % 4 == 0) {

   // if it is divisable by 100 than it shoud also divisable by 400

   // like 2000 etc

   if (year % 100 == 0) {

   // year is divisible by 400, so the year is a leap year

   if (year % 400 == 0)

   leap = true;

   else

   leap = false;

   } else

   leap = true;

   } else

   leap = false;

   return leap;

   }

}

public class TestMothDays {

   public static void main(String[] args) {

   MonthDays m1 = new MonthDays(2, 2000);

   MonthDays m2 = new MonthDays(2, 2015);

   System.out.println(m1.getNumberOfDays());

   System.out.println(m2.getNumberOfDays());

   try {

   m1.setMonth(100);

   }catch(IllegalArgumentException e) {

   System.out.println(e.getMessage());

   }

   try {

   m1.setYear(-100);

   }catch(IllegalArgumentException e) {

   System.out.println(e.getMessage());

   }

   }

}

====================================================
SEE OUTPUT
103 public class TestMothDays i 104 public static void main(String[ args) f MonthDays m1-new MonthDays(2, 2000); MonthDays m2

Thanks, PLEASE COMMENT if there is any concern.

Add a comment
Know the answer?
Add Answer to:
Take your MonthDay class from Assignment #6 and modify it to throw an IllegalArgumentException when the user of the class attempts to pass an invalid value for month and year. Make sure you have your...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • public class Date { private int month; private int day; private int year; /** default constructor...

    public class Date { private int month; private int day; private int year; /** default constructor * sets month to 1, day to 1 and year to 2000 */ public Date( ) { setDate( 1, 1, 2000 ); } /** overloaded constructor * @param mm initial value for month * @param dd initial value for day * @param yyyy initial value for year * * passes parameters to setDate method */ public Date( int mm, int dd, int yyyy )...

  • Java Programming: Hi, I need help Modifying my code that will throw an IllegalArgumentException. for the...

    Java Programming: Hi, I need help Modifying my code that will throw an IllegalArgumentException. for the following data entry error conditions: A number less than 1 or greater than 12 has been entered for the month A negative integer has been entered for the year utilizes a try and catch clause to display an appropriate message when either of these data entry errors exceptions occur. Thank you let me know if you need more info import java.util.*; //Class definition public...

  • Programming Exercise 11-8 PROBLEM:The class dateType defined in Programming Exercise 6 prints the date in numerical...

    Programming Exercise 11-8 PROBLEM:The class dateType defined in Programming Exercise 6 prints the date in numerical form. Some applications might require the date to be printed in another form, such as March 24, 2019. Derive the class extDateType so that the date can be printed in either form. Add a member variable to the class extDateType so that the month can also be stored in string form. Add a member function to output the month in the string format, followed...

  • Need comments added please. public class TenDate {    private int NMnth;    private int NDy;...

    Need comments added please. public class TenDate {    private int NMnth;    private int NDy;    private int NYr;    private String [] MnthNm =        {"", "January", "February", "March", "April", "May", "June", "July",            "August", "September", "October", "November", "December"};    private int [] MnthD =        {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};    public TenDate()    {        NMnth = 1;        NDy = 1;...

  • Hello, I have a bug in my code, and when I run it should ask the...

    Hello, I have a bug in my code, and when I run it should ask the user to enter the month and then the year and then print out the calendar for the chosen month and year. My problem with my code is that when I run it and I enter the month, it doesn't ask me for the year and it prints all the years of the month I chose. Please help! Code: #include "calendarType.h" #include <iostream> using namespace...

  • Copy all the classes given in classes Calendar, Day, Month, & Year into BlueJ and then...

    Copy all the classes given in classes Calendar, Day, Month, & Year into BlueJ and then generate their documentation. Examine the documentation to see the logic used in creating each class. (Code given below) import java.util.ArrayList; import java.util.Iterator; class Calendar { private Year year; public Calendar() { year = new Year(); } public void printCalendar() { year.printCalendar(); } } class Year { private ArrayList<Month> months; private int number; public Year() { this(2013); } public Year(int number) { this.number = number;...

  • You are to create a class Appointment.java that will have the following: 5 Instance variables: private...

    You are to create a class Appointment.java that will have the following: 5 Instance variables: private String month private int day private int year private int hour private int minute 1 default constructor public Appointment() 1 non-default constructor that accepts arguments for all instance variables, your constructor must call the setter methods below to set the values of the instance variables public Appointment(String monthPassed, int dayPassed, int yearPassed, int hourPassed, int minutePassed) 5 setter methods (one for each instance variable)...

  • C++ problem 11-6 In Programming Exercise 2, the class dateType was designed and implemented to keep...

    C++ problem 11-6 In Programming Exercise 2, the class dateType was designed and implemented to keep track of a date, but it has very limited operations. Redefine the class dateType so that it can perform the following operations on a date, in addition to the operations already defined: Set the month. Set the day. Set the year. Return the month. Return the day. Return the year. Test whether the year is a leap year. Return the number of days in...

  • Extend this date validation program, so that it checks for valid leap years (in which the...

    Extend this date validation program, so that it checks for valid leap years (in which the month February has 29, instead of 28, days). A leap year is any year that is divisible by 4 but not divisible by 100, unless it is also divisible by 400. Do not allow February to have 29 days on years that are not leap years. **Please view both photos for the entire code and post new code with the leap year modification. Thank...

  • #include "stdafx.h" #include <iostream> using namespace std; class dateType {    private:        int dmonth;...

    #include "stdafx.h" #include <iostream> using namespace std; class dateType {    private:        int dmonth;        int dday;        int dyear;       public:       void setdate (int month, int day, int year);    int getday()const;    int getmonth()const;    int getyear()const;    int printdate()const;    bool isleapyear(int year);    dateType (int month=0, int day=0, int year=0); }; void dateType::setdate(int month, int day, int year) {    int numofdays;    if (year<=2008)    {    dyear=year;...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT