|
|
|
|
#1 |
|
Clem - shhh.
|
Quick Computer Science Help (DrJava)
Ah, I'm trying to do our first project (we have three of them throughout the semester) and running into a bit of snag. Don't get me wrong, so far it's been an amazing cake walk (it is Intro I, after all), but I don't think we've talked about this yet.
I'm writing a SimpleDate class with (int year, int month, int day) and so far everything's fine, but one of the methods they want us to implement is to find the numbers of days from the first object to another. The notes in the project sheet look like this: /* numberOfDaysTo returns the number of days */ /* from this SimpleDate to the other SimpleDate. */ /* Note that the keyword 'this' refers to the first date, */ /* and the variable 'other' refers to the second date. */ public int numberOfDaysTo(SimpleDate other) That's great and all, but we haven't covered using 'this' and 'other' to differentiate anything. I can write the math out fine, but I don't how to ask it to subtract the second year (Simple Date other), month, etc from the first. Keep in mind that this is the most beginning of courses, so simple solutions that are within my realm of understanding are what I need (plus I don't need answers, per se, just someone to explain it). Do I need to create another constructor that'll store a reference to SimpleDate other or what? Last edited by Iron Past; 10-01-2009 at 04:43 PM. |
|
|
|
|
|
#2 |
|
Sir Isaac Newton in Space
Join Date: Oct 2008
Location: Ottawa, Canada
Posts: 1,670
|
Have they covered the dot operator yet?
"this" is a special keyword that refers to yourself (i.e. the instance of the object whose method you're running) this.day is the day variable on the object running numberOfDaysTo other.day is the day variable on the "other" SimpleDate object being passed to numberOfDaysTo. Of course, that assumes that day is a public variable. If not, you'll have to write/use a get function on the "other" object to access its day variable. I hope this is what you're asking and I didn't just explain something totally obvious to you.
__________________
Gamertag|PSN|Steam = Codicier Playing:Dark Souls (PC), SWTOR, Red Alert 3, Dota 2 (Only bots because I suck) |
|
|
|
|
|
#3 |
|
Clem - shhh.
|
Ah, that helps. We've covered the dot operator in other parts, but only using it to invoke methods in the class tests, not in the context of 'this' and 'other,' which we haven't covered at all.
The class compiles now, but I'm having trouble calling it. Says it can't be applied to (int,int,int). But that's a really big step, thanks. Edit: Got it! I needed to store a reference to SimpleDate other, not just plug in int. It's an ongoing process, but thank you very much for your help. And I'm sure my terminology is all over the place, so sorry.
Last edited by Iron Past; 10-01-2009 at 05:32 PM. |
|
|
|
|
|
#4 |
|
Sir Isaac Newton in Space
Join Date: Oct 2008
Location: Ottawa, Canada
Posts: 1,670
|
No problem.
__________________
Gamertag|PSN|Steam = Codicier Playing:Dark Souls (PC), SWTOR, Red Alert 3, Dota 2 (Only bots because I suck) |
|
|
|
|
|
#5 |
|
Clem - shhh.
|
Hmm. Is it possible to have the return value of an int output as a variable, or is that a little beyond what I should try right now? For example, if my end statement int is 5 and I want that to equate to an output of Friday, can I do that without too much fuss?
|
|
|
|
|
|
#6 |
|
Sir Isaac Newton in Space
Join Date: Oct 2008
Location: Ottawa, Canada
Posts: 1,670
|
Well, it depends on how you're representing a value of Friday. Is Friday just a string "Friday"?
__________________
Gamertag|PSN|Steam = Codicier Playing:Dark Souls (PC), SWTOR, Red Alert 3, Dota 2 (Only bots because I suck) |
|
|
|
|
|
#7 |
|
Clem - shhh.
|
Yes, but I think I'm stating to get ahead of myself, judging by the title of the next project. They just move so slooooow in that class, but I suppose it's better than moving too fast.
|
|
|
|
![]() |
| Bookmarks |
| Thread Tools | |
| Display Modes | |
|
|