Hi there, I'm trying to create accessor methods like shown in the instruction text, (creating day/month/year variables) but I'm stuck when it comes to creating a string representation. I thought of doing an if then statement but that didn't seem to work, either that or i placed it in the wrong area. Could somebody help?
#Trying to add accessor methods for additional string values
62 messages · Page 1 of 1 (latest)
⌛ This post has been reserved for your question.
Hey @sudden dagger! Please use
/closeor theClose Postbutton above when your problem is solved. Please remember to follow the help guidelines. This post will be automatically closed after 300 minutes of inactivity.
TIP: Narrow down your issue to simple and precise questions to maximize the chance that others will reply in here.
You're supposed to return a String, not an int
forgot about that ^^ changed that
im not sure what to do next though, like I'll get the string representations
Your questions are hard to follow... Do you struggle with English?
sorry
I'm having a hard time figuring out how I can do what is lined in the instruction picture, I added the accessor methods but the variables for the string representations still need to be declared right?
Well... It's up to you. If you ask me, the String versions shouldn't be called accessors, instead what they return should be constructed on each call
But you can define the variables and their values in the constructor instead
could i do like
for the day
returning the day + suffix
and then make an if-then method to determine which day number will be assigned which suffix
and then do the same thing for assigning a string name to a month integer
"return, and then"?
could you, like, try to make a huge effort into checking that you're communicating your ideas clearly?
no
thats not what i mean
im saying
return day + suffix
for example
and somewhere earlier in the code ill make an if-then method to determine which day's integer value will be assigned to a certain suffix
erm
if-else then
Can you, can you re-read that last sentence and check that's what you meant?
do you know what im trying to say
I think so, my problem is being sure of it
im not sure if thats something i could do and i was wondering if that was something that'd be a possibility for me to do in order to have "suffix" defined in the return statement later on
I mean, something like that is indeed possible and normally simple
Like
if(day < 10) {
suffix = "beginning";
} else if(day < 20) {
suffix = "middle";
} // and so on
yes
here's what i have so far, question; do I have to change what I did? I need to add a toString method now and I don't know if I did anything wrong where a toString method may need to be, or would I create a completely new method?
I haven't used toString before so tbh I don't really know much about it
So, you'd add a method that looks like that:
@Override
public String toString() {
return "day " + day + ", month " + month + ", year " + year;
}
@Override, what would that be?
anyways, i must've done something wrong? maybe? I keep getting null here whenever I want to grab "monthWord" and "suffix"
It's just something you should do when you override an existing method
gotcha
Probably the constructor doesn't initialize these variables
that would be here, right?
could I just add like
this.monthWord = monthWord and the same thing with suffix, even though they aren't variables you input?
I mean, as they're not something you input, you're going to have to compute them instead
I don't see any computing
sorry?
I hinted that you need to compute the values of these variables. You don't seem to have done that
my bad
computing the values
i think i got it to work
and then just doing this returned what i wanted
this passage asking me to add a second method, I tried using toString again because it wants day/month/year to be string format it seems, but that doesn't work? can I only use toString once or something?
toString() and anything, you can have only one version per class. So you'll need to give it another name
i did it thank you
If you are finished with your post, please close it.
If you are not, please ignore this message.
Note that you will not be able to send further messages here after this post have been closed but you will be able to create new posts.
Well. At least you didn't need much