Ask Question

Write a program that reads in the following data, all entered on one line with at least one space separating them: a) an integer representing a month b) an integer representing the day of the month c) an integer representing a year 1. Check that the month is between 1 and 12. If it's not print an error message. 2. Day cannot be 0 or less nor can it be more than 31 for all months. Print an error message if these conditions occur. If day is in the range of 0 and 31, you will still need to check other conditions as noted below in 4. 3. Year cannot be less than 1. Print an error message if that occurs. If any one of these three conditions occurs, only one error message should print and no further code should be executed. If everything is good so far, then continue as below: 4. Check that day is correct for the month. Remember the poem "30 days has September, April, June and November, All the rest have 31 except February which has 28 but in a leap year has 29"

+4
Answers (1)
  1. 27 December, 20:13
    0
    import java. util. Scanner;

    public class DataConstraint {

    public static void main (String[] args) {

    System. out. print ("Enter month, day, year separated by spaces : ");

    Scanner sc=new Scanner (System. in);

    int M, D, Y;

    M=sc. nextInt ();

    D=sc. nextInt ();

    Y=sc. nextInt ();

    if (1<=M && M<=12 && 1<=D && D=1)

    {

    if (M==4 || M==6 ||M==9 || M==11) {

    if (D==31) {

    System. out. println ("month "+M+" can not have more than 30 days");

    System. exit (0);

    }

    }

    else if (M==2)

    {

    if ((Y%400==0) || (Y%4==0 && Y%100!=0)) {

    if (D>=30) {

    System. out. println ("month "+M+" cannot have "+D+" days");

    System. exit (0);

    }

    }

    else {

    if (D>=29) {

    System. out. println (Y+" is not a leap year, "+D+" is invalid");

    System. exit (0);

    }

    }

    }

    System. out. println (M+" "+D+" "+Y+" is a valid date");

    }

    else

    }

    }

    Explanation:

    Use a conditional statement to check the month is between 1 and 12. Check that the date is between 1 and 31 and year must be positive value. If no. of days are more than 29, then the year cannot be a leap year.
Know the Answer?
Not Sure About the Answer?
Find an answer to your question 👍 “Write a program that reads in the following data, all entered on one line with at least one space separating them: a) an integer ...” in 📗 Computers & Technology if the answers seem to be not correct or there’s no answer. Try a smart search to find answers to similar questions.
Search for Other Answers