welcome to the CoG network!
Colony of Gamers
 
 
 
 
 
 
 
 
 
 
 
Old 10-01-2009, 04:41 PM   #1
Iron Past
Clem - shhh.
 
Iron Past's Avatar
 
Join Date: Oct 2008
Location: San Antonio
Posts: 3,676
Blog Entries: 2
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?
__________________
GamerTag: Eyefink, PSN: IronPast
Would you kindly?

Last edited by Iron Past; 10-01-2009 at 04:43 PM.
Iron Past is offline   Reply With Quote
Old 10-01-2009, 05:02 PM   #2
Codicier
Sir Isaac Newton in Space
 
Codicier's Avatar
 
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)
Codicier is offline   Reply With Quote
Old 10-01-2009, 05:15 PM   #3
Iron Past
Clem - shhh.
 
Iron Past's Avatar
 
Join Date: Oct 2008
Location: San Antonio
Posts: 3,676
Blog Entries: 2
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.
__________________
GamerTag: Eyefink, PSN: IronPast
Would you kindly?

Last edited by Iron Past; 10-01-2009 at 05:32 PM.
Iron Past is offline   Reply With Quote
Old 10-01-2009, 05:28 PM   #4
Codicier
Sir Isaac Newton in Space
 
Codicier's Avatar
 
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)
Codicier is offline   Reply With Quote
Old 10-01-2009, 07:23 PM   #5
Iron Past
Clem - shhh.
 
Iron Past's Avatar
 
Join Date: Oct 2008
Location: San Antonio
Posts: 3,676
Blog Entries: 2
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?
__________________
GamerTag: Eyefink, PSN: IronPast
Would you kindly?
Iron Past is offline   Reply With Quote
Old 10-01-2009, 07:58 PM   #6
Codicier
Sir Isaac Newton in Space
 
Codicier's Avatar
 
Join Date: Oct 2008
Location: Ottawa, Canada
Posts: 1,670
Quote:
Originally Posted by Iron Past View Post
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?
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)
Codicier is offline   Reply With Quote
Old 10-02-2009, 04:57 AM   #7
Iron Past
Clem - shhh.
 
Iron Past's Avatar
 
Join Date: Oct 2008
Location: San Antonio
Posts: 3,676
Blog Entries: 2
Quote:
Originally Posted by Codicier View Post
Well, it depends on how you're representing a value of Friday. Is Friday just a string "Friday"?
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.
__________________
GamerTag: Eyefink, PSN: IronPast
Would you kindly?
Iron Past is offline   Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT -6. The time now is 04:00 PM.


Powered by vBulletin® Version 3.8.7
Copyright ©2000 - 2013, vBulletin Solutions, Inc.
// Google Analytics - Must remain as a separate script // External Source Executed