Advertisement
Guest User

Untitled

a guest
Oct 23rd, 2017
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.32 KB | None | 0 0
  1. /*
  2. * Name: Kolby Ihlen
  3. * ID: V00901667
  4. * Date: October 23, 2017
  5. * Filename: CalendarGames.java
  6. * Details: CSC110 Assignment 04
  7. */
  8. import java.util.*;
  9.  
  10. public class CalendarGames{
  11.  
  12.  
  13. public static String monthToString(int month){
  14. String monthString;
  15. switch (month) {
  16. case 1: return "January";
  17. case 2: return "February";
  18. case 3: return "March";
  19. case 4: return "April";
  20. case 5: return "May";
  21. case 6: return "June";
  22. case 7: return "July";
  23. case 8: return "August";
  24. case 9: return "September";
  25. case 10: return "October";
  26. case 11: return "November";
  27. case 12: return "December";
  28. default: return "Invalid month";
  29. }
  30.  
  31. }
  32.  
  33.  
  34. public static boolean isLeapYear(int year){
  35. if ((year % 400 == 0) || ((year % 4 == 0) && (year % 100 != 0))) {
  36. return true;
  37. } else {
  38. return false;
  39. }
  40. }
  41.  
  42.  
  43.  
  44.  
  45.  
  46. public static int numDaysInMonth(int month, int year){
  47. if (month==2){
  48. if(isLeapYear(year)){
  49. return 29;
  50. }
  51. else{
  52. return 28;
  53. }
  54. }
  55. else if(month==4||month==6||month==9||month==11){
  56. return 30;
  57. }
  58. else{
  59. return 31;
  60. }
  61.  
  62. }
  63.  
  64.  
  65.  
  66. public static void guessMyBirthday(){
  67. int min = 0;
  68. int max = 13;
  69. Scanner ob = new Scanner(System.in);
  70. boolean correct = false;
  71. int guessMonth = 0;
  72. String question;
  73. int foundDay = 0;
  74. int foundMonth = 0;
  75. int foundYear;
  76. while(correct==false){
  77. guessMonth=(min+max)/2;
  78. System.out.print("Is your birthday in "+monthToString(guessMonth)+"? yes or no: ");
  79. question=ob.nextLine();
  80. if(question.equals("yes")){
  81. foundMonth=guessMonth;
  82. } else if(question.equals("no")) {
  83. System.out.print("Is your birthday after this month? yes or no: ");
  84. question = ob.nextLine();
  85. if(question.equals("yes")){
  86. min=guessMonth;
  87. } else if(question.equals("no")){
  88. max=guessMonth;
  89. } else {
  90. System.out.println("Not a month");
  91. }
  92. } else {
  93. System.out.println("Wrong input");
  94. }
  95. }
  96. if(foundMonth==2){
  97. System.out.print("What year were you born? ");
  98. foundYear=ob.nextInt();
  99. } else {
  100. foundYear=0;
  101. }
  102. correct=false;
  103. min=0;
  104. max=numDaysInMonth(foundMonth, foundYear);
  105. while(correct==false){
  106. int guessDay=(min+max)/2;
  107. System.out.print("Is your birthday on "+monthToString(foundMonth)+guessDay+"? yes or no: ");
  108. question=ob.nextLine();
  109. if(question.equals("yes")){
  110. foundDay=guessDay;
  111. } else if(question.equals("no")) {
  112. if(question.equals("yes")){
  113. min=guessDay;
  114. } else if(question.equals("no")){
  115. max=guessDay;
  116. } else {
  117. System.out.println("Not a month");
  118. }
  119. } else {
  120. System.out.println("Wrong input");
  121. }
  122. }
  123. System.out.println("Your birthday is on "+monthToString(foundMonth)+foundDay+".");
  124. }
  125.  
  126.  
  127. public static void main(String[] args){
  128. guessMyBirthday();
  129.  
  130.  
  131. }
  132. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement