00:01
In this video, we are going to create a program that prompts the user to enter the month in year and displays the number of days in the month.
00:06
For instance, if the user entered month 2 in year 2012, the program would display february 2012 had 29 days.
00:14
If the user entered month 3 in year 2015, the program should display march 2015 had 31 days.
00:24
I've already set up the class as well as the main method within it.
00:27
I've also imported java .utal .scanner since we know we'll be taking user input.
00:30
So we can go ahead into the main method and create a new scanner object named input by calling scanner with the argument system .in.
00:53
After this we can prompt the user to enter a month and a year.
00:59
We can use system .0 .0 .print l .n.
01:24
At this point we want to save the month followed by the year, so we can use next int and create two variables, int, month equals input.
01:40
Next, int.
01:45
And in year equals input dot next in.
01:56
So before we get into a switch statement, we can initialize in days equals 31.
02:05
We can initialize it as 31 since most of the months have 31 days, so 7 out of 12.
02:13
Next, we can initialize a string to hold the respective month, so we don't have to initialize it every time in our cases of our switch statement.
02:32
Now, here we can either do an if -then -else statement or a switch statement.
02:37
This is mostly based on readability, but we're just going to create a switch statement that handles each month as well as the number of days in that month and even leave years for the month of february.
02:49
So to do a switch statement, we say switch, our variable month, and within we have case one.
03:18
So for case one, the month string is january.
03:20
The days in january are 31, so we don't have to change that value.
03:26
And we can say break and then go do case two.
03:41
Our month string here is february.
04:01
Here we'll put an if statement for the leap year, but we'll come back to this.
04:09
Let's just get our other cases in.
04:24
Case 3, our month's string is march.
04:40
And again, we're going to want to put a break statement to omit the execution of the next case in the case that it was 3, right? so we're going to have to do this after every case.
04:58
Case 4, our month string is april...