Results 1 to 5 of 5
- 02-10-2008, 08:23 PM #1
Member
- Join Date
- Jan 2008
- Posts
- 23
- Rep Power
- 0
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 #2
Member
- Join Date
- Dec 2007
- Location
- Mumbai, India
- Posts
- 37
- Rep Power
- 0
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 #3
- 10-13-2010, 11:31 AM #4
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
-
Similar Threads
-
Graph program code,hardnut
By Javashak in forum Advanced JavaReplies: 0Last Post: 03-24-2008, 03:25 PM -
Peculiarty in code of simple program...
By Kreuz14 in forum New To JavaReplies: 4Last Post: 01-23-2008, 03:27 AM -
Current month
By Java Tip in forum Java TipReplies: 0Last Post: 01-07-2008, 08:40 AM -
Merry Christmas and happy New year
By rgbosque in forum Forum LobbyReplies: 3Last Post: 12-31-2007, 02:44 AM -
code for making a java swing program a demo verson
By fakhruddin in forum AWT / SwingReplies: 1Last Post: 11-27-2007, 08:54 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks