In this lab, you add the input and output statements to a partially completed Java program. When completed, the user should be able to enter a year, a month, and a day to determine if the date is valid. Valid years are those that are greater than 0, valid months include the values 1 through 12, and valid days include the values 1 through 31.


Instructions


Notice that variables have been declared for you.


Write the simulated housekeeping() method that contains input statements to retrieve a year, a month, and a day from the user.


Add statements to the simulated housekeeping() method that convert the String representation of the year, month, and day to ints.

Respuesta :

Answer:

import javax.swing.JOptionPane;

public class BadDate {

  private static final int MINIMUM_YEAR = 0, MINIMUM_MONTH = 1, MAXIMUM_MONTH = 12, MINIMUM_DAY = 1, MAXIMUM_DAY = 31;

  private int year = 0;

  private int month = 0;

  private int day = 0;

  public void housekeeping() {

      String yearData = JOptionPane.showInputDialog("Enter year");

      String monthData = JOptionPane.showInputDialog("Enter month");

      String dayData = JOptionPane.showInputDialog("Enter day");

      this.year = Integer.parseInt(yearData);

      this.month = Integer.parseInt(monthData);

      this.day = Integer.parseInt(dayData);

  }

 

  public boolean isLeapYear(){

      if(((this.year % 400) == 0) || (((this.year % 4) == 0) && ((this.year % 100) != 0)))

          return true;

      else

          return false;

  }

  public void endOfJob() {

      boolean validDate = true;

     

      if((month < MINIMUM_MONTH) || (MAXIMUM_MONTH < month))

          validDate = false;

      else {

          if(((month == 1) || (month == 3) || (month == 5) || (month == 7) ||

                  (month == 8) || (month == 10) || (month == 12)) && ((day < 1) || (31 < day)))

              validDate = false;

         

          else if(((month == 4) || (month == 6) || (month == 9) || (month == 11)) && ((day < MINIMUM_DAY) || (30 < day)))

              validDate = false;

         

          else if(month == 2) {

              if(isLeapYear() && ((day < MINIMUM_DAY) || (29 < day)))

                  validDate = false;

              else if((day < MINIMUM_DAY) || (28 < day))

                  validDate = false;

          }

      }

     

      if (validDate) {

          JOptionPane.showMessageDialog(null, "Date: " + this.month + "/" + this.day + "/" + this.year);

          System.out.println(this.month + "/" + this.day + "/" + this.year + " is a valid date");

      } else {

          JOptionPane.showMessageDialog(null, this.month + "/" + this.day + "/" + this.year + " is an invalid date");

          System.out.println(this.month + "/" + this.day + "/" + this.year + " is an invalid date");

      }

  }

  public static void main(String args[]) {

     

      BadDate date = new BadDate();

      date.housekeeping();

      date.endOfJob();

  }

}

Explanation:

  • Take the year, month and day from user  as an input.
  • Check if the year of this date is a leap year inside the isLeapYear method.
  • Inside the housekeeping method , take the year, the month and day  to ensure that date is valid .
  • Display positive response by printing valid date if date is correct.
  • Otherwise alert user that an invalid date was entered.