Question

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 you!

import java.util.Scanner; public class IC07_HackerChallenge { public static void main (String[] args) { new Scanner (System.ino days break 31 = } if (year >= 1000) { if (no_days > day) System.out.println(The date you entered is valid!); else System

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

Below I've modified your code for checking leap year, I have not changed your logic.

//filename: IC07_HackerChallenge.java

package test;

import java.util.Scanner;

public class IC07_HackerChallenge {

   public static void main(String[] args) {
       Scanner kb = new Scanner(System.in);
           System.out.println("Please enter date in form MM/DD/YYYY");
           String inputDate = kb.nextLine();
           String monthPart = inputDate.substring(0, 2);
           int month = Integer.parseInt(monthPart);
           String dayPart = inputDate.substring(3, 5);
           int day = Integer.parseInt(dayPart);
           String yearPart = inputDate.substring(6);
           int year = Integer.parseInt(yearPart);
           int no_days = 0;
           System.out.println();
           if(month >= 1 && month <=12) {
               switch(month) {
               case 4:
               case 6:
               case 9:
               case 11:
                   no_days = 30;
                   break;
               case 2:
                   // leap year if divisible by 4 but not divisible by 100, unless it is also divisible by 400
                   if((year%4 == 0 && year%100 != 0) || year%400 == 0)
                       no_days = 29;
                   else
                       no_days = 28;
                   break;
               case 1:
               case 3:
               case 5:
               case 7:
               case 8:
               case 10:
               case 12:
                   no_days = 31;
                   break;
               }
               if (year >= 1000) {
                   if(no_days >= day && day >= 1)
                       System.out.println("The date you entered is valid!");              
                   else
                       System.out.println("Invalid day");
               } else {
                   System.out.println("Invalid year");
               }
           } else {
               System.out.println("Invalid month");
           }  
           kb.close();
   }
}

//Give me an upvote if you like the answer, thanks

Add a comment
Know the answer?
Add Answer to:
Extend this date validation program, so that it checks for valid leap years (in which the...
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
  • 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...

  • PLEASE WRITE CODE FOR C++ ! Time Validation, Leap Year and Money Growth PartA: Validate Day and Time Write a program tha...

    PLEASE WRITE CODE FOR C++ ! Time Validation, Leap Year and Money Growth PartA: Validate Day and Time Write a program that reads a values for hour and minute from the input test file timeData.txt . The data read for valid hour and valid minute entries. Assume you are working with a 24 hour time format. Your program should use functions called validateHour and validateMinutes. Below is a sample of the format to use for your output file  timeValidation.txt :   ...

  • 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...

  • In the following program, declare an integer array daysInMonth that stores the number of days in January, February, March, April, and so on

    # JAVA In the following program, declare an integer array daysInMonth that stores the number of days in January, February, March, April, and so on. Fill it with the appropriate values (31, 28, 31, 30, ...).The program reads a month and year from the user.When the year is a leap year, change the entry for February to 29.Print out how many days the given month has.CODE:import java.util.Scanner;public class NumberOfDays{   public static void main(String[] args)   {      // Declare and initialize daysOfMonth      ....

  • JAVA programing Question 1 (Date Class):                                   &nbsp

    JAVA programing Question 1 (Date Class):                                                                                                     5 Points Create a class Date with the day, month and year fields (attributes). Provide constructors that: A constructor that takes three input arguments to initialize the day, month and year fields. Note 1: Make sure that the initialization values for the day, month and year fields are valid where the day must be between 1 and 31 inclusive, the month between 1 and 12 inclusive and the year a positive number. Note 2:...

  • 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 )...

  • In Java You are to write a program that determines the day of the week for...

    In Java You are to write a program that determines the day of the week for New Year's Day in the year 3000. To do this, you must create your own date class (MyDate) and use the following interface and main program: /** Interface for Date objects to be used by the Year3000 driver program. @author Jon Sorenson */ public interface DateInterface { /** @return the day of the month (1-31) */ public int getDay(); /** @return the day of...

  • An ordinal date consists of a year and a day within it, both of which are...

    An ordinal date consists of a year and a day within it, both of which are integers. The year can be any year in the Gregorian calendar while the day within the year ranges from one, which represents January 1, through to 365 (or 366 if the year is a leap year) which represents December 31. Ordinal dates are convenient when computing differences between dates that are a specific number of days (rather than months). For example, ordinal dates can...

  • 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;...

  • JAVA CODE Beginner. please use if, else if or switch statements, but don't use arrays, please  ...

    JAVA CODE Beginner. please use if, else if or switch statements, but don't use arrays, please   Write a program that reads a string from the keyboard and tests whether it contains a valid date. Display the date and a message that indicates whether it is valid. If it is not valid, also display a message explaining why it is not valid. The input date will have the format mm/dd/yyyy. A valid month value mm must be from 1 to 12...

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