In: Computer Science
Write a complete Java Program to solve the following problem.
February 18 is a special date as this is the date that can be divisible by both 9 and 18
Write a program that asks the user for a numerical month and numerical day of the month and then determines whether that date occurs before, after, or on February 18.
If the date occurs before February 18, output the word Before. If the date occurs after February 18, output the word After. If the date is February 18, output the word Special.
Note: Passing the sample test cases are not enough to earn the full marks. You need to test your program for different months and dates to see whether your program will work in all the cases.
Input
The input consists of two integers each on a separate line.
These integers represent a date in 2015.
The first line will contain the month, which will be an integer in
the range from 1 (indicating January) to 12 (indicating
December).
The second line will contain the day of the month, which will be an
integer in the range from 1 to 31. You can assume that the day of
the month will be valid for the given month.
Output
Exactly one of Before, After or Special will be printed on one line.
Sample Input 1
1 7
Sample Output 1
Before
Sample Input 2
8 31
Sample Output 2
After
Sample Input 3
2 18
Sample Output 3
Special
Must be coded in java. Easy code for grade 11 class
Code :-
import java.util.Scanner;
class SpecialDay{
public static void main(String[] args){
Scanner sc=new Scanner(System.in);
int month=sc.nextInt();
int day=sc.nextInt();
if(month>=1 && month <=12){
if(month==2 && day==18){
System.out.println("Special");
}
else if(month<=2 && day<18){
System.out.println("Before");
}
else {
System.out.println("After");
}
}
}
}
--------------------------------------------------------------------------
Code with explanation :-
import java.util.Scanner;
class SpecialDay{
public static void main(String[] args){
Scanner sc=new Scanner(System.in);
int month=sc.nextInt();
int day=sc.nextInt();
if(month>=1 && month <=12){ //code executes only when the month is a valid input
if(month==2 && day==18){ // if month is 2 and day is 18 then its a special day
System.out.println("Special");
}
else if(month<=2 && day<18){ // if month is less than or equal to 2 and day is less than
//18 then its "Before" is printed.
System.out.println("Before");
}
else { //In all the other remaining cases output should be "After"
System.out.println("After");
}
}
//Note:- Code executes only when valid month is given
// Day is assumed to be correct to the respective month.
}
}
--------------------------------------------------------------------------
Output Screenshot :-
-----------------------------------------------------------------
Thank you.