Java

Moderators: zibadian
Number of threads: 7836
Number of posts: 18235

This Forum Only
Post New Thread
Single Post View       Linear View       Threaded View      f

Report
New to Java, Help with Leap Year program Posted by mhagen41 on 21 Jan 2011 at 9:59 AM
Hey everyone, I'm new here and I'm stuck on an a program that asks the user to input a date (yyyy-mm-dd). My program will output the amount of days since January 01 1900.
Anyways, everything was running good up until my leap year conversion equation. I have to take the integer part of a few of the numbers, and I'm not sure if I am doing it correctly. Can anyone help me out?

-Thanks

import javax.swing.*;
import java.util.Date ;
import java.util.Scanner;

public class Days{
public static void main (String args[]){

//variables
String input;
Scanner source;
int year;
int month;
int day;
int year2;
int earlyMonth;
int year3;
int month2;
int leapYear;
int day1;
int day2;

input = JOptionPane.showInputDialog("Enter a date (yyyy-mm-dd)");
source = new Scanner (input);
source.useDelimiter("-");

year = source.nextInt( );
month = source.nextInt( );
day = source.nextInt( );

year2 = year - 1900;
earlyMonth = (int)Math.floor(14-month)/12.;
year3 = year2 - earlyMonth;
month2 = month + 12*earlyMonth;


leapYear = 1 + Math.min(year3,0) +(int)Math.floor(year3)/4.) - (int)Math.floor(year3/100.) + (int)Math.floor(year3+300)/400 ;
}
}

This is from my professor:

Formula: Use the following formulas to determine the day:
1) year2 = year - 1900
2) earlyMonth = | (14-month)/12 |
3) year3 = year2 - earlyMonth
4) month2 = month + 12*earlyMonth
5) leapYear = 1 + min(year3,0) + | (year3)/4) | - | (year3/100) | + | (year3+300)/400 |
6) day1 = -1.63 + | (month2-1)*30.6 |
7) day2 = day + year3*365 + leapYear + day1

Note 1: the notation
| ... |
in these equations means to take the integer part of the result.
Note 2: the cryptic names used in these mathematical equations are NOT suitable for use in a computer
program.
Equation (1) determines the year offset from the starting point.
Equation (2) is an early-month correction. It is easier to do the calculation if we think of January and
February as the 13th and 14th months of the previous year, so the leap day is added on at the “end” of the
year. Equation (3) corrects the date, and Equation (4) corrects the month accordingly.
Equation (5) calculates the number of leap years since the starting point, using the fact that every year
divisible by 4 is a leap year, unless it is divisible by 100 and not divisible by 400. Excel makes an
intentional error by allowing for February 29, 1900, a date that didn’t exist. It does so to maintain
compatibility with an earlier popular spreadsheet program, Lotus 123, which made this error
unintentionally (see http://www.cpearson.com/excel/datetime.htm).
Equation (6) determines the number of days preceding the given month in a non-leap year, and then
Equation (7) adds on the day of the month, and the number of days preceding the year. The final day is d2.
Methods:

Report
Re: New to Java, Help with Leap Year program Posted by silveredge52 on 21 Jan 2011 at 12:57 PM
Hey,
I think the int casts used will do the job because of the way the methods you are using work. If something doesn't seem to be working right, debug individual components using print statements, ie.
        int year3 = 27;
        int answerI;
        answerI = (int) Math.floor(year3/4);
        System.out.println("value of answerI as an integer is: " + answerI );


Math methods are described here.

ps. I think there is a problem with the formula for day1 because of the -1.63 will cause a double value that is not directly usable as an integer.

g.l. se

Report
Re: New to Java, Help with Leap Year program Posted by mhagen41 on 21 Jan 2011 at 5:30 PM
thanks! My friend helped me out and we found the error. I was missing a few brackets in the leapYear formula.



 

Recent Jobs

Official Programmer's Heaven Blogs
Web Hosting | Browser and Social Games | Gadgets

Popular resources on Programmersheaven.com
Assembly | Basic | C | C# | C++ | Delphi | Flash | Java | JavaScript | Pascal | Perl | PHP | Python | Ruby | Visual Basic
© Copyright 2011 Programmersheaven.com - All rights reserved.
Reproduction in whole or in part, in any form or medium without express written permission is prohibited.
Violators of this policy may be subject to legal action. Please read our Terms Of Use and Privacy Statement for more information.
Operated by CommunityHeaven, a BootstrapLabs company.