Back to the Program-A-Day homepage
Problem
Write a Java program DaysInAMonth, that prompts for a month and year and determines the amount of days that will be in that month.
Your program should do the following:
- prompt the user for the month as an integer (for this example we used 0 based numbering i.e. 0 for Jan)
- prompt the user for the year as an integer
- some form of input validation, use sentinel values and determine if invalid month or years are entered
- output a friendly message to System.out stating the result
This problem will cover:
Example: User inputs "2" for the month and "2004" for the year would result in something like "February 2004 has 29 days."
|
|
Days In A Month
|
Solution
Start 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
Solution Code
import javax.swing.*;
public class DaysInAMonth
{
final static int MONTH30 = 30;
final static int FEB = 28;
final static int MONTH31 = 31;
public static void main(String[] args)
{
int year, monthNum;
String monthName;
//sentinel values for error checking
monthNum = -1;
year = -1;
monthName = "";
monthNum = Integer.parseInt(JOptionPane.showInputDialog("Please enter the month [Jan(0) to Dec(11)]"));
year = Integer.parseInt(JOptionPane.showInputDialog("Please enter the year"));
if (monthNum == -1 && year == -1)
System.out.println("Please try again, there was an error with the month or the year you have entered");
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
//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.");
}
//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.");
}
//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.");
}
//number is larger than 11
else
System.out.println("Invalid month number entered, choose between 0 - 11 (Jan - Dec)");
}
}
|
Back to the Program-A-Day homepage