Uni Programing Help

Chrisss said:
jcb getting others to do work for him I see :cool:
Good practice, I haven't done any for a while, and I have some CW coming up.

According to this I was born 32425 days after the year 1900.

Code:
		System.out.println(day + "/" + month + "/" + year);
		days = day;	
		switch(month)
		{
		case 1: break;
		case 2: days = days + 31; break;
		case 3: days = days + 59; break;
		case 4: days = days + 90; break;
		case 5: days = days + 121; break;
		case 6: days = days + 151; break;
		case 7: days = days + 181; break;
		case 8: days = days + 212; break;
		case 9: days = days + 243; break;
		case 10: days = days + 273; break;
		case 11: days = days + 304; break;
		case 12: days = days + 334; break;	
		}
		int years = year;
		while(years != 1900)
		{
			days = days + 365;
		if(((year % 4 == 0)&&(year % 100 != 0))||(year % 400 == 0))
		{
			days = days + 1;
			years = years - 1;
			}
		}
		System.out.println(days);
 
Phnom_Penh said:
Good practice, I haven't done any for a while, and I have some CW coming up.

According to this I was born 32425 days after the year 1900.

Code:
		System.out.println(day + "/" + month + "/" + year);
		days = day;	
		switch(month)
		{
		case 1: break;
		case 2: days = days + 31; break;
		case 3: days = days + 59; break;
		case 4: days = days + 90; break;
		case 5: days = days + 121; break;
		case 6: days = days + 151; break;
		case 7: days = days + 181; break;
		case 8: days = days + 212; break;
		case 9: days = days + 243; break;
		case 10: days = days + 273; break;
		case 11: days = days + 304; break;
		case 12: days = days + 334; break;	
		}
		int years = year;
		while(years != 1900)
		{
			days = days + 365;
		if(((year % 4 == 0)&&(year % 100 != 0))||(year % 400 == 0))
		{
			days = days + 1;
			years = years - 1;
			}
		}
		System.out.println(days);
How do people program that in about an hour, when i st for around 8 and get hardly anyhwere :confused:
 
jcb33 said:
How do people program that in about an hour, when i st for around 8 and get hardly anyhwere :confused:
lol, it took about 10 mins, I spent most of the time removing the worlds biggest mosquito from my room :p. If I was doing it for cw it would take longer because I'd test it and put in error messages etc.
 
Phnom_Penh said:
lol, it took about 10 mins, I spent most of the time removing the worlds biggest mosquito from my room :p. If I was doing it for cw it would take longer because I'd test it and put in error messages etc.
Any tips on how to get decent at java? Been doing exercises and such, but not getting much better
 
Use Eclipse for starters, if you're still using BlueJ. Best thing is to get some decent exercises, tough ones though, not step by step ones. The main way I learnt was at the last minute so that my coursework would compile. :p
 
jcb33 said:
Any tips on how to get decent at java? Been doing exercises and such, but not getting much better

Write something non trivial which you may find useful and fun to write. I am currently working on a project which has 50+ class files and it really forces you to structure your code well else it gets pretty hard to refactor.
 
Hey all, im currently trying to account for the gap year, I so far have:

Code:
int leapYearTarget = yearBirth;

          int leapYearTarget2 = year;  

        

        for(int counter=leapYearTarget;counter<leapYearTarget2;counter++){

            if(leapYearTarget % 4 == 0 && leapYearTarget % 100 != 0 ){

                dayTotal++;

            }

            else if(leapYearTarget % 400 == 0){

                dayTotal++;

            }

        }

        

        System.out.println(dayTotal);

Which returns: 0!

yearBirth = the year of birth entered (I Put 1987) and year = the year to represent todays date (2007) Which is also entered by the user....

Am I missing something, as I dont want it to add 0.....
 
Not sure what your problem is - could you go into more detail?

Maybe it's because leapYearTarget(always 1987) has the same value for every iteration of the loop?

Shouldn't you be doing the % of counter instead?
 
Phnom_Penh said:
Good practice, I haven't done any for a while, and I have some CW coming up.

According to this I was born 32425 days after the year 1900.

Code:
	     int years = year;
		while(years != 1900)
		{
			days = days + 365;
		if(((year % 4 == 0)&&(year % 100 != 0))||(year % 400 == 0))
		{
			days = days + 1;
			years = years - 1;
			}
		}
		System.out.println(days);
Sure that works? algorithmically, you shouldn't be counting 'year' as a leap year if the month is January or February (because you haven't got to the extra day yet).
 
Chrisss said:
Not sure what your problem is - could you go into more detail?

Maybe it's because leapYearTarget(always 1987) has the same value for every iteration of the loop?

Shouldn't you be doing the % of counter instead?

The problem is I want it to add 4 if its divisible by 4, if its not a century. But it keeps returning a 0.

It should go Counter =1987, yes coutner is less than 2007, add 1 to counter.

Counter does not meet requirements!

dayTotal = 0

Counter = 1988, yes counter is less than 2007, add 1 to counter.

Counter meets requirements!

dayTotal = 1

But its not doing anything it seems!
 
Add this line to the beginning of your loop and you should see what's going wrong(which is what I said in my first post)

System.out.println(leapYearTarget);
 
Chrisss said:
Add this line to the beginning of your loop and you should see what's going wrong(which is what I said in my first post)

System.out.println(leapYearTarget);
Hmmmm!

Please Input Today's Year

? 2007
Your Date Of Birth Is: 5/29/1987

Today's Date Is: 2/27/2007

1987

1987

1987

1987

1987

1987

1987

1987

1987

1987

1987

1987

1987

1987

1987

1987

1987

1987

1987

1987

0

7209
 
Thanks, just seing the problem made me think duh and fix it!

Code:
         int leapYearTarget = yearBirth;
        int leapYearTarget2 = year;  
        
        for(int counter=leapYearTarget;counter<leapYearTarget2;counter++){
            System.out.println(leapYearTarget);
            leapYearTarget++;
            if(leapYearTarget % 4 == 0 && leapYearTarget % 100 != 0 ){
                dayTotal++;
            }
            else if(leapYearTarget % 400 == 0){
                dayTotal++;
            }
        }


Code:
Your Date Of Birth Is: 5/29/1987

Today's Date Is: 2/27/2007

1987

1988

1989

1990

1991

1992

1993

1994

1995

1996

1997

1998

1999

2000

2001

2002

2003

2004

2005

2006

5

7214
 
There are some very compact formulas for this around. I had a little play myself, but this was the best I could manage (in C/C++ but should work in Java I think):

Code:
int Days(int d, int m, int y)
{
  y-=m<3;
  return d+(595+306*(m<3?m+9:m-3))/10+y*365+y/4-y/100+y/400-693960;
}
I believe this works, but I haven't checked too carefully against other sources.

Comment #1: I thought it would be easy to check against Excel, but Excel thinks 1900 was a leap year! Yay Microsoft!
Comment #2: I really want to see what happens if jcb33 uses this in his coursework!
 
DaveF said:
Sure that works? algorithmically, you shouldn't be counting 'year' as a leap year if the month is January or February (because you haven't got to the extra day yet).
No, it was put together in 10 mins as an example, not a model ;), as I used year and not years in the if statement, so it would add a day for every year, not every leap year :\. What you said is quite right, although it's harder to fix than it'd seem :p.
 
Last edited:
Phnom_Penh said:
What you said is quite right, although it's harder to fix than it'd seem :p.
Easiest way is to regard Jan and Feb as months 13 and 14 of the previous year.

If you look at the lengths of the months, there's a fair bit of mathematical evidence that the calender originally considered March as the 1st month; you find that starting with that point, if you take the length of a month as 30.6 days and round appropriately to get whole numbers, you get all the other lengths of the months except February, which always feels a bit like the "forgotten" month stuck on the end anyway. And where better to stick a leap day than on the last day of the year?

(And of course this tallies nicely with the linguistic evidence of September having once been the 7th month, and October the 8th month...)
 
DaveF said:
There are some very compact formulas for this around. I had a little play myself, but this was the best I could manage (in C/C++ but should work in Java I think):

Code:
int Days(int d, int m, int y)
{
  y-=m<3;
  return d+(595+306*(m<3?m+9:m-3))/10+y*365+y/4-y/100+y/400-693960;
}
I believe this works, but I haven't checked too carefully against other sources.

Comment #1: I thought it would be easy to check against Excel, but Excel thinks 1900 was a leap year! Yay Microsoft!
Comment #2: I really want to see what happens if jcb33 uses this in his coursework!
Sorry not going to be using anything that is actualy given to me :) However I am a bit flumexed atm!

I have:

Code:
int[] dayListing = {-1,31,28,31,30,31,30,31,31,30,31,30,31};

I want to be able to call the data from there for the following:

Code:
 boolean BirthNull = false;
        
        while(!BirthNull){
            Birth = TextIO.getInt();
            if(monthBirth == 1){
                if(dayBirth >31){
                    System.out.println("Im Sorry This Month Is Invalid (To High), Please Try Again!");
                    BirthNull = false;
                }        
                else if(daysInMonth[1] <1){
                    System.out.println("Im Sorry This Month Is Invalid (To Low), Please Try Again!");
                    BirthNull = false;
                }
                else{
                    BirthNull = true;
                }
            }

Doing it the way I have above is not that good as im just having an array sit there doing nothing for the most part of my code, and I need to be able to do a similar thing later on in the code to make sure that when you add 1 to 28th feb it becomes the 1st march!
 
Chrisss said:
So what's the problem?
well I have it working, but I am not sure how I can make it so it knows:

If(Month == 2) And If(Day == 28) Day++ = Month 3, Day 1, instead of 29th Feb...

Without repeating a load of if statements
 
jcb33 said:
Code:
int[] dayListing = {-1,31,28,31,30,31,30,31,31,30,31,30,31};
Not knowing Java, I'll assume the syntax is valid even though it looks wrong to me. My first question would be: why have you called it dayListing? What's it got to do with lists?

I want to be able to call the data from there for the following: (code snipped)
You do know what the point of an array is? And how you access elements in the array?
 
Back
Top Bottom