Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * Name: Kolby Ihlen
- * ID: V00901667
- * Date: October 23, 2017
- * Filename: CalendarGames.java
- * Details: CSC110 Assignment 04
- */
- import java.util.*;
- public class CalendarGames{
- public static String monthToString(int month){
- String monthString;
- switch (month) {
- case 1: return "January";
- case 2: return "February";
- case 3: return "March";
- case 4: return "April";
- case 5: return "May";
- case 6: return "June";
- case 7: return "July";
- case 8: return "August";
- case 9: return "September";
- case 10: return "October";
- case 11: return "November";
- case 12: return "December";
- default: return "Invalid month";
- }
- }
- public static boolean isLeapYear(int year){
- if ((year % 400 == 0) || ((year % 4 == 0) && (year % 100 != 0))) {
- return true;
- } else {
- return false;
- }
- }
- public static int numDaysInMonth(int month, int year){
- if (month==2){
- if(isLeapYear(year)){
- return 29;
- }
- else{
- return 28;
- }
- }
- else if(month==4||month==6||month==9||month==11){
- return 30;
- }
- else{
- return 31;
- }
- }
- public static void guessMyBirthday(){
- int min = 0;
- int max = 13;
- Scanner ob = new Scanner(System.in);
- boolean correct = false;
- int guessMonth = 0;
- String question;
- int foundDay = 0;
- int foundMonth = 0;
- int foundYear;
- while(correct==false){
- guessMonth=(min+max)/2;
- System.out.print("Is your birthday in "+monthToString(guessMonth)+"? yes or no: ");
- question=ob.nextLine();
- if(question.equals("yes")){
- foundMonth=guessMonth;
- } else if(question.equals("no")) {
- System.out.print("Is your birthday after this month? yes or no: ");
- question = ob.nextLine();
- if(question.equals("yes")){
- min=guessMonth;
- } else if(question.equals("no")){
- max=guessMonth;
- } else {
- System.out.println("Not a month");
- }
- } else {
- System.out.println("Wrong input");
- }
- }
- if(foundMonth==2){
- System.out.print("What year were you born? ");
- foundYear=ob.nextInt();
- } else {
- foundYear=0;
- }
- correct=false;
- min=0;
- max=numDaysInMonth(foundMonth, foundYear);
- while(correct==false){
- int guessDay=(min+max)/2;
- System.out.print("Is your birthday on "+monthToString(foundMonth)+guessDay+"? yes or no: ");
- question=ob.nextLine();
- if(question.equals("yes")){
- foundDay=guessDay;
- } else if(question.equals("no")) {
- if(question.equals("yes")){
- min=guessDay;
- } else if(question.equals("no")){
- max=guessDay;
- } else {
- System.out.println("Not a month");
- }
- } else {
- System.out.println("Wrong input");
- }
- }
- System.out.println("Your birthday is on "+monthToString(foundMonth)+foundDay+".");
- }
- public static void main(String[] args){
- guessMyBirthday();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement