Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class Device {
- public static final int ORIGINYEAR = 1980;
- public static void main(String args[]) {
- int days;
- int bad_year;
- int good_year;
- Scanner keyboard = new Scanner(System.in);
- System.out.print("Enter days: ");
- days = keyboard.nextInt();
- bad_year = badCode(days);
- good_year = goodCode(days);
- System.out.println("badCode - Days: " + days + " = Year: " + bad_year);
- System.out.println("goodCode - Days: " + days + " = Year: " + good_year);
- keyboard.close();
- }
- /**
- * Supposedly calculates the year given days since ORIGINYEAR. Fails on a leap
- * year.
- *
- * @param days
- * number of days since January 1, 1980 (int >= 0)
- * @return the year given days
- */
- public static int badCode(int days) {
- int year = ORIGINYEAR; /* = 1980 */
- while (days > 365) {
- if (LeapYear.leapYear(year)) {
- if (days > 366) {
- days -= 366;
- year += 1;
- }
- } else {
- days -= 365;
- year += 1;
- }
- }
- return year;
- }
- /**
- * Correctly calculates the year given days since ORIGINYEAR.
- *
- * @param days
- * number of days since January 1, 1980 (int >= 0)
- * @return the year
- */
- public static int goodCode(int days) {
- // method code here
- int year = ORIGINYEAR;
- int leap_check = 1;// so there is not an infinite loop
- while (days > 365 && leap_check == 1) {
- if (LeapYear.leapYear(year)) {
- if (days > 366) {
- days -= 366;
- year += 1;
- } else {// if year still contains 366 days, do not increment the year
- leap_check = 3;
- }
- } else {
- days -= 365;
- year += 1;
- }
- }
- return year;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement