Difference between revisions of "Days In A Month"
Line 113: | Line 113: | ||
|Solution=Start by importing the swing java package. <br/> | |Solution=Start by importing the swing java package. <br/> | ||
− | <pre> import javax.swing.*;</pre> | + | <pre> import javax.swing.*;</pre><br/> |
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. | 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. | ||
Line 120: | Line 120: | ||
final static int FEB = 28; | final static int FEB = 28; | ||
final static int MONTH31 = 31; | final static int MONTH31 = 31; | ||
− | </pre> | + | </pre><br/> |
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. | 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. | ||
Line 131: | Line 131: | ||
year = -1; | year = -1; | ||
monthName = ""; | monthName = ""; | ||
− | </pre> | + | </pre><br/> |
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. | 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. |
Revision as of 01:37, 7 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 need to use conditional statements to check for each unit, one for Celcius and one for Fahrenheit. One thing to note is that because unit is a String datatype, we will have to only extract the first character assuming it has be entered in correctly, this is easily accomplished using 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 |