menu
bartleby
search
close search
Hit Return to see all results

Ask the user to enter the month of the year (number 1 through 12): “Enter the month of the year - number 1 to 12”b. Get the month using a Scannerc. checks for invalid input (i.e. numbers that are not 1 – 12) and terminates the programusing System.exit(1)if input is invalidd. Prints the name of the month: “The month is XXXXX”Now, this time, write a class, NumberToMonthUsingSwitch, that does the same but uses the switch statement.Tips Use the switch statement along with case and break Use the System.exit(1) in the default to terminate the programSubmission Copy and paste your code Screen shot the console with user input of three example months (one invalid input, two anymonth)Sample RunEnter the month of the year - number 1 to 1277Invalid InputEnter the month of the year - number 1 to 122The month is FebruaryEnter the month of the year - number 1 to 1210The month is October

Question

Ask the user to enter the month of the year (number 1 through 12): “Enter the month of the year - number 1 to 12”
b. Get the month using a Scanner
c. checks for invalid input (i.e. numbers that are not 1 – 12) and terminates the program
using System.exit(1)if input is invalid
d. Prints the name of the month: “The month is XXXXX”
Now, this time, write a class, NumberToMonthUsingSwitch, that does the same but uses the switch statement.
Tips
 Use the switch statement along with case and break
 Use the System.exit(1) in the default to terminate the program
Submission
 Copy and paste your code
 Screen shot the console with user input of three example months (one invalid input, two any
month)
Sample Run
Enter the month of the year - number 1 to 12
77
Invalid Input
Enter the month of the year - number 1 to 12
2
The month is February
Enter the month of the year - number 1 to 12
10
The month is October

check_circleAnswer
Step 1

The algorithm for the program would be as below:

  1. Declare one int variable for accepting user input
  2. Prompt the user for the input
  3. Accept the user input into int data type
  4. Check the conditions as given
  5. Depending upon the condition print appropriate messages.

We use the Scanner class from the util package of java to accept user input.

It is important to note the following explanation about the program:

  1. For multi-line body under if-else condition it is necessary to include curly braces {} indicating the program block boundaries.
Step 2

The java program is given below:

import java.util.*;

public class NumberToMonthUsingSwitch{

     public static void main(String[] args) {

           int month;

           Scanner reader = new Scanner(System.in);

           System.out.println("Enter the month of the year - number 1 to 12:");

           month = reader.nextInt();

           if (month <1 || month > 12){

                System.out.println("Invalid input");

                System.exit(1);

           }else {

                switch (month){

                case 1:

                     System.out.println("The month is January");

                     break;

                case 2:

                     System.out.println("The month is February");

                     break;

                case 3:

                     System.out.println("The month is March");

                     break;

                case 4:

                     System.out.println("The month is April");

                     break;

                case 5:

                     System.out.println("The month is May");

                     break;

                case 6:

                     S...

Want to see the full answer?

See Solution

Check out a sample Q&A here.

Want to see this answer and more?

Our solutions are written by experts, many with advanced degrees, and available 24/7

See Solution
Tagged in

Engineering

Computer Engineering

Related Computer Engineering Q&A

Find answers to questions asked by student like you

Show more Q&A add
question_answer

Q: What are REST web services and what is the reason for its popularity?

A: REST stands for Representational State Transfer and is an architectural style for most web based app...

question_answer

Q: Identify the errors in the following program. 2 errorspublic class Test {public static void main(St...

A: The two errors in the given program are:The argument is passed in wrong order in the line nPrintln(5...

question_answer

Q: this question is from fundamentals of logic. related to boolean algebra. I am unable to decipher the...

A: Click to see the answer

question_answer

Q: What is the CPU time if the number of instructions executed by the program is 500 cycles per instruc...

A: CPU time:The formula for computing the CPU time is provided below:

question_answer

Q: Imagine you’d like a contractor to build a software application for you that operates like Windows C...

A: Since there are multiple questions and no specification as to which question needs to be solved, her...

question_answer

Q: Write a program called ListEvenNumbers wich prints the even numbers from 20 to 30 (including 30). an...

A: As there is no programming language mentioned, we are doing this program in java to print even numbe...

question_answer

Q: Hello, I am struggling to write a program in Python for a following problem: Write a file ( .py). In...

A: Solution:Given data,The python code to strips the first letters from all file names in a directory i...

question_answer

Q: rewrite the function F = (A ⊕ B) ⊕ C without the XOR operator, ⊕.

A: The XOR operator is defined as 1 ⊕ 1 = 0, 1 ⊕ 0 = 1, 0 ⊕ 1 = 1, and 0 ⊕ 0 = 0. As the output is true...

question_answer

Q: Which of the following code is used to check the web worker object is exists or not? if the web work...

A: The below given code snippet is used to check whether the web worker object is exist or not and if t...

Sorry about that. What wasn’t helpful?