• Post Reply Bookmark Topic Watch Topic
  • New Topic

Calling a method for a driver class  RSS feed

 
haruki jay
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello. I am a beginner programmer, and i'm a little confused on how to call a method in a driver class.
For ex, i attached my code for a method i created thats in charge of making sure the person enters the right integers to unlock a door.

then, in the driver class (main), i'm asked to open the door by entering the correct integers. If i enter the wrong one, to display a message that its wrong and if i enter the right one, to show a message that it's right.

Thank you so much for your help, i just need some guidance  
Screen-Shot-2017-03-31-at-1.52.21-PM.png
[Thumbnail for Screen-Shot-2017-03-31-at-1.52.21-PM.png]
my code for this example
 
Junilu Lacar
Sheriff
Posts: 11477
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please UseCodeTags (←Click that, it's a link) when posting code; don't post code as an image.
 
Junilu Lacar
Sheriff
Posts: 11477
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here's what your code looks like when posted using code tags:

Now it's easier to discuss this because it has line numbers which other people can reference. On line 4, you set a variable status to true. Why aren't you setting it to false in the else part? You'll also need to show how you're trying to use this method in your main method. Do you not know how to call this method? Since the method is not static, you have to call it by dereferencing an object reference, like so:

Line 1 above creates an instance of the SecureDoor (I don't know what your actual class is called so I'm just using "SecureDoor" as an example). The door.openDoor() call on line 2 is how you'd have to call the method you defined.
 
haruki jay
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Junilu Lacar wrote:Please UseCodeTags (←Click that, it's a link) when posting code; don't post code as an image.


will do, thank you for the tip!
 
Campbell Ritchie
Marshal
Posts: 56534
172
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Don't write
if (something) return true; else return false;
Is it much more elegant to write simply
return something;
So let's simplify your method, shall we:-But now we have a problem: I shall leave it to you to work out, but I shall give a hint about what the problem is: status.

While you work out what sort of problem I have landed you with, I shall tweak the formatting a bit because you shou‍ld leave space round binary operators, and your line would become too long:-Does exactly the same, but looks a bit better and is easier to read.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!