[Boards: 3 / a / aco / adv / an / asp / b / bant / biz / c / can / cgl / ck / cm / co / cock / d / diy / e / fa / fap / fit / fitlit / g / gd / gif / h / hc / his / hm / hr / i / ic / int / jp / k / lgbt / lit / m / mlp / mlpol / mo / mtv / mu / n / news / o / out / outsoc / p / po / pol / qa / qst / r / r9k / s / s4s / sci / soc / sp / spa / t / tg / toy / trash / trv / tv / u / v / vg / vint / vip / vp / vr / w / wg / wsg / wsr / x / y ] [Search | Free Show | Home]

Java Homework Help Question

This is a blue board which means that it's for everybody (Safe For Work content only). If you see any adult content, please report it.

Thread replies: 16
Thread images: 1

File: BLAP10001.jpg (677KB, 2550x3300px) Image search: [Google]
BLAP10001.jpg
677KB, 2550x3300px
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?
>>
>>213312
I asked a question about something unrelated to programming one time on the /pcbg/, and omce on the Java thread.
>>213310
My problem is I don't know the toStrong method. Im not sure how it writes out. Is it a base Java method?
>>
>>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.
>>
>>213331
>>213332
Ok, I'll have to try this. I'm away from my computer right now, but I'll try to get back when I can, thanks
>>
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
Thread posts: 16
Thread images: 1


[Boards: 3 / a / aco / adv / an / asp / b / bant / biz / c / can / cgl / ck / cm / co / cock / d / diy / e / fa / fap / fit / fitlit / g / gd / gif / h / hc / his / hm / hr / i / ic / int / jp / k / lgbt / lit / m / mlp / mlpol / mo / mtv / mu / n / news / o / out / outsoc / p / po / pol / qa / qst / r / r9k / s / s4s / sci / soc / sp / spa / t / tg / toy / trash / trv / tv / u / v / vg / vint / vip / vp / vr / w / wg / wsg / wsr / x / y] [Search | Top | Home]

I'm aware that Imgur.com will stop allowing adult images since 15th of May. I'm taking actions to backup as much data as possible.
Read more on this topic here - https://archived.moe/talk/thread/1694/


If you need a post removed click on it's [Report] button and follow the instruction.
DMCA Content Takedown via dmca.com
All images are hosted on imgur.com.
If you like this website please support us by donating with Bitcoins at 16mKtbZiwW52BLkibtCr8jUg2KVUMTxVQ5
All trademarks and copyrights on this page are owned by their respective parties.
Images uploaded are the responsibility of the Poster. Comments are owned by the Poster.
This is a 4chan archive - all of the content originated from that site.
This means that RandomArchive shows their content, archived.
If you need information for a Poster - contact them.