Write two scnr.nextInt statements to get input values into birthMonth and birthYear. Then write a statement to output the month, a slash, and the year. End with newline. The program will be tested with inputs 1 2000, and then with inputs 5 1950. Ex: If the input is 1 2000, the output is: 1/2000 Note: The input values come from user input, so be sure to use scnr.nextInt statements, as in birthMonth

Respuesta :

Answer:

The java program fulfilling all the requirements is given below.

import java.util.Scanner;

public class BirthDate {

   

   // variable to hold month

   static int birthMonth;

   

   // variable to hold month

   static int birthYear;

   

   public static void main(String args[]) {

   

   // object of Scanner created to enable user input  

     Scanner scnr = new Scanner(System.in);

     

     // user input taken for month of birth

     System.out.println("Enter the month of birth ");

     birthMonth = scnr.nextInt();

     

     // user input taken for year of birth

     System.out.println("Enter the year of birth ");

     birthYear = scnr.nextInt();

     

     // birth date is displayed in the mentioned format

     System.out.print("The birth date is " + birthMonth + "/" + birthYear);

     

     // ending with newline

     System.out.println("");

   }

}

OUTPUT

Enter the month of birth  

1

Enter the year of birth  

2000

The birth date is 1/2000

Enter the month of birth  

5

Enter the year of birth  

1950

The birth date is 5/1950

Explanation:

The above program is described below.

1 – The variables are declared static since they are used in main() method which is static. Both variables are declared integers as per the question.

2 – Program begins with taking user input for month of the birth followed by year of the birth.

3 – The user input is taken separately for both variables using scanner object, scnr, as per the question requirement.

4 – The birth date is displayed to the console output in the format mentioned in the question: month / year, month of birth followed by slash followed by year of birth. The display statement is written as System.out.print("").

5 – The program is tested using the set of values given in the question.

6 – The input for month and year is not tested for validity since this is not mentioned in the question.

7 – The program ends with newline given by System.out.println("").

8 – print() method displays the output. The println() displays output and inserts a new line after the output.