Month and year program code!!!!HELP PLEASE!!!

• 02-10-2008, 09:23 PM
chitwood
Month and year program code!!!!HELP PLEASE!!!
I need help!!!!

This is the assignment and here is the code I have already???? Can someone help me please?

Create a program that prompts the user to enter the month and year. The program will then display the number of days in the month. For example, if the user enters month 8 and year 2007, the program will display "August 2007 has 31 days". Use input dialog boxes and message dialog boxes.

Don't forget that leap year must be part of your calculation. The formula for determining whether a year is leap year is found in your textbook and also on the internet (using a simple Google search). Once you've determined whether you have a leap year, then you can determine how many days are in February.

import javax.swing.JOptionPane;

public class LeapYear {
public static void main(String[] args) {
// Prompt the user to enter a year
String yearString = JOptionPane.showInputDialog("Enter a year");

//Convert the string into an int value
int year = Integer.parseInt(yearString);

// Check if the year is a leap year
boolean isLeapYear =
(year % 4 == 0 && year % 100 != 0) || (year % 400 == 0);

// Display the result in a message dialog box
JOptionPane.showMessageDialog(null,
year + " is a leap year? " + isLeapYear);
}
}

I dont understand how to add the month portion to the assignment Can someone help me please?????????

Thanks,

Chitwood
• 10-13-2010, 11:16 AM
felixtfelix
here you go.

import javax.swing.JOptionPane;

public class LeapYear {

private int[] days ={31,28,31,30,31,30,31,31,30,31,30,31};
private String[] months = {
"January","February","March","April","May","Ju ne",
"July","August","September","October","November"," December"
};

public void display(int month,int year){
if(isLeapYear(year)){
days[1] = 29;
}

JOptionPane.showMessageDialog(null, months[month-1] +" " +year +" has "+days[month-1] +" days");
}

public boolean isLeapYear(int year){
return (year % 4 == 0 && year % 100 != 0) || (year % 400 == 0);
}

public static void main(String[] args){
LeapYear leapYear = new LeapYear();

int month = Integer.parseInt(JOptionPane.showInputDialog("Ente r Month"));
int year = Integer.parseInt(JOptionPane.showInputDialog("Ente r Month"));

leapYear.display(month, year);
}
}
• 10-13-2010, 11:27 AM
DarrylBurke
Quote:

Originally Posted by felixtfelix
here you go.

Thank you for providing a free homework service in what is meant to be a forum.

Would you be free to wash my car for me too? Without charge, of course.

db
• 10-13-2010, 11:31 AM
Eranga
Quote:

Originally Posted by Darryl.Burke
Thank you for providing a free homework service in what is meant to be a forum.

Would you be free to wash my car for me too? Without charge, of course.

db

Mine too, I didn't wash my car for months... :eek:
• 10-13-2010, 01:21 PM
Fubarable
Quote:

Originally Posted by Darryl.Burke
Thank you for providing a free homework service in what is meant to be a forum.

The OP might be a little late handing in this assignment though, so not to worry too much!