/* Solution to part C of project 0 - */ /** class Birthday : This class defines various methods for finding some * interesting information about a person's birthday/age.*/ import java.util.*; import java.io.*; import uwcse.io.*; public class Birthday{ private GregorianCalendar mycal; /** constructor of the class Birthday: takes a month,date and * year and initializes mycal using these fields */ public Birthday(int year, int month, int date) { mycal=new GregorianCalendar(year,month,date); } /** This function returns the age in days */ public int getAgeInDays() { /* Variable currentcal stores the current date and time */ GregorianCalendar currentcal=new GregorianCalendar(); long ageInMillis; int ageInDays; //System.out.println(currentcal.getTime()); ageInMillis=currentcal.getTimeInMillis()-mycal.getTimeInMillis(); /* divide age in days by number of milliseconds in a day to get the age in days */ ageInDays=(int)(ageInMillis/(1000*3600*24)); return ageInDays; } /** Function for checking if the age in days is interesting * where interesting means that it is divisible by 1000 */ public boolean isAgeInteresting() { int ageInDays=getAgeInDays(); if(ageInDays%1000 == 0) return true; else return false; } /** This function is for taking input from the user * and intialising a Birthday object using java iostreams directly*/ public static Birthday getNewBirthdayObject1() { String str; int year=0,month=0,day=0; /* Take the required input from the user */ try{ BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); System.out.println("Hi, I am using java iostreams directly for input"); System.out.println("\nEnter the following information about birthday \n"); System.out.print("month: "); str=br.readLine(); /* subtract one from the month value as the internal * representation for Georgian calendar uses numbering * from 0 for months */ month=Integer.parseInt(str)-1; System.out.print("day: "); str=br.readLine(); day=Integer.parseInt(str); System.out.print("year: "); str=br.readLine(); year=Integer.parseInt(str); }catch(IOException ioe){ioe.printStackTrace();} /* create a new Birthday object based on the input from the * user and return it */ return new Birthday(year,month,day); } /** This function is for taking input from the user * and intialising a birthday object using uwcse.jar * functions*/ public static Birthday getNewBirthdayObject() { int year,month,day; Input inp=new Input(); /* Take the required input from the user */ System.out.println("Hi, I am using uwcse.jar functions for input"); System.out.println("\nEnter the following information about the"+ "birthday\n"); /* subtract one from the month value as the internal * representation for Georgian calendar uses numbering * from 0 for months */ month=inp.readInt("month: ") -1; day=inp.readInt("day: "); year=inp.readInt("year: "); /* create a new birthday object based on the input from the * user and return it */ return new Birthday(year,month,day); } /** Main function : creates an object of the Birthday class * and calls the required functions to produce the * desired output*/ public static void main(String args[]) { int ageInDays; /* Here are the two options for taking the input * if you want to use uwcse.jar functions, uncomment * the first line and comment the second one * if you want to directly use java libraries, use * the second call and comment the first one. */ Birthday mybday=getNewBirthdayObject(); //Birthday mybday=getNewBirthdayObject1(); ageInDays=mybday.getAgeInDays(); System.out.println("\nThe age in days is : "+ageInDays); if(mybday.isAgeInteresting()) System.out.println("The age is interesting"); else System.out.println("The age is not interesting"); System.out.println(); } }