Difference between revisions of "Days In A Month"
m (Replaced SolutionCode pipe characters with {{!}} template.) |
|||
Line 166: | Line 166: | ||
<pre> | <pre> | ||
//JAN //MAR //MAY //JUL //AUG //OCT //DEC | //JAN //MAR //MAY //JUL //AUG //OCT //DEC | ||
− | if (monthNum == 0 || monthNum == 2 || monthNum == 4 || monthNum == 6 || monthNum == 7 || monthNum == 9 || monthNum == 11) | + | if (monthNum == 0 || monthNum == 2 || monthNum == 4 || |
+ | monthNum == 6 || monthNum == 7 || monthNum == 9 || | ||
+ | monthNum == 11) | ||
{ | { | ||
if (monthNum == 0) | if (monthNum == 0) |
Revision as of 19:15, 9 April 2010
Back to the Program-A-Day homepage
ProblemWrite a Java program DaysInAMonth, that prompts for a month and year and determines the amount of days that will be in that month.
|
By Students...I remember coming into COMP 1010 having taken AP Computer Science in highschool. One thing to note is the KISS theory, Keep it Simple Stupid. In other words, don't over complicate the matter where even you yourself can't understand your own code looking over it.
| |
---|---|---|
SolutionStart by importing the swing java package. import javax.swing.*; Define your named constants and provide them with appropriate values, in this example there is one constant for 31 days one for 30 days and one for february. You can name them however you want but be sure to declare them as static constants since we are working in static main. Also note that it is declared outside of main, more often named constants are used throughout the java class where multiple functions can access it. final static int MONTH30 = 30; final static int FEB = 28; final static int MONTH31 = 31; Next define your local variables and initialize them to sentinel values for future error checking described later. We will need a year, monthNum and monthName as defined accordingly. int year, monthNum; String monthName; //sentinel values for error checking monthNum = -1; year = -1; monthName = ""; Capturing the user input using JOptionPane. We will need to use Integer.parseInt to cast the string results to integers for the month and year. monthNum = Integer.parseInt(JOptionPane.showInputDialog("Please enter the month [Jan(0) to Dec(11)]")); year = Integer.parseInt(JOptionPane.showInputDialog("Please enter the year")); Now we have to break up the problem into a series of cases and separate the conditions into if statements. The first 3 if statements is for error checking and validate the user input to make sure valid numbers were entered for month and year. First case, if user left both month and year blank. if (monthNum == -1 && year == -1) System.out.println("Please try again, there was an error with the month or the year you have entered"); Second case, if user entered an invalid monthNumber like a negative month number else if (monthNum <= -1) { System.out.println("Please try again, there was an error with the month you have entered"); } Third case, if user entered an invalid year, i.e. negative or in this problem we assume year 1000 is the first year else if (year <= 999) { System.out.println("Please try again, there was an error with the month you have entered"); } The last case, which is most important since it is processing the valid input will be the next step. In this case there is a set of nested cases. Again, we will have to separate the conditions into statements. In this problem we separated the months by 31 days, 30 days, and february as a case of its own for calculating leap years. Months with 31 days, since we are assuming Jan = 0, the condition will "or" all integer representations of the months with 31 days. Remember July and August are back to back months with 31 days! The first condition checks for the appropriate monthNum and the nested conditions will assign the appropriate monthName depending on which integer was chosen. There may be alternate solutions and methods to solve this problem but for the sake of example and topics to cover we have chosen this method. Lastly, System.out.println prints an meaningful output message. //JAN //MAR //MAY //JUL //AUG //OCT //DEC if (monthNum == 0 || monthNum == 2 || monthNum == 4 || monthNum == 6 || monthNum == 7 || monthNum == 9 || monthNum == 11) { if (monthNum == 0) monthName = "January"; else if (monthNum == 2) monthName = "March"; else if (monthNum == 4) monthName = "May"; else if (monthNum == 6) monthName = "July"; else if (monthNum == 7) monthName = "August"; else if (monthNum == 9) monthName = "October"; else if (monthNum == 11) monthName = "December"; System.out.println(monthName + " " + year + " has " + MONTH31 + " days."); } February case checks to see if monthNum = 1 but for the nested conditions, it checks if the year is divisible by 4 by using the mod operator. If it is in fact a leap year, 1 is added to the constant. //FEB else if (monthNum == 1) { monthName = "February"; //check for leap if (year % 4 == 0) System.out.println(monthName + " " + year + " has " + (FEB + 1) + " days."); else System.out.println(monthName + " " + year + " has " + FEB + " days."); } Months with 30 days are the last group of valid inputs to check. Similar to the months with 31 days, the first condition checks for an appropriate monthNum and the nested condition assigns the appropriate monthName. //APR //JUN //SEP //NOV else if (monthNum == 3 || monthNum == 5 || monthNum == 8 || monthNum == 10) { if (monthNum == 3) monthName = "April"; else if (monthNum == 5) monthName = "June"; else if (monthNum == 8) monthName = "September"; else if (monthNum == 10) monthName = "November"; System.out.println(monthName + " " + year + " has " + MONTH30 + " days."); } Lastly, for additional robustness, you can check if the user enters a monthNum that is too high. Remember, initially we checked for negative monthNum, but this time is now the opposite end of the spectrum where the monthNum is too beyond the scope. If the monthNum is too high output a meaningful error message. //number is larger than 11 else System.out.println("Invalid month number entered, choose between 0 - 11 (Jan - Dec)"); This concludes the program, compile and run. | ||
Code |