#Trying to add accessor methods for additional string values

62 messages · Page 1 of 1 (latest)

sudden dagger
#

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?

slender runeBOT
#

This post has been reserved for your question.

Hey @sudden dagger! Please use /close or the Close Post button 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.

dull dagger
#

You're supposed to return a String, not an int

sudden dagger
#

forgot about that ^^ changed that

#

im not sure what to do next though, like I'll get the string representations

dull dagger
#

Your questions are hard to follow... Do you struggle with English?

sudden dagger
#

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?

dull dagger
#

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

sudden dagger
#

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

dull dagger
#

"return, and then"?

#

could you, like, try to make a huge effort into checking that you're communicating your ideas clearly?

sudden dagger
#

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

dull dagger
#

erm

sudden dagger
#

if-else then

dull dagger
#

Can you, can you re-read that last sentence and check that's what you meant?

sudden dagger
#

do you know what im trying to say

dull dagger
#

I think so, my problem is being sure of it

sudden dagger
#

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

dull dagger
#

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
sudden dagger
#

yes

sudden dagger
#

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

dull dagger
#

So, you'd add a method that looks like that:

#
@Override
public String toString() {
  return "day " + day + ", month " + month + ", year " + year;
}
sudden dagger
#

@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"

dull dagger
#

It's just something you should do when you override an existing method

sudden dagger
#

gotcha

dull dagger
#

Probably the constructor doesn't initialize these variables

sudden dagger
#

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?

dull dagger
#

I mean, as they're not something you input, you're going to have to compute them instead

sudden dagger
#

alright

#

that may not be it, i may have done something wrong

dull dagger
#

I don't see any computing

sudden dagger
#

sorry?

dull dagger
#

I hinted that you need to compute the values of these variables. You don't seem to have done that

sudden dagger
#

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?

dull dagger
#

toString() and anything, you can have only one version per class. So you'll need to give it another name

sudden dagger
#

i did it thank you

slender runeBOT
# sudden dagger 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.

dull dagger
#

Well. At least you didn't need much

sudden dagger
#

ill put this here as what i need to do next incase i have questions