• Post Reply Bookmark Topic Watch Topic
  • New Topic

Implementing Methods  RSS feed

 
D Doemer
Ranch Hand
Posts: 36
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have a homework question which makes little sense to me given my lack of foundation in java. It says:

For a class representing a light switch (bodies of methods omitted):




Implement three (3) methods: One (of the two) constructor, the method on, and the method getPosition.


I don't have the slightest clue what to write, so if anyone can be of assistance that would be great.

Thanks,

D
 
Chan Ag
Rancher
Posts: 1090
14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
D Doemer wrote:
Implement three (3) methods: One (of the two) constructor, the method on, and the method getPosition.


I don't have the slightest clue what to write, so if anyone can be of assistance that would be great.



Actually constructors are not methods. It is wrong to refer to constructors as methods. Ok. Now let's create the body of those constructors and that one method.

So, now what is a constructor supposed to do. The answer to that is, 'a constructor initializes the object you're constructing'.
So you're constructing a Switch. So what would it mean to initialize a Switch based on your class definition? That's the starting point for filling in the body of those constructors.
Ok, a hint for that is -- A Switch has a Position. So start with initializing your switch with a position.

About the methods getPosition(), on and off start with writing down in plain english what each of those methods are supposed to do. Then turn that into how you'd do that in Java.


 
Mike. J. Thompson
Bartender
Posts: 689
17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well you've been given the method names, and now need to fill in the bodies of those methods.

You need to decide what those methods mean for a light switch. You have a method on(), a method off(), and a method getPosition().

So what do you think these methods need to do? What should getPosition() return immediately after calling on()? What should it return immediately after calling off()?
 
Rico Felix
Ranch Hand
Posts: 411
5
IntelliJ IDE Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
One addition to the previous tip in the assumption that you don't know how to initialize the position its done as follows:

 
D Doemer
Ranch Hand
Posts: 36
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Guys, thanks for your replies. And I do apologize for being dense, but even in the last week of this class that I'm in, code still looks like this to me:

会意字 / 會意字 مۉلٖىمۉ سه ته‌بٖى بۉژه %^&*@!.



That being said, I have this one and one other assignment, and then I'm out of this torment. I found this code online for something similar:



So if he's looking to have this light switch turn on and off, that looks about right to me, yes?


Not sure where this --> comes in Rico, but I believe you. Just don't know where to put it.

Now, as for the method "On". Doesn't that code I printed above do virtually the same thing??

And then finally, the method "getPosition". The only comparable thing to my assignment that I could find is:



Of course I don't know why it's talking about 3d -- obviously, that parts not fitting.

So yes, I am in fact, that clueless and am feeling more and more like as the days go on.

Thanks again.
 
Mike. J. Thompson
Bartender
Posts: 689
17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The getPosition() and setPosition() methods you posted are working with coordinates, and aren't applicable to your assignment so you should ignore them.

This isn't going to be something you can look up online just by searching for the method names. There may be code out there that has method names the same as your code, but the important thing here is your requirements.

Your depOsition() method needs to return the current state of the light switch (and might have been called gerState() instead).

So the on() and off() methods need to change the state of the light switch, which is where Rico's code comes in.

The gerPosition() method needs to return the current state.
 
D Doemer
Ranch Hand
Posts: 36
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yeah, I don't get it. Sorry. If I were to think about this in my own words, and if it were necessary for a computer to actually flip a light switch (which it isn't since we obviously do that by hand), I would write "computer, flip that light switch on". Not trying to be sarcastic at all, it's just how my brain works. I don't see the need for the code, which is probably what I should have learned in the first week of class. 60% of my classmates don't understand either, but I digress.

This is what I came up with after hours of reading, and I'm sure that's not the whole thing.




I'm still having trouble believing that people just do this off the tops of their heads. If a beginner is asked to write a code, and say, it's for light switches turning on and off, shouldn't they first be given an example of another object doing something, maybe a tv turning on and off, or a toilet flushing (just trying to come up with stuff off the top of my head). In other words, wouldn't it make sense to physically SEE a similar code before being expected to do it? I know I'm overly analytical but I can't call something or return the current state of the light switch when I don't even know what that means.

Thanks for trying to help. I'll skip this one since it's the only question I can't answer and I'm just ready to be done lol.

D
 
Jacob Draper
Ranch Hand
Posts: 35
Java Linux Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I believe your getPosition method, when returning should be, return position (lowercase p instead). The constructor and on method looks good to me.
 
Chan Ag
Rancher
Posts: 1090
14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
D,

Like I said in my first response, where is your description of what those methods should do.

I don't see that. I see a lot of implementation details going back and forth but that's step 2.

We aren't past the step 1 yet.

It is ok to be confused or overwhelmed when you have many things to do at the same time. Hence we break a problem into components that can be dealt with at a time.

So how about this for example.

1) public void on()
write here in simple english what it should do.

2) public void off()
....

Hope it'll help.
Chan.

Edit : I see some method implementations in your previous response. They look ok. So I take it you've understood
what those methods should do, which is good. Ignore what I said.
 
Mike. J. Thompson
Bartender
Posts: 689
17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You just need to add the off() method.
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
D Doemer wrote:Yeah, I don't get it. Sorry. If I were to think about this in my own words, and if it were necessary for a computer to actually flip a light switch (which it isn't since we obviously do that by hand), I would write "computer, flip that light switch on".

<nitpick>
Just a small point: you need to be a bit careful when you use words like "flip", because it may mean different things to different people.

Personally, when I think of "flipping" a switch, I mean: change it to its other position. So, if the light is on it will turn it off, and vice-versa.

Indeed, in bit-based operations, this is achieved by an "XOR 1" (or in Java: 'booleanValue ^ true').
</nitpick>

HIH

Winston
 
Rico Felix
Ranch Hand
Posts: 411
5
IntelliJ IDE Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
D Doemer wrote:If I were to think about this in my own words, and if it were necessary for a computer to actually flip a light switch (which it isn't since we obviously do that by hand)


Actually a computer can flip a light switch on and off electronically... in that sense we use the phrases toggle on and toggle off... If you have ever heard of home automation then this is the type of programming that you will need for such tasks...
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!