Results 1 to 4 of 4
  1. #1
    omygoodness is offline Member
    Join Date
    Jan 2010
    Posts
    10
    Rep Power
    0

    Default Calendar in onject way

    Hi,
    I have task to solve:

    Java Code:
    Day
    
    Create the class Day for representing days in a calendar.
    The class should include attributes specifying the day: dayOfMonth, month, year.
    
       1. Implement the following public constructors:
              * Day(int numberOfDaysSinceTheBeginningOfTheEpoch);, where numberOfDaysSinceTheBeginningOfTheEpoch is the number of days that elapsed since some distinguished date like 15.10.1582.
              * Day(int dayOfYear, int year);
              * Day(String dayOfWeek, int weekOfYear, int year);
              * Day(int dayOfMonth, int month, int year);
              * You might also need a private constructor for initializing the calendar (to be used only once):
                private Day(int day, int month, int year, String dayOfWeek);.
       2. Implement the following public methods:
              * String dayOfWeek()
              * String month()
              * String season()
              * String academicSeason()
              * int dayOfYear(), int daysLeft() // the number of days left till the end of the year
              * int weekOfYear(), int weeksLeft() // the number of weeks left till the end of the year
              * int monthOfYear(), int monthsLeft()
              * int year()
              * boolean isLeapYear()
              * int daysBetween(Day day) returning the number of days between this and day (might be a negative number!)
          Remarks
              * You might need some private helper methods too.
              * You may assume that all academic seasons start and end the same day each year.
       3. Define and use in implementation of methods the following static constants:
              * Days of week: public final static String MONDAY = "MONDAY";
              * month names
              * season names
              * academic year terms like: "winter term", "summer term", "holidays", "exams"
          You will need a static constant to link some particular date to the corresponding day of week. For example:
          private final static Day TODAY = new Day(17, 12, 2009, Day.THURSDAY);
          Note
              * You may use enumerations instead of static constants.
       4. Add suitable test/presentation of the methods and constructors in the method main().
    And I have something like this but I am not sure about it
    maybe someone have idea to improve code or can write it in shorter way:
    Thank you.

    Java Code:
    public class Day
    {
    	//enums
    	public static enum DaysOfWeek {
    		MONDAY("monday",1),
    		TUESDAY("tuesday",2),
    		WEDNESDAY("wednesday",3),
    		THURSDAY("thursday",4),
    		FRIDAY("friday",5),
    		SATURDAY("saturday",6),
    		SUNDAY("sunday",7);
    		
    		private final String dayStr;
    		private final int dayInt; 
    			
    		DaysOfWeek(String s,int i)
    		{
    			dayStr=s;
    			dayInt=i;
    		}
    		
    		public String getString()
    		{
    			return dayStr;
    		}
    		
    		public int getInt()
    		{
    			return dayInt;
    		}
    	}
    	//extend these enums
    	public static enum Months {
    		JANUARY("january",1),
    		FEBRUARY("february",2),
    		MARCH("march",3),
    		APRIL("april",4),
    		MAY("may",5),
    		JUNE("june",6),
    		JULY("july",7),
    		AUGUST("august",8),
    		SEPTEMBER("september",9),
    		OCTOBER("october",10),
    		NOVEMBER("november",11),
    		DECEMBER("december",12);
    		
    		private final String monthStr;
    		private final int monthInt;
    		
    		private Months(String s, int i)
    		{
    			monthStr=s;
    			monthInt=i;
    		}
    		
    		public String getString()
    		{
    			return monthStr;
    		}
    		
    		public int getInt()
    		{
    			return monthInt;
    		}
    	}
    	public static enum Seasons
    	{
    		SPRING("spring"),
    		WINTER("winter"),
    		SUMMER("summer"),
    		AUTUMN("autumn");
    		
    		private String seasonStr;
    			
    		private Seasons(String s)
    		{
    			seasonStr=s;
    		}
    		
    		public String getString()
    		{
    			return seasonStr;
    		}
    	};
    	public static enum AcademicTerms {
    		WINTER_TERM("winter term"),
    		SUMMER_TERM("summer term"),
    		HOLIDAYS("holidays"),
    		EXAMS("exams");
    		
    		private String termStr;
    		
    		private AcademicTerms(String t)
    		{
    			termStr=t;
    		}
    		
    		public String getString()
    		{
    			return termStr;
    		}
    	}
    	
    	private static final DaysOfWeek BEGINNING_WEEKDAY=DaysOfWeek.FRIDAY;//mialem z tym problemy bo static i final jednoczesnie!!!!!!
    	private static final Day BEGINNING_DATE=new Day(15,10,1582);
    	
    	private final int D;//day of month
    	private final Months M;//should it be int or Months?
    	private final int Y;//year number
    	
    	//constructors
    	public Day(int numberOfDaysSinceTheBeginningOfTheEpoch)
    	{		
    		int additionalYears=BEGINNING_DATE.Y;
    		
    		int additionalDays=BEGINNING_DATE.dayOfYear()+numberOfDaysSinceTheBeginningOfTheEpoch;
    		if(additionalDays>0)
    		{
    			while(additionalDays>(isLeapYear(additionalYears)?366:365))
    			{
    				additionalDays-=isLeapYear(additionalYears)?366:365;
    				additionalYears++;
    			}
    		}
    		else
    		{
    		//	if(additionalYears)
    			while(additionalDays<1)//przed ostatnim sprawdzeniem dodac 10
    			{	additionalYears--;
    				additionalDays+=isLeapYear(additionalYears)?366:365;
    			}
    		}
    		
    		//find month and day of month
    		int monthNum=0;
    		int daysNum=0;
    		int oldNumDays=0;//just to init
    		while(daysNum<additionalDays)
    		{
    			monthNum++;
    			oldNumDays=daysNum;
    			if(monthNum<8)	daysNum+=30+monthNum%2;
    			else	daysNum+=31-monthNum%2;
    			if(monthNum==3)
    				if(isLeapYear(additionalYears))
    					daysNum-=1;
    				else
    					daysNum-=2;
    		}
    		//switch between Greg and Jul
    		additionalDays-=oldNumDays;
    		//10 days further - from Greg to Jul
    		boolean isGJ=false;
    			if(additionalYears<1582)
    				isGJ=true;
    			else if(additionalYears==1582)
    				if(monthNum<10)
    					isGJ=true;
    				else if(monthNum==10)
    					if(additionalDays<15)
    						isGJ=true;
    		if(isGregorian(BEGINNING_DATE) && isGJ)
    		//-10 days if true
    		{
    			additionalDays-=10;
    			if(additionalDays<1)
    			{
    				monthNum--;
    				if(monthNum<1)
    				{
    					additionalYears--;
    					monthNum=12;
    				}
    			//	additionalDays=30+(monthNum+(monthNum>7?1:0))%2-(monthNum==2&&isLeapYear(additionalYears)?1:2)+additionalDays;
    			additionalDays=daysInMonth(monthNum,additionalYears)+additionalDays;
    			}
    		}
    		
    		///////////////////////////////////////////System.out.println(30+(monthNum+(monthNum>7?1:0))%2-(monthNum==2&&isLeapYear(additionalYears)?1:2));
    		//10 days further - from Jul to Greg
    		isGJ=false;//I could use isGregorian(Day k), but then I would have to create an object
    		//I could overload isGregorian, but then I would use it only here
    			if(additionalYears>1582)
    				isGJ=true;
    			else if(additionalYears==1582)
    				if(monthNum>10)
    					isGJ=true;
    				else if(monthNum==10)
    					if(additionalDays>4)
    						isGJ=true;
    				//+10 days if true
    		if(!isGregorian(BEGINNING_DATE)&&isGJ)//.smallerThan(5,10,1582)&&isGJ)
    		{
    			additionalDays+=10;
    			if(additionalDays>daysInMonth(monthNum,additionalYears))//(30+(monthNum+(monthNum>7?1:0))%2-(monthNum==2&&isLeapYear(additionalYears)?1:2)-additionalDays));
    			{
    				additionalDays-=daysInMonth(monthNum,additionalYears);
    				monthNum++;
    				if(monthNum>12)
    				{
    					additionalYears++;
    					monthNum=1;
    				}
    			}
    		}
    				
    		//assign vars
    		Y=additionalYears;
    		M=monthToEnum(monthNum);
    		D=additionalDays;
    	}
    	
    	public Day(int dayOfYear, int year)
    	{
    		if(dayOfYear<1) throw new IndexOutOfBoundsException("Day cannot be smaller than 0");
    		if(dayOfYear>366)	throw new IndexOutOfBoundsException("Number of days too big");
    		if((dayOfYear == 366)&&(!isLeapYear(year)))	throw new IndexOutOfBoundsException("Number of days too big");
    		
    		//find month and day of month
    		int monthNum=0;
    		int daysNum=0;
    		int oldNumDays=0;//just to init
    		while(daysNum<dayOfYear)
    		{
    			monthNum++;
    			oldNumDays=daysNum;
    			if(monthNum<8)	daysNum+=30+monthNum%2;
    			else	daysNum+=31-monthNum%2;
    			if(monthNum==3)
    				if(isLeapYear(year))
    					daysNum-=1;
    				else
    					daysNum-=2;
    		}
    		//check Greg.
    		dayOfYear-=oldNumDays;
    		if(year==1582 &&
    			monthNum==10 &&
    				dayOfYear>=5 && dayOfYear<=14)
    					throw new IndexOutOfBoundsException("Day of week was removed from this year (change to Gregorian from Julian calendar)");
    		//assign			
    		Y=year;
    		M=monthToEnum(monthNum);
    		D=dayOfYear;
    	}
    	
    	public Day(String dayOfWeekStr, int weekOfYear, int year)
    	{
    		DaysOfWeek dayOfWeek=weekToEnum(dayOfWeekStr);
    		int dayOfYear=1-(new Day(1,1,year).dayOfWeekInt());
    		dayOfYear+=7*(weekOfYear-1);
    		dayOfYear+=dayOfWeek.getInt();
    		if(dayOfYear<1)
    			throw new IndexOutOfBoundsException(dayOfWeek.getString()+" doesn't exist on week "+weekOfYear);
    		if(dayOfYear>366 || (dayOfYear==366 && !isLeapYear(year)))
    			throw new IndexOutOfBoundsException("Last week of "+year+" doesn't have "+dayOfWeek.getString());
    		//find month and day of month
    		int monthNum=0;
    		int daysNum=0;
    		int oldNumDays=0;//just to init
    		while(daysNum<dayOfYear)
    		{
    			monthNum++;
    			oldNumDays=daysNum;
    			if(monthNum<8)	daysNum+=30+monthNum%2;
    			else	daysNum+=31-monthNum%2;
    			if(monthNum==3)
    				if(isLeapYear(year))
    					daysNum-=1;
    				else
    					daysNum-=2;
    		}
    		//check Greg.
    		dayOfYear-=oldNumDays;
    		if(year==1582 &&
    			monthNum==10 &&
    				dayOfYear>=5 && dayOfYear<=14)
    					throw new IndexOutOfBoundsException("Day of week was removed from this year (change to Gregorian from Julian calendar)");
    		//assign
    		Y=year;
    		M=monthToEnum(monthNum);
    		D=dayOfYear;
    	}
    	
    	public Day(DaysOfWeek dayOfWeek, int weekOfYear, int year)
    	{
    		int dayOfYear=1-(new Day(1,1,year).dayOfWeekInt());
    		dayOfYear+=7*(weekOfYear-1);
    		dayOfYear+=dayOfWeek.getInt();
    		if(dayOfYear<1)
    			throw new IndexOutOfBoundsException(dayOfWeek.getString()+" doesn't exist on week "+weekOfYear);
    		if(dayOfYear>366 || (dayOfYear==366 && !isLeapYear(year)))
    			throw new IndexOutOfBoundsException("Last week of "+year+" doesn't have "+dayOfWeek.getString());
    		//find month and day of month
    		int monthNum=0;
    		int daysNum=0;
    		int oldNumDays=0;//just to init
    		while(daysNum<dayOfYear)
    		{
    			monthNum++;
    			oldNumDays=daysNum;
    			if(monthNum<8)	daysNum+=30+monthNum%2;
    			else	daysNum+=31-monthNum%2;
    			if(monthNum==3)
    				if(isLeapYear(year))
    					daysNum-=1;
    				else
    					daysNum-=2;
    		}
    		//check Greg.
    		dayOfYear-=oldNumDays;
    		if(year==1582 &&
    			monthNum==10 &&
    				dayOfYear>=5 && dayOfYear<=14)
    					throw new IndexOutOfBoundsException("Day of week was removed from this year (change to Gregorian from Julian calendar)");
    		//assign
    		Y=year;
    		M=monthToEnum(monthNum);
    		D=dayOfYear;
    	}
    	
    	public Day(int dayOfMonth, int month, int year)
    	{
    		if(dayOfMonth>daysInMonth(month,year) || dayOfMonth<1)
    		{
    			throw new IndexOutOfBoundsException("Day number invalid");
    		}
    		//check Greg.
    		if(year==1582 &&
    			month==10 &&
    				dayOfMonth>=5 && dayOfMonth<=14)
    					throw new IndexOutOfBoundsException("Date does not exist (change to Gregorian from Julian calendar)");
    		//assign
    		D=dayOfMonth;
    		M=monthToEnum(month);
    		Y=year;
    	}
    	
    	//additional functions
    	private int daysInMonth(int month, int year)
    	{
    		int days;
    		if(month<8)
    			if(month==2)
    				return days=isLeapYear(year)?29:28;
    			else
    				return days=30+month%2;
    		return days=31-month%2;
    	}
    	
    	private boolean isGregorian(Day d)
    	{
    		if(d.Y>1582)
    			return true;
    		if(d.Y==1582)
    			if(d.M.getInt()>10)
    				return true;
    			if(d.M.getInt()==10)
    				if(d.D>=15)
    					return true;
    		return false;
    	}
    	
    	private Months monthToEnum(String s)
    	{
    		for (Months p : Months.values())
    		{
       			if(p.getString()==s) return p;
    		}
    		throw new IllegalArgumentException("name of month invalid");
    	}
    		
    	private Months monthToEnum(int i)
    	{
    	//	System.out.println(i);
    		for (Months m : Months.values())
    		{
        		if(m.getInt()==i) return m;
    		}
    		throw new IllegalArgumentException("number of month invalid");
    	}
    	
    	private DaysOfWeek weekToEnum(String i)
    	{
    		for (DaysOfWeek m : DaysOfWeek.values())
    		{
        		if(m.getString()==i) return m;
    		}
    		throw new IllegalArgumentException("number of week invalid");
    	}
    		
    	private DaysOfWeek weekToEnum(int i)
    	{
    		for (DaysOfWeek m : DaysOfWeek.values())
    		{
        		if(m.getInt()==i) return m;
    		}
    		throw new IllegalArgumentException("number of week invalid");
    	}
    	
    	private boolean isLeapYear(int yr)
    	{
    		if ((yr%4 == 0) && (yr%100 != 0) || (yr%400 == 0))
            	return true;
         	else
            	return false;
    	}
    	
    	public int dayOfWeekInt()
    	{
    		int numDiff=daysBetween(BEGINNING_DATE);
    	/*	if(numDiff<0)
    			return weekToEnum((BEGINNING_WEEKDAY.getInt()-1-numDiff)%7+1-(!isGregorian(BEGINNING_DATE)&&isGregorian(this)?10:0)).getInt();
    		else 
    			return weekToEnum((BEGINNING_WEEKDAY.getInt()-1+numDiff)%7+1+(!isGregorian(BEGINNING_DATE)&&isGregorian(this)?10:0)).getInt();*/
    		int k=(BEGINNING_WEEKDAY.getInt()-1-numDiff)%7+1;
    		if(k<1)
    			return weekToEnum(k+7).getInt();
    		else return weekToEnum(k).getInt();
    	}
    	
    	//compulsory functions
    	public String dayOfWeek()
    	{
    		int numDiff=daysBetween(BEGINNING_DATE);
    		System.out.println(numDiff);
    		/*if(numDiff<0)//i think this should just be deleted!
    			return weekToEnum((BEGINNING_WEEKDAY.getInt()-1+numDiff-((!isGregorian(BEGINNING_DATE)&&isGregorian(this))?10NIEEEEEEEEE:0))%7+1).getString();
    		else 
    			return weekToEnum((BEGINNING_WEEKDAY.getInt()-1+numDiff+((!isGregorian(BEGINNING_DATE)&&isGregorian(this))?10:0))%7+1).getString();*/
    		int k=(BEGINNING_WEEKDAY.getInt()-1-numDiff)%7+1;
    		if(k<1)
    			return weekToEnum(k+7).getString();
    		else return weekToEnum(k).getString();
    	}
    	
    	public String month()
    	{
    		return M.getString();
    	}
    	
    	public String season()//do using IF
    	//should i use month names?
    	{
    		switch(M)
    		{
    			case JANUARY:
    			case FEBRUARY:
    				return Seasons.WINTER.getString();
    			case DECEMBER:
    				if(D>=22) return Seasons.WINTER.getString();
    				else return Seasons.AUTUMN.getString();
    			case MARCH:
    				if(D>=21) return Seasons.SPRING.getString();
    				else return Seasons.WINTER.getString();
    			case APRIL:
    			case MAY:
    				return Seasons.SPRING.getString();
    			case JUNE:
    				if(D>=22) return Seasons.SUMMER.getString();
    				else return Seasons.SPRING.getString();
    			case JULY:
    			case AUGUST:
    				return Seasons.SUMMER.getString();
    			case SEPTEMBER:
    				if(D>=23) return Seasons.AUTUMN.getString();
    				else return Seasons.SUMMER.getString();
    			case OCTOBER:
    			case NOVEMBER:
    				return Seasons.AUTUMN.getString();
    			default:
    				return "empty";
    		}
    	}
    
    	public String academicSeason()//co oznacza HOLIDAYS? i didn't count the shorter
    	{
    		//winter
    		if(M.getInt()==2&&D>=22&&D<=28)
    			return AcademicTerms.HOLIDAYS.getString();
    		if(M.getInt()==2&&D>=15&&D<=20)//second wave of exams
    			return AcademicTerms.EXAMS.getString();
    		if(M.getInt()==2&&D<=13)
    			return AcademicTerms.EXAMS.getString();
    		if(M.getInt()==11&&D>=11&&D<=15)
    			return AcademicTerms.HOLIDAYS.getString();
    		if((M.getInt()==12&&D>=20)||(M.getInt()==1&&D<=3))
    			return AcademicTerms.HOLIDAYS.getString();
    		if((M.getInt()==9 && D>=30)||(M.getInt()>9)||(M.getInt()==1&&D<=30))
    			return AcademicTerms.WINTER_TERM.getString();
    			
    		//summer
    		if(M.getInt()==9&&D<=11)
    			return AcademicTerms.EXAMS.getString();//second wave
    		if((M.getInt()==6&&D>=22)||(M.getInt()==7&&D<=2))
    			return AcademicTerms.EXAMS.getString();
    		if(M.getInt()==4&&D>=2&&D<=7)
    			return AcademicTerms.HOLIDAYS.getString();
    		if((M.getInt()==3)||(M.getInt()>3&&M.getInt()<6)||(M.getInt()==6&&D<=21))
    			return AcademicTerms.SUMMER_TERM.getString();
    				
    		return AcademicTerms.HOLIDAYS.getString();
    	}
    	
    	public int dayOfYear()
    	{
    		int numDays=31*(M.getInt()-1)-(M.getInt()-1)/2;//31 30 31 30 31 30 31|31 30 31 30 31
    		if(M==Months.SEPTEMBER || M==Months.NOVEMBER) numDays++;
    		//after Feb
    		if(M.getInt()>2)
    			if(isLeapYear())
    				return numDays-1+D;
    			else
    				return numDays-2+D;
    		return numDays+D;
    	}
    	
    	public int daysLeft()
    	{
    		return (isLeapYear()?366:365)-dayOfYear();
    	}
    	
    	public int weekOfYear()//monday is the first day of week
    	{
    		return (int)((dayOfYear()-dayOfWeekInt())/7)+2;
    	}
    	
    	public int weeksLeft()
    	{
    		return (int)((daysLeft()+1-8+dayOfWeekInt())/7)+1;
    	} // the number of weeks left till the end of the year, without current week!
    	
    	public int monthOfYear()
    	{
    		return M.getInt();
    	}
    	
    	public int monthsLeft()
    	{
    		return 12-M.getInt();
    	}
    	 
    	public int year()
    	{
    		return Y;
    	}
    	
    	public boolean isLeapYear()
    	{
    		if ((Y%4 == 0) && (Y%100 != 0) || (Y%400 == 0))
            	return true;
         	else
            	return false;
    	}
    	
    	public int daysBetween(Day day)
    	{
    		//leap additions
    		int bYear;
    		int sYear;
    		if(Y>day.Y)
    		{
    			bYear=Y-1;//triple check!
    			sYear=day.Y;
    		}
    		else
    		{
    			sYear=Y;
    			bYear=day.Y-1;
    		}
    		int numLeap=0;
    		//div by 4
    		int temp=bYear-bYear%4;
    		if(temp>sYear)
    		{
    			numLeap=(int)((temp-sYear)/4)+1;
    		}
    		//div by 100
    		temp=bYear-bYear%100;
    		if(temp>sYear)
    		{
    			numLeap-=(int)((temp-sYear)/100)+1;
    		}
    		//div by 400
    		temp=bYear-bYear%400;
    		if(temp>sYear)
    		{
    			numLeap+=(int)((temp-sYear)/400)+1;
    		}
    		int numDays=365*(day.Y-Y)-dayOfYear()+day.dayOfYear();
    		//returns and check Gregorian	
    		
    		if(numDays<0)
    		{
    			if(!(isGregorian(this)&&isGregorian(day))) numDays+=10;//will it work for weekOfYear and other?
    			return numDays-numLeap;
    		}
    		else
    		{
    			if(!(isGregorian(this)&&isGregorian(day))) numDays-=10;
    			return numDays+numLeap;
    		}
    	};
    	
    	public static void main(String[] args)
    	{
    		/*Day k=new Day(26,12,2009);
    		System.out.println("day: "+k.D);
    		System.out.println("month: "+k.M.getString());
    		System.out.println("year: "+k.Y);
    		System.out.println("Day of week: "+k.dayOfWeek());
    		System.out.println("month: "+k.month());
    		System.out.println("season: "+k.season());
    		System.out.println("academic season: "+k.academicSeason());//add ALL holidays!
    		System.out.println("day of year: "+k.dayOfYear());
    		System.out.println("days till end of year: "+k.daysLeft());
    		System.out.println("week of year: "+k.weekOfYear());//start of week is Monday, so it's the 52nd week
    		System.out.println("weeks left: "+k.weeksLeft());//if week didn't end then it is also counted
    		System.out.println("month of year: "+k.monthOfYear());
    		System.out.println("months left: "+k.monthsLeft());
    		System.out.println("year: "+k.year());
    		System.out.println("is it leap: "+k.isLeapYear());
    		System.out.println("number of days left till: "+k.daysBetween(BEGINNING_DATE));
    		
    		System.out.println("KKK");
    		System.out.println(new Day(270).D);
    		System.out.println(new Day(270).M.getString());
    		System.out.println(new Day(270).Y);
    		System.out.println();
    		System.out.println(new Day(645).D);
    		System.out.println(new Day(645).M.getString());
    		System.out.println(new Day(645).Y);
    		System.out.println();
    		System.out.println(new Day(127,1840).D);
    		System.out.println(new Day(127,1840).M.getString());
    		System.out.println(new Day(127,1840).Y);
    		System.out.println();
    		System.out.println(new Day(DaysOfWeek.WEDNESDAY,23,2000).D);
    		System.out.println(new Day(DaysOfWeek.WEDNESDAY,23,2000).M.getString());
    		System.out.println(new Day(DaysOfWeek.WEDNESDAY,23,2000).Y);
    		System.out.println();
    		System.out.println(new Day(127,1840).dayOfWeek());
    		System.out.println();
    		System.out.println(new Day(DaysOfWeek.WEDNESDAY,23,2000).dayOfWeek());
    		System.out.println();
    		System.out.println(new Day(-(BEGINNING_DATE.dayOfYear()-3)).D);
    		System.out.println(new Day(-(BEGINNING_DATE.dayOfYear()-3)).M.getString());
    		System.out.println(new Day(-(BEGINNING_DATE.dayOfYear()-3)).Y);*/
    		System.out.println(BEGINNING_DATE.dayOfYear());
    		
    		System.out.println("1"+new Day(1,1,1600).dayOfWeek());
    		System.out.println("2"+new Day(26,12,2009).dayOfWeek());
    		System.out.println("3"+new Day(4,10,1582).dayOfWeek());
    		System.out.println("1AAA"+new Day(1,1,1600).daysBetween(BEGINNING_DATE));
    		System.out.println("2AAA"+new Day(26,12,2009).daysBetween(BEGINNING_DATE));
    		//System.out.println("3"+new Day(1,1,1600).daysBetween(new Day(15,10,1582)));
    		//System.out.println(new Day(1,1,1500).daysBetween(new Day(15,10,1582)));
    	//	System.out.println(new Day(15,10,1582).daysBetween(new Day(1,1,1600)));
    	//	System.out.println(new Day(1,1,1600).daysBetween(new Day(15,10,1582)));
    		
    		//System.out.println(new Day(15,10,1582).daysBetween(new Day(26,12,2009)));
    		//System.out.println(new Day(26,12,2009).daysBetween(new Day(15,10,1582)));
    		
    		System.out.println("DDD"+new Day(20,5,1000).dayOfWeek());
    		System.out.println("DDD"+new Day(10,12,500).dayOfWeek());
    		System.out.println("DDD"+new Day(20,5,1582).dayOfWeek());
    		System.out.println("DDD"+new Day(31,12,1111).dayOfWeek());
    		System.out.println("DDD"+new Day(27,12,2009).dayOfWeek());
    	}
    }

  2. #2
    Supamagier is offline Senior Member
    Join Date
    Aug 2008
    Posts
    384
    Rep Power
    7

    Default

    Wow.

    I didn't exactly read through all of it, but noticed one big thing.
    You can greatly shorten up your code.
    For example, you really need about one constructor with code, and you can use this in other constructors:
    Java Code:
    public Day(params) { this(params,additional params); }
    So the code that does all the stuff it should do, should be placed in the constructor with the most parameters, so that you can call that constructor from the others, whereas you can fill in the params the user didn't need to fill in yourself, with the default values.

    Also, with the methods you can do this little trick.
    Java Code:
    public int daysOfWeekInt() { // code to calculate }
    public String daysOfWeekString() { return String.valueOf(daysOfWeekInt()); }
    same with for example dayToEnum():
    Java Code:
    public Day dayToEnum(String s) { // code to calculate }
    public Day dayToEnum(int i) { return dayToEnum(String.valueOf(i)); }
    Hope this helps.

    Good luck,

    ~Matt

    P.S.: The method names are probably not as you have them, but it's a lot of code, and I didn't wanna search for the methods again to figure the correct name. I suppose I made my point with the examples though.
    I die a little on the inside...
    Every time I get shot.

  3. #3
    omygoodness is offline Member
    Join Date
    Jan 2010
    Posts
    10
    Rep Power
    0

    Default

    Maybe you will write new version of this program for me? :) I have lost in my own lines... I am beginner in java so everything is so complicated.

  4. #4
    Supamagier is offline Senior Member
    Join Date
    Aug 2008
    Posts
    384
    Rep Power
    7

    Default

    Sorry, I won't do that. ;) Both because I don't feel like doing your homework and because I'm just too busy (and that's not just a lame excuse).

    Good luck, :)

    ~Matt
    I die a little on the inside...
    Every time I get shot.

Similar Threads

  1. How to add a calendar????
    By zifis in forum New To Java
    Replies: 5
    Last Post: 04-07-2009, 04:04 PM
  2. Calendar bug?
    By Stigvig in forum Advanced Java
    Replies: 14
    Last Post: 02-08-2009, 09:56 AM
  3. calendar
    By John in forum SWT / JFace
    Replies: 12
    Last Post: 08-07-2008, 10:54 PM
  4. Calendar.DATE
    By mew in forum New To Java
    Replies: 1
    Last Post: 01-04-2008, 07:51 PM
  5. Web calendar
    By Daniel in forum Enterprise JavaBeans (EJB)
    Replies: 1
    Last Post: 06-27-2007, 05:36 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •