Advertisement
Guest User

Untitled

a guest
Sep 23rd, 2017
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.65 KB | None | 0 0
  1. public class Device {
  2.  
  3. public static final int ORIGINYEAR = 1980;
  4.  
  5. public static void main(String args[]) {
  6. int days;
  7. int bad_year;
  8. int good_year;
  9.  
  10. Scanner keyboard = new Scanner(System.in);
  11. System.out.print("Enter days: ");
  12. days = keyboard.nextInt();
  13.  
  14. bad_year = badCode(days);
  15. good_year = goodCode(days);
  16.  
  17. System.out.println("badCode - Days: " + days + " = Year: " + bad_year);
  18. System.out.println("goodCode - Days: " + days + " = Year: " + good_year);
  19.  
  20. keyboard.close();
  21. }
  22.  
  23. /**
  24. * Supposedly calculates the year given days since ORIGINYEAR. Fails on a leap
  25. * year.
  26. *
  27. * @param days
  28. * number of days since January 1, 1980 (int >= 0)
  29. * @return the year given days
  30. */
  31. public static int badCode(int days) {
  32. int year = ORIGINYEAR; /* = 1980 */
  33.  
  34. while (days > 365) {
  35. if (LeapYear.leapYear(year)) {
  36. if (days > 366) {
  37. days -= 366;
  38. year += 1;
  39. }
  40. } else {
  41. days -= 365;
  42. year += 1;
  43. }
  44. }
  45. return year;
  46. }
  47.  
  48. /**
  49. * Correctly calculates the year given days since ORIGINYEAR.
  50. *
  51. * @param days
  52. * number of days since January 1, 1980 (int >= 0)
  53. * @return the year
  54. */
  55. public static int goodCode(int days) {
  56. // method code here
  57. int year = ORIGINYEAR;
  58. int leap_check = 1;// so there is not an infinite loop
  59. while (days > 365 && leap_check == 1) {
  60. if (LeapYear.leapYear(year)) {
  61. if (days > 366) {
  62. days -= 366;
  63. year += 1;
  64. } else {// if year still contains 366 days, do not increment the year
  65. leap_check = 3;
  66. }
  67.  
  68. } else {
  69. days -= 365;
  70. year += 1;
  71. }
  72.  
  73. }
  74. return year;
  75. }
  76. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement