Difference between revisions of "Days In A Month"

From CompSciWiki
Jump to: navigation, search
Line 142: Line 142:
 
</pre><br/>
 
</pre><br/>
  
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
+
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

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 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

Solution Code

Back to the Program-A-Day homepage