Days In A Month

From CompSciWiki
Revision as of 01:53, 7 April 2010 by HenryK (Talk | contribs)

Jump to: navigation, search

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

Wiki trays01.jpg


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

Back to the Program-A-Day homepage