Uni Programing Help

Soldato
Joined
11 Apr 2003
Posts
4,218
Location
Notts
No, im not asking anyone to do my work for me before you ask! I have been asked to:
Code:
This part of the assignment is based on writing and testing a small class to
represent dates.

Your report should contain an annotated copy of the source code, along
with description of the steps that you used to test your program.

• Write a class declaration for Date, representing a Date as day, month,
and year. Use appropriate access control.

• Write a method void set(int d, int m, int y) which sets a Date
to a particular value.

• Write a member function called output which displays the value of a
Date in suitable form.

• Write member functions to add and subtract years and months from a
Date

• Write member functions tomorrow() and yesterday() that add or
subtract one day from a Date. Take care to handle the beginning and
end of months and years correctly.

• Write a member function that converts a Date into the number of days
before/after the first of January 1900. Use this function to calculate
your age in days.

Note that your marks for this section will also depend on the quality
of your code, in terms of comments, layout, naming conventions, and other
stylistic points.

So far I have the basic system in place, I am able to input a date, which will be written to an int, and then I return the inputed values.

Reading this, I do not see that I have to create an entire calendar (Nor do I know how!) However I was just wondering if you would say the same, and wheter or not people think it would be ok to import a Calendar class for use in my program, to corectly return dates, allow me to add and subtract days/months/years etc.

If yes, does anyone know where I would find Calendar.Java as my searches simply return descriptions about the class, but not the actual Calendar.Java class...

Thanks :)
 
Last edited:
sam83uk said:
im not 100pc sure what you're trying to do but if you wanna cover the whole year with these dates why not

My_DateClass calender[12][31];

access 22 of january - calender[0][22].output()

(c++ btw, i dont know java)
Yeah I was thinking of creatin an array, however I would have to write a portion of calendar in order to account for gap years
 
Currently trying to get the .output and .set bits working got

Code:
    public void set(){
        int m, d, y = TextIO.getInt();
    }
    public void output(){
       
        System.out.print(dates);
        System.out.print("/");

I cant get set to work, as it tells me that it does not understand m, etc when i type:
Code:
Test.set(m);

I also cannot get output to output the date of month, day and year at the same time rather than going:

Code:
Test.output();
Test1.output();
Test2.output();

Each time, it would be better to just get the output in one line of code
 
Phnom_Penh said:
Erm use a scanner for input.
As for output, System.out.println(day + "/" + month + "/" + year); tbh.
thanks, just found this in some of our lecture notes, think it be worth me revising them some more before I sit scratching my head for a few hours!

Having said that, it still does not like it, as I do not define day, month, and year in Date, but in the sub classes of Day, Month and Year
 
Last edited:
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:
 
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
 
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.....
 
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!
 
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
 
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
 
Hi all im trying to convert 24 if statements into 2 for loops, This is what they look like:

Code:
        for(int counter=monthBirth;counter<monthBirth;counter++){
            monthBirth++;
            monthTotal = monthTotal+daysInMonth[monthBirth];
            
        }        
        for(int counter=month;counter<month;counter++){
            System.out.println(counter);
            month++;
            monthTotalFinal = monthTotalFinal+daysInMonth[month];
        }

However they do nothing for some reason, and no output is printed for them.... Anyone hint what im doing wrong please?
 
Code:
        int counter1 = monthBirth;
        int counter2 = month;
        
        for(int counter=counter1;counter<monthBirth;counter++){
            counter1++;
            monthTotal = monthTotal+daysInMonth[monthBirth];
            
        }        
        for(int counter=counter2;counter<month;counter++){
            System.out.println(counter);
            counter++;
            monthTotalFinal = monthTotalFinal+daysInMonth[month];
        }

Still returns nothing at all
 
Back
Top Bottom