Difference between revisions of "Days In A Month"
(Added quotations to student quotes) |
|||
(2 intermediate revisions by one other user not shown) | |||
Line 19: | Line 19: | ||
<br> | <br> | ||
− | |SideSectionTitle= | + | |SideSectionTitle=...By Students |
− | |SideSection=I remember coming into COMP 1010 having taken AP Computer Science in high school. One thing to note is the KISS theory, Keep it Simple Stupid. In other words, don't over complicate the matter where even yourself can't understand your own code looking over it. <br><br>Back in my day it was a bit of a game to try and code the fewest lines as possible, I think now it is more about code readability and code cleanliness, the flow of logic. Does it make sense? Is it readable? Can someone easily follow the thought process? <br><br>You also don't realize how important and how much you will appreciate comments until you have to debug code that has not been commented or that you haven't touched in a while. I used to be lazy to comment one day I was at work and I had to fix a bug in a program that I wrote. It was quite the adventure trying to remember what I was thinking back when I wrote it to which I am now appreciative of comments that people write including my own. Computers are fast these days; one core, two core, three core, and more, and number of lines is a story of the past. | + | |SideSection="I remember coming into COMP 1010 having taken AP Computer Science in high school. One thing to note is the KISS theory, Keep it Simple Stupid. In other words, don't over complicate the matter where even yourself can't understand your own code looking over it. <br><br>Back in my day it was a bit of a game to try and code the fewest lines as possible, I think now it is more about code readability and code cleanliness, the flow of logic. Does it make sense? Is it readable? Can someone easily follow the thought process? <br><br>You also don't realize how important and how much you will appreciate comments until you have to debug code that has not been commented or that you haven't touched in a while. I used to be lazy to comment one day I was at work and I had to fix a bug in a program that I wrote. It was quite the adventure trying to remember what I was thinking back when I wrote it to which I am now appreciative of comments that people write including my own. Computers are fast these days; one core, two core, three core, and more, and number of lines is a story of the past." |
<BR> | <BR> | ||
Line 92: | Line 92: | ||
{{OutputBlock | {{OutputBlock | ||
|Code= | |Code= | ||
− | Please enter the month [Jan(1) to Dec(12)]: | + | Please enter the month [Jan(1) to Dec(12)]: 12 |
Please enter the Year: asdf | Please enter the Year: asdf | ||
Invalid Input of Year | Invalid Input of Year | ||
Line 103: | Line 103: | ||
{{CodeBlock | {{CodeBlock | ||
|Code= | |Code= | ||
− | if (monthNum = | + | if (monthNum <= 0 && year <= 999) |
− | System.out.println("Please try again, there was an error with the month | + | System.out.println("Please try again, there was an error with the month and the year you have entered"); |
}} | }} | ||
{{OutputBlock | {{OutputBlock | ||
|Code= | |Code= | ||
− | Please enter the month [Jan(1) to Dec(12)]: | + | Please enter the month [Jan(1) to Dec(12)]: -1 |
− | Please enter the Year: | + | Please enter the Year: 987 |
− | + | Please try again, there was an error with the month and the year you have entered | |
}} | }} | ||
Line 117: | Line 117: | ||
{{CodeBlock | {{CodeBlock | ||
|Code= | |Code= | ||
− | else if (monthNum <= | + | else if (monthNum <= 0) |
{ | { | ||
System.out.println("Please try again, there was an error with the month you have entered"); | System.out.println("Please try again, there was an error with the month you have entered"); | ||
Line 124: | Line 124: | ||
{{OutputBlock | {{OutputBlock | ||
|Code= | |Code= | ||
− | Please enter the month [Jan(1) to Dec(12)]: | + | Please enter the month [Jan(1) to Dec(12)]: -1 |
− | Please enter the Year: | + | Please enter the Year: 2009 |
− | + | Please try again, there was an error with the month you have entered | |
}} | }} | ||
Line 140: | Line 140: | ||
{{OutputBlock | {{OutputBlock | ||
|Code= | |Code= | ||
− | Please enter the month [Jan(1) to Dec(12)]: | + | Please enter the month [Jan(1) to Dec(12)]: 12 |
− | Please enter the Year: | + | Please enter the Year: 987 |
− | + | Please try again, there was an error with the month you have entered | |
}} | }} | ||
Line 150: | Line 150: | ||
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. | 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. | ||
− | + | {{CodeBlock | |
+ | |Code= | ||
//JAN //MAR //MAY //JUL //AUG //OCT //DEC | //JAN //MAR //MAY //JUL //AUG //OCT //DEC | ||
− | if (monthNum == | + | if (monthNum == 1 {{!}}{{!}} monthNum == 3 {{!}}{{!}} monthNum == 5 {{!}}{{!}} |
− | monthNum == | + | monthNum == 7 {{!}}{{!}} monthNum == 8 {{!}}{{!}} monthNum == 10 {{!}}{{!}} |
− | monthNum == | + | monthNum == 12) |
{ | { | ||
− | if (monthNum == | + | if (monthNum == 1) |
monthName = "January"; | monthName = "January"; | ||
− | else if (monthNum == | + | else if (monthNum == 3) |
monthName = "March"; | monthName = "March"; | ||
− | else if (monthNum == | + | else if (monthNum == 5) |
monthName = "May"; | monthName = "May"; | ||
− | |||
− | |||
else if (monthNum == 7) | else if (monthNum == 7) | ||
+ | monthName = "July"; | ||
+ | else if (monthNum == 8) | ||
monthName = "August"; | monthName = "August"; | ||
− | else if (monthNum == | + | else if (monthNum == 10) |
monthName = "October"; | monthName = "October"; | ||
− | else if (monthNum == | + | else if (monthNum == 12) |
monthName = "December"; | monthName = "December"; | ||
System.out.println(monthName + " " + year + " has " + MONTH31 + " days."); | System.out.println(monthName + " " + year + " has " + MONTH31 + " days."); | ||
} | } | ||
− | + | }} | |
+ | |||
+ | {{OutputBlock | ||
+ | |Code= | ||
+ | Please enter the month [Jan(1) to Dec(12)]: 3 | ||
+ | Please enter the Year: 2009 | ||
+ | March 2009 has 31 days. | ||
+ | }} | ||
+ | |||
The case of February checks 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. | The case of February checks 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. | ||
− | + | {{CodeBlock | |
+ | |Code= | ||
//FEB | //FEB | ||
− | else if (monthNum == | + | else if (monthNum == 2) |
{ | { | ||
monthName = "February"; | monthName = "February"; | ||
Line 188: | Line 198: | ||
System.out.println(monthName + " " + year + " has " + FEB + " days."); | System.out.println(monthName + " " + year + " has " + FEB + " days."); | ||
} | } | ||
− | + | }} | |
+ | {{OutputBlock | ||
+ | |Code= | ||
+ | Please enter the month [Jan(1) to Dec(12)]: 2 | ||
+ | Please enter the Year: 2008 | ||
+ | February 2009 has 29 days. | ||
+ | }} | ||
+ | {{OutputBlock | ||
+ | |Code= | ||
+ | Please enter the month [Jan(1) to Dec(12)]: 2 | ||
+ | Please enter the Year: 2009 | ||
+ | February 2009 has 28 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. | 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. | ||
− | + | {{CodeBlock | |
+ | |Code= | ||
//APR //JUN //SEP //NOV | //APR //JUN //SEP //NOV | ||
− | else if (monthNum == | + | else if (monthNum == 4{{!}}{{!}} monthNum == 6 {{!}}{{!}} monthNum == 9 {{!}}{{!}} monthNum == 11) |
{ | { | ||
− | if (monthNum == | + | if (monthNum == 4) |
monthName = "April"; | monthName = "April"; | ||
− | else if (monthNum == | + | else if (monthNum == 6) |
monthName = "June"; | monthName = "June"; | ||
− | else if (monthNum == | + | else if (monthNum == 9) |
monthName = "September"; | monthName = "September"; | ||
− | else if (monthNum == | + | else if (monthNum == 11) |
monthName = "November"; | monthName = "November"; | ||
System.out.println(monthName + " " + year + " has " + MONTH30 + " days."); | System.out.println(monthName + " " + year + " has " + MONTH30 + " days."); | ||
} | } | ||
+ | }} | ||
+ | {{OutputBlock | ||
+ | |Code= | ||
+ | Please enter the month [Jan(1) to Dec(12)]: 11 | ||
+ | Please enter the Year: 2009 | ||
+ | November 2009 has 30 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. | 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. | ||
− | + | {{CodeBlock | |
+ | |Code= | ||
//number is larger than 11 | //number is larger than 11 | ||
else | else | ||
− | System.out.println("Invalid month number entered, choose between | + | System.out.println("Invalid month number entered, choose between 1 - 12 (Jan - Dec)"); |
− | + | }} | |
+ | {{OutputBlock | ||
+ | |Code= | ||
+ | Please enter the month [Jan(1) to Dec(12)]: 13 | ||
+ | Please enter the Year: 2009 | ||
+ | Invalid month number entered, choose between 1 - 12 (Jan - Dec) | ||
+ | }} | ||
+ | |||
This concludes the program, compile and run. | This concludes the program, compile and run. | ||
Line 220: | Line 259: | ||
|SolutionCode= | |SolutionCode= | ||
import javax.swing.*; | import javax.swing.*; | ||
+ | import java.util.Scanner; | ||
− | public class DaysInAMonth | + | public class DaysInAMonth |
{ | { | ||
final static int MONTH30 = 30; | final static int MONTH30 = 30; | ||
final static int FEB = 28; | final static int FEB = 28; | ||
final static int MONTH31 = 31; | final static int MONTH31 = 31; | ||
− | + | ||
public static void main(String[] args) | public static void main(String[] args) | ||
{ | { | ||
int year, monthNum; | int year, monthNum; | ||
String monthName; | String monthName; | ||
− | + | Scanner input = new Scanner(System.in); | |
+ | |||
//sentinel values for error checking | //sentinel values for error checking | ||
monthNum = -1; | monthNum = -1; | ||
year = -1; | year = -1; | ||
monthName = ""; | monthName = ""; | ||
− | |||
− | |||
− | |||
− | + | System.out.print("Please enter the month [Jan(1) to Dec(12)]: "); | |
− | + | if(input.hasNextInt()) | |
− | + | ||
{ | { | ||
− | + | monthNum = input.nextInt(); | |
− | + | ||
− | + | System.out.print("Please enter the Year: "); | |
− | + | if(input.hasNextInt()) | |
− | System.out. | + | |
− | + | ||
− | + | ||
− | + | ||
− | if ( | + | |
{ | { | ||
− | + | year = input.nextInt(); | |
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | if (monthNum <= 0 && year <= 999) | |
− | if ( | + | System.out.println("Please try again, there was an error with the month and the year you have entered"); |
− | System.out.println( | + | else if (monthNum <= -1) |
+ | { | ||
+ | System.out.println("Please try again, there was an error with the month you have entered"); | ||
+ | } | ||
+ | else if (year <= 999) | ||
+ | { | ||
+ | System.out.println("Please try again, there was an error with the month you have entered"); | ||
+ | } | ||
else | else | ||
− | System.out.println(monthName + " " + year + " has " + FEB + " days."); | + | //JAN //MAR //MAY //JUL //AUG //OCT //DEC |
+ | if (monthNum == 1 {{!}}{{!}} monthNum == 3 {{!}}{{!}} monthNum == 5 {{!}}{{!}} monthNum == 7 {{!}}{{!}} monthNum == 8 {{!}}{{!}} monthNum == 10 {{!}}{{!}} monthNum == 12) | ||
+ | { | ||
+ | if (monthNum == 1) | ||
+ | monthName = "January"; | ||
+ | else if (monthNum == 3) | ||
+ | monthName = "March"; | ||
+ | else if (monthNum == 5) | ||
+ | monthName = "May"; | ||
+ | else if (monthNum == 7) | ||
+ | monthName = "July"; | ||
+ | else if (monthNum == 8) | ||
+ | monthName = "August"; | ||
+ | else if (monthNum == 10) | ||
+ | monthName = "October"; | ||
+ | else if (monthNum == 12) | ||
+ | monthName = "December"; | ||
+ | |||
+ | System.out.println(monthName + " " + year + " has " + MONTH31 + " days."); | ||
+ | } | ||
+ | //FEB | ||
+ | else if (monthNum == 2) | ||
+ | { | ||
+ | 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."); | ||
+ | } | ||
+ | //APR //JUN //SEP //NOV | ||
+ | else if (monthNum == 4{{!}}{{!}} monthNum == 6 {{!}}{{!}} monthNum == 9 {{!}}{{!}} monthNum == 11) | ||
+ | { | ||
+ | if (monthNum == 4) | ||
+ | monthName = "April"; | ||
+ | else if (monthNum == 6) | ||
+ | monthName = "June"; | ||
+ | else if (monthNum == 9) | ||
+ | monthName = "September"; | ||
+ | else if (monthNum == 11) | ||
+ | monthName = "November"; | ||
+ | |||
+ | System.out.println(monthName + " " + year + " has " + MONTH30 + " days."); | ||
+ | } | ||
+ | //number is larger than 11 | ||
+ | else | ||
+ | System.out.println("Invalid month number entered, choose between 1 - 12 (Jan - Dec)"); | ||
} | } | ||
− | + | else | |
− | else | + | |
{ | { | ||
− | + | System.out.println("Invalid Input"); | |
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | System.out.println( | + | |
} | } | ||
− | + | } | |
− | + | else | |
− | + | { | |
+ | System.out.println("Invalid Input"); | ||
+ | } | ||
} | } | ||
} | } | ||
− | |||
}} | }} |
Latest revision as of 15:27, 8 December 2011
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 the month.
|
...By Students"I remember coming into COMP 1010 having taken AP Computer Science in high school. One thing to note is the KISS theory, Keep it Simple Stupid. In other words, don't over complicate the matter where even yourself can't understand your own code looking over it.
| |
---|---|---|
SolutionStart by importing the swing java package. import javax.swing.*; import java.util.Scanner;
final static int MONTH30 = 30; final static int FEB = 28; final static int MONTH31 = 31;
int year, monthNum; String monthName; Scanner input = new Scanner(System.in); //sentinel values for error checking monthNum = -1; year = -1; monthName = "";
System.out.print("Please enter the month [Jan(1) to Dec(12)]: "); if(input.hasNextInt()) { monthNum = input.nextInt(); System.out.print("Please enter the Year: "); if(input.hasNextInt()) { //code here } else { System.out.println("Please try again, there was an error with the month or the year you have entered"); } } else { System.out.println("Invalid month number entered, choose between 1 - 12 (Jan - Dec)"); } Please enter the month [Jan(1) to Dec(12)]: abcd Please enter the Year: 2009 Invalid input of month Please enter the month [Jan(1) to Dec(12)]: 12 Please enter the Year: asdf Invalid Input of Year
First case, if user left both month and year blank. if (monthNum <= 0 && year <= 999) System.out.println("Please try again, there was an error with the month and the year you have entered"); Please enter the month [Jan(1) to Dec(12)]: -1 Please enter the Year: 987 Please try again, there was an error with the month and the year you have entered
else if (monthNum <= 0) { System.out.println("Please try again, there was an error with the month you have entered"); } Please enter the month [Jan(1) to Dec(12)]: -1 Please enter the Year: 2009 Please try again, there was an error with the month you have entered
else if (year <= 999) { System.out.println("Please try again, there was an error with the month you have entered"); } Please enter the month [Jan(1) to Dec(12)]: 12 Please enter the Year: 987 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 == 1 || monthNum == 3 || monthNum == 5 || monthNum == 7 || monthNum == 8 || monthNum == 10 || monthNum == 12) { if (monthNum == 1) monthName = "January"; else if (monthNum == 3) monthName = "March"; else if (monthNum == 5) monthName = "May"; else if (monthNum == 7) monthName = "July"; else if (monthNum == 8) monthName = "August"; else if (monthNum == 10) monthName = "October"; else if (monthNum == 12) monthName = "December"; System.out.println(monthName + " " + year + " has " + MONTH31 + " days."); } Please enter the month [Jan(1) to Dec(12)]: 3 Please enter the Year: 2009 March 2009 has 31 days.
//FEB else if (monthNum == 2) { 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."); } Please enter the month [Jan(1) to Dec(12)]: 2 Please enter the Year: 2008 February 2009 has 29 days. Please enter the month [Jan(1) to Dec(12)]: 2 Please enter the Year: 2009 February 2009 has 28 days.
//APR //JUN //SEP //NOV else if (monthNum == 4|| monthNum == 6 || monthNum == 9 || monthNum == 11) { if (monthNum == 4) monthName = "April"; else if (monthNum == 6) monthName = "June"; else if (monthNum == 9) monthName = "September"; else if (monthNum == 11) monthName = "November"; System.out.println(monthName + " " + year + " has " + MONTH30 + " days."); } Please enter the month [Jan(1) to Dec(12)]: 11 Please enter the Year: 2009 November 2009 has 30 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 1 - 12 (Jan - Dec)"); Please enter the month [Jan(1) to Dec(12)]: 13 Please enter the Year: 2009 Invalid month number entered, choose between 1 - 12 (Jan - Dec)
| ||
Code |