Difference between revisions of "Days In A Month"
Line 142: | Line 142: | ||
</pre><br/> | </pre><br/> | ||
− | Now we | + | 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. <br/> |
+ | |||
+ | First case, if user left both month and year blank. | ||
+ | <pre> | ||
+ | if (monthNum == -1 && year == -1) | ||
+ | System.out.println("Please try again, there was an error with the month or the year you have entered"); | ||
+ | </pre><br/> | ||
+ | Second case, if user entered an invalid monthNumber like a negative month number | ||
+ | <pre> | ||
+ | else if (monthNum <= -1) | ||
+ | { | ||
+ | System.out.println("Please try again, there was an error with the month you have entered"); | ||
+ | } | ||
+ | </pre><br/> | ||
+ | Third case, if user entered an invalid year, i.e. negative or in this problem we assume year 1000 is the first year | ||
+ | <pre> | ||
+ | else if (year <= 999) | ||
+ | { | ||
+ | System.out.println("Please try again, there was an error with the month you have entered"); | ||
+ | } | ||
+ | </pre><br/> | ||
+ | |||
<pre>unit.charAt(int index)</pre> | <pre>unit.charAt(int index)</pre> |
Revision as of 00:42, 8 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.
|
Days In A Month
| |
---|---|---|
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"); }
unit.charAt(int index) Where index is the position at which the char character is at in the String. For each case, calculate your results and print the output using System.out. To make your program more robust, you may want to use an else case for all invalid characters. if (unit.charAt(0) == 'c') { result = 9 * temperature / 5 + 32; System.out.println(temperature + " degree Celcius = " + result + " degree Fahrenheit"); } else if (unit.charAt(0) == 'f') { result = (temperature - 32) * 5 / 9; System.out.println(temperature + " degree Fahrenheit = " + result + " degree Celcius"); } else System.out.printline("You entered an incorrect unit, please try again"); | ||
Code |