1answer.
Ask question
Login Signup
Ask question
All categories
  • English
  • Mathematics
  • Social Studies
  • Business
  • History
  • Health
  • Geography
  • Biology
  • Physics
  • Chemistry
  • Computers and Technology
  • Arts
  • World Languages
  • Spanish
  • French
  • German
  • Advanced Placement (AP)
  • SAT
  • Medicine
  • Law
  • Engineering
den301095 [7]
3 years ago
13

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"
Computers and Technology
1 answer:
Rina8888 [55]3 years ago
4 0

Answer:

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<=31 && Y>=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{

if(1>=M || M>=12) System.out.println(M+" is not a valid month");

if(1>=D || D>=31) System.out.println(D+" is not a valid date");

if(Y<1) System.out.println("year can not be negative");

}

}

}

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.

You might be interested in
A leading global vendor of computer software, hardware for computer, mobile and gaming systems, and
V125BC [204]

Answer:

Microsoft

Explanation:

5 0
2 years ago
Write a program that prompts the user to enter the number of integer numbers you need to enter, then ask user to enter these int
nadya68 [22]

Answer:

import java.util.Scanner;

public class Main

{

public static void main(String[] args) {

 Scanner ob = new Scanner(System.in);

 System.out.println("How many numbers? ");

 int n = ob.nextInt();

       int[] arr = new int[n];  

 for (int i=0; i<n; i++) {

     System.out.print("Enter the number: ");

     arr[i] = ob.nextInt();

 }

    System.out.println(indexOfLargestElement(arr));

}

public static int indexOfLargestElement(int[] arr) {

    int max = arr[0];

    int maxIndex = 0;

    for (int i=0; i<arr.length; i++) {

        if (arr[i] >= max) {

            max = arr[i];

            maxIndex = i;

        }

    }

    return maxIndex;

}

}

Explanation:

Create a method called indexOfLargestElement that takes one parameter, an array

Initialize the max and maxIndex

Create a for loop iterates throgh the array

Check each element and find the maximum and its index

Return the index

Inside the main:

Ask the user to enter how many numbers they want to put in array

Get the numbers using a for loop and put them inside the array

Call the indexOfLargestElement method to find the index of the largest element in the array

8 0
3 years ago
What lets you do many things, like write book reports and stories?
Shalnov [3]

Answer:

4. application programs

Explanation:

5 0
3 years ago
A good first step to understanding any kind of text is to :
Vitek1552 [10]

Answer:

find the main idea

Explanation:

6 0
2 years ago
The seven basic internal components found in a computer tower
Minchanka [31]
Motherboard, CPU, RAM, PSU, HDDs, GPU, and a Sound card.
8 0
3 years ago
Other questions:
  • The set of instructions that directs the computer to perform a variety of tasks is known as a
    9·1 answer
  • Two threads try to acquire the same resource at the same time and both are blocked. Then, they continually change their states i
    14·1 answer
  • design a relational database in EER for bike helmets and their reviews. a bike helmet has a name and color attributes. a bike co
    12·1 answer
  • The __________ operator increases the value of the variable by 1 after the original value is used in the expression in which the
    7·1 answer
  • Anyone can help me please ?
    7·1 answer
  • Es costoso construir un robot
    6·1 answer
  • Function of pons for class 7​
    15·1 answer
  • One of 34 possible _________________ can be assigned to each axis of classification in the seven-character code.
    12·1 answer
  • Plagiarism is considered
    6·1 answer
  • Which language should you use to add functionality to web pages.
    10·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!