Hi, I'm creating a class and want to write a method that prints the time an object was created. Is there any way to do this?
Thanks in advance!!
Kumalh.
Printable View
Hi, I'm creating a class and want to write a method that prints the time an object was created. Is there any way to do this?
Thanks in advance!!
Kumalh.
Also, how do I return the object name?
Capture and save the current time in the objects constructor.Quote:
the time an object was created
Not sure what the object's "name" is.Quote:
return the object name
Do you mean the name of the class? See the getClass() method.
Or the name of the variable that holds the reference to the object. Pass that to the constructor as a String.
How do I do this? And do I save it as a String?Quote:
Capture and save the current time in the objects constructor.
By name I mean the name of the instance. So say you created a new Calendar object and named it 'Cal' (e.g. cal = new Calendar(4, 8, 2011) )Quote:
Not sure what the object's "name" is.
So I think that is the variable that holds the reference to the object. Correct?
Norm has answered these questions. For the time, check out the API for System, or Calendar, or Date. Or do some googling. If something you see confuses you, create an SSCCE that tests it out and post it here.
You can't get the variable name. You can pass a String to the constructor, as Norm already said. You also might want to look into using a Map and using the String names as the keys and the Calendars the values.
Ok, I'll check out Calendar and Date. Thanks.
Also, I'm writing my own Calendar class (I know one already exists, but I'm required to write my own. So I can't use any methods from the pre-existing calendar class).
I've written all of my constructors, but I've come across a problem.
Code :// Constructor for calendar given the day, month, and year. public Calendar(int d, int m, int y) { date = d; month = m; year = y; }
So here is one of the constructors of the class (they are all fairly similar, just putting in default values when they aren't given). Now, obviously the date given can't exceed the number of days in that month, nor can the given month exceed the number of months in a year. How do I return an error when somebody tries to do this?
For example, if somebody tries to createhow do I prevent this?Code :Calendar cal = new Calendar(5, 13, 2011);
What do you want to happen? Do you want to throw an Exception? Then throw one from the constructor. Do you want to show an error message? If so, check the input before you even call the constructor. You could even do both, I suppose.
Ok, so I've sorted out all of that stuff. Thank you for your help.
I have another problem that I can't get though. I'm not sure if I should start a new post or not (I'm new - I apologise), so I'll post it here.
One of the required methods for a calendar object is the ability to roll the calendar date forward or backward by a given number of days. So far, I have this:
Code :private void addOne(boolean up) { if(up == true) date = date + 1; else date = date - 1; } public void rollDays(int d) { while(d != 0) { if(d > 0){ addOne(true); d = d - 1; } else { addOne(false); d = d + 1; } }
Now my problem is getting the method to recognise when to increase the month. So for example, if rolling forward 10 days from the 25th may, how do I recognise when it reaches the 31st, then start rolling again from the 1st of june.. Similarly, how do I recognise when rolling backward. Like when it hits the 1st of the month and needs to go to the last day of the previous month.
I also have a method that finds the number of days in each month, and whether it is a leap year or not. I imagine these will need to be used, just don't know where...
Well, how do you do this by hand or in your head? Pretend you have a friend who has never seen a calendar before. Write out instructions that he could follow to do the task, and you'll have an algorithm that should be pretty easy to translate to code.
You seem to mean well, but people don't like crossposting:
This thread has been cross posted here:http://www.java-forums.org/new-java/47220-rolling-forward-days-calendar.html
Although cross posting is allowed, for everyone's benefit, please read:
Ahh sorry about the cross posting. This is my first time using forums so I'm not too familiar with all of that stuff. Like you said, I only meant well. In future I'll be sure to be more careful. Again, I apologise :)
So I've written this:
Code :public void rollDaysTEST(int d) { int count = d; for(int i = 0; i == d; i = i + 1){ date = date + 1; if(date > getDaysInCurrentMonth()){ month = month + 1; date = count; } else count = count - 1; } }
However, when I call it, nothing happens. The date value doesn't get increased. What am I missing?
I'm not sure what you expect that code to do. Perhaps an SSCCE, or at least some comments, would help us understand it?
Sorry about that. If my commenting makes no sense, I apologise. It's been a very, very long night.
Code :// Method rolls the date of the calendar forward by a given number of days public void rollDaysTEST(int d) { int count = d; for(int i = 0; i == d; i = i + 1){ // Loop starts at 0, and finishes when the given number of days has been added to the calendar date = date + 1; // Increases the date by one day each run of the for loop if(date > getDaysInCurrentMonth()){ // Checks whether the date is greater than the number of days in the month month = month + 1; // If the date is greater, the month is increased date = count; } // The new date becomes what is left of the count. This is to add the days left when the month increases. (e.g. if 10 days is added to the 25th of may, when the month changes to June, there are 4 days left. else count = count - 1; // Decreases the count by one } }