//JulianDate.java 102a //calculate julian date of any year/month/day input. //a program with lots of if statements. import javax.swing.*; public class JulianDate extends JApplet { public void init () { String input; int year, month, day; int previousMonthDays; String leap; int julian; input = JOptionPane.showInputDialog(null,"Enter year"); year = Integer.parseInt(input); if (year < 1753) { JOptionPane.showMessageDialog(null,"Sorry, calendar was wacky before 1753. Bye!"); return; } input = JOptionPane.showInputDialog(null,"Enter month number"); month = Integer.parseInt(input); if (month<1 || month>12) { JOptionPane.showMessageDialog(null,"bogus month. Bye!"); return; } input = JOptionPane.showInputDialog(null,"Enter day"); day = Integer.parseInt(input); if (day<1 || day>31) { JOptionPane.showMessageDialog(null,"bogus day. Bye!"); return; } //test for leapyear. //a year is a leap year if evenly divisible by 4 but not also evenly //divisible by 100 unless also evenly divisible by 400. //I.e. century years are not normally leap years unless they are also 400 //years. 1800, 1900, 2100 are not leap years, but 2000 is. if (year%4==0 && year%100!=0 || year%400==0) leap = "yes"; else leap = "no"; //test that month day combo is valid. have already checked that day is <= 31 //So know that 31 day months are valid. if (month==4 || month==6 || month==9 || month==11) { //30 day months if (day > 30) { if (month == 4) JOptionPane.showMessageDialog(null,"April has only 30 days. Bye!"); else if (month == 6) JOptionPane.showMessageDialog(null,"June has only 30 days. Bye!"); else if (month == 4) JOptionPane.showMessageDialog(null,"September has only 30 days. Bye!"); else if (month == 11) JOptionPane.showMessageDialog(null,"November has only 30 days. Bye!"); return; } } else if (month == 2) //Feb. if (leap.equals("no")) { //not a leap year if (day > 28) { JOptionPane.showMessageDialog(null,"February has only 28 days this year. Bye!"); return; } } else //is leap year if (day > 29) { JOptionPane.showMessageDialog(null,"February has only 29 days this year. Bye!"); return; } //determine the number of days in all the previous months previousMonthDays = 0; if (month > 1) previousMonthDays += 31; //Jan if (month > 2) { previousMonthDays += 28; //Feb if (leap.equals("yes")) previousMonthDays++; //Feb 29 } if (month > 3) previousMonthDays += 31; //Mar if (month > 4) previousMonthDays += 30; //Apr if (month > 5) previousMonthDays += 31; //May if (month > 6) previousMonthDays += 30; //Jun if (month > 7) previousMonthDays += 31; //Jul if (month > 8) previousMonthDays += 31; //Aug if (month > 9) previousMonthDays += 30; //Sep if (month > 10) previousMonthDays += 31; //Oct if (month > 11) previousMonthDays += 30; //Nov julian = previousMonthDays + day; JOptionPane.showMessageDialog(null,"Julian date: " + julian); } }