I've got this homework assignment, pic related, and I've just got a small problem.
At the bottom, it says I need to use a "toString" method, and Im not sure how to write it out. I figured it was just a string return value method, and I'd make a return using an if/then statement, but it hasnt worked.
If someone could help explain what/how to do a toString method, and possibly a way to write it for me, that'd be really cool.
the variables it needs for the statement are:
private int speed;
private boolean isFanOn;
private double fanRadius;
private String color;
I wanted to see if I can make up a method to return the needed variables if the "fan is on" true or not. I just cant seem to get it right.
If you're looking for quick help >>>/g/sqt might offer some assistance.
/wsr/ is a slower board, but you probably will get a decent response
Out of interest, how old are you OP?
>>213308
20. New to programming, and this is a starter class. I woulda asked my professor, but their office hours were all during classes, and it has been a busy weekend. Last time I was on /g/ they told me to fuck off to /wsr/.
What exactly isn't working with toString()?
I haven't written in Java in a while, but I'd just create an empty string (to be returned at the end) and add the appropriate attributes to it. (i.e. fan.speed + " " + fan.color etc.)
>>213309
Did you make a thread there? Don't do that. It's not a tech support board and shouldn't be treated as such.
If you ask on /sqt/ though, which is literally a general dedicated to small/stupid questions, you should be OK.
What's the code you've got for your toString at the moment?
>>213315
From what I'm reading, you're to implement your own toString() method as part of the fan object. You're not using one of Java's default toString() methods, because there isn't one defined for Fan objects (your job).
>>213315
/sqt/ is honestly useful for stuff like this
my Java isn't the best at all but shouldn't you have something like this
public String toString() {
return speed+" "+" "+colour;
}
>>213319
It's a default method? I didn't know that, my prof didn't really cover that, and she just went oner it last class. How does it work?
>>213321
Oh. Ok, I'm just out of it from this weekend, I left this stuff to the last minute.
Is there a way I can put in a if/them statement in there? It needs to return different stuff depending if the boolean variable is false or not. Ive tried some other stuff and it just didn't work
>>213330
I don't see why not, something like
if the fan is on then {
//do something }
else if the fan is off {
//return something else }
else{
//you shouldn't be seeing anything here but it could be useful for error checking}
But like I said, my Java really isn't the best. You're better off waiting for someone else to hop along or something.
>>213326
.toString() changes an item, for example, an integer, into its string representation. It's already defined by default for Integer objects.
toString isn't a method that's available by default for every data type.
This assignment has you making a new data type called Fan. This data type you're writing is being defined ENTIRELY by you, so the only methods that exist for it are the ones you're writing. You have to make your own toString() method as part of your fan object, which returns a string representation of your Fan data type.
>>213330
Yeah you can put if statements in there. toString() is a method, and you can do whatever you'd normally do in a method there.
Ok, OP here.
I think I just made a few dumb mistakes in my code and wasn't looking at it the right way.
I wasnt thinking straight, ive been tired all day. thanks for the explanations and help anons
>>213339
Glad to hear you came to some sort of conclusion