Using the switch multiple-selection statement control statements to return the n
ID: 3796395 • Letter: U
Question
Using the switch multiple-selection statement control statements to return the number of days in a month. Each month will be represented by a corresponding integer (1 is January, 2 is February, ... 12 is December). There should be separate cases for months with 30 days (September, April, June, and November), months with 31 days (January, March, May, July, August, October, December) and February is its own case. There should be one text document that provides the problem-solving methodology. Each Step of the Engineering Problem Solving Process needs to be documented. I. (10%) Understand the Problem, II. (10%) Design, III. (70%) Implementation (Code), IV (10%) Test Plan.Explanation / Answer
import java.io.*;
import java.util.Scanner;
public class HelloWorld{
public static void main(String []args){
int month;
int year;
Scanner sc=new Scanner(System.in);
System.out.println("Enter a month(1-12): ");
month = sc.nextInt();
System.out.println("Enter a Year: ");
year = sc.nextInt();
switch(month)
{
case 1:
case 3:
case 5:
case 7:
case 8:
case 10:
case 12: System.out.println("31 days");
break;
case 4:
case 6:
case 9:
case 11: System.out.println("30 days");
break;
case 2:
if((year%400==0)||((year%100!=0)&&(year%4==0)))
{
System.out.println("29 days");
}
else
{
System.out.println("28 days");
}
break;
default: System.out.println("Invalid input! Please enter month number between 1-12");
}
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.