Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

My code is not doing anything  RSS feed

 
John Sing
Ranch Hand
Posts: 58
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi I have been trying to finish this programming project for quite a while now and I thought I finally got it to work since it compiled. But when I try to run it nothing happens. I think this is because I have nothing in the main method in the driver class but if I declare password in the main method then I won't be able to access it from the other class. So what should I do to fix this? Here is my driver class



and here is the code connected to the driver

 
Carey Brown
Bartender
Posts: 3011
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
  • Nothing in main().
  • What is 'length'?
  • What is 'lengthUse'?
  • setLength() not called.
  • setLengthUse() not called.
  • 'length' and 'lengthUsed' only being set in constructor.
  • Class names should begin with upper-case.
  • Parameter 'method' in methodCaller() not used.
  • password, length, and lengthUse are declared static. They should be non-static fields.
  • 'callvariables' not used.

  •  
    John Sing
    Ranch Hand
    Posts: 58
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Carey Brown wrote:
  • Nothing in main().
  • What is 'length'?
  • What is 'lengthUse'?
  • setLength() not called.
  • setLengthUse() not called.
  • 'length' and 'lengthUsed' only being set in constructor.
  • Class names should begin with upper-case.
  • Parameter 'method' in methodCaller() not used.
  • password, length, and lengthUse are declared static. They should be non-static fields.
  • 'callvariables' not used.



  • Okay before I start going through all those problems I have one question. You say I should not declare my variables to be static. But how would I be able to access them in more then one method? Also I tried taking away all the statics in the code but then I get a few errors in my main code. Here is my new code



    and I am getting the following errors

    " [line: 7]
    Error: non-static method getPassword() cannot be referenced from a static context

    [line: 8]
    Error: non-static method getLength() cannot be referenced from a static context

    [line: 9]
    Error: non-static method getLengthUse() cannot be referenced from a static context"

    and here is the new driver



    So yeah I have been through this a few times where I keep adding a removing statics and I always end up wrong somehow. Also what should I put inside the main method to get the code to start?
     
    Carey Brown
    Bartender
    Posts: 3011
    46
    Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    For each class you need a clear understanding of what that class is responsible for.
    Your Driver class seems to be the test code that prompts for a password, calls Finished method()s, and displays encrypted password.
    Keeping track of length and lengthUse should not be the responsibility of Driver.

    Your Finished class seems responsible for encrypting a password.
    It should provide a public method that takes a password to be encrypted and returns the encrypted password.
    All other helper methods should be private.
    Other variables like length and lengthUse should be local to a method or, at least, be private fields.

    Names are misleading.
    'setPassword' does not follow the convention of a getter. This is more like inputPassword().
    In setPassword() you pass in a parameter named 'pass'. This is immediately overwritten by the first statement in the method. You shouldn't have any parameters see as how passing in 'pass' is pointless.
     
    Henry Wong
    author
    Sheriff
    Posts: 23283
    125
    C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    John Sing wrote:
    So yeah I have been through this a few times where I keep adding a removing statics and I always end up wrong somehow.


    Yeah, doing trial and error, by adding and removing static keywords, until the compiler stops complaining is *not* going to work here. You need to understand the issue, in order to be able to fix it.

    Do you understand the difference between a static and non-static context? The pros/cons of using each? And why is it an issue to try to access a non-static field/method from a static context?

    Henry
     
    Carey Brown
    Bartender
    Posts: 3011
    46
    Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Your main() should start out by creating an instance of your PasswordCodeDriver1 class. Then you call three methods (to be created) of the class: inputPassword(), encryptPassword(), and displayPassword(). Those are the fundamental responsibilities of your Driver class. Input and display methods should be obvious, you already have the code elsewhere. Encrypt should create an instance of your PasswordCodeFinished2 class and call its encrypt() method (again, you'll have to create this).
     
    John Sing
    Ranch Hand
    Posts: 58
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Carey Brown wrote:For each class you need a clear understanding of what that class is responsible for.
    Your Driver class seems to be the test code that prompts for a password, calls Finished method()s, and displays encrypted password.
    Keeping track of length and lengthUse should not be the responsibility of Driver.

    Your Finished class seems responsible for encrypting a password.
    It should provide a public method that takes a password to be encrypted and returns the encrypted password.
    All other helper methods should be private.
    Other variables like length and lengthUse should be local to a method or, at least, be private fields.

    Names are misleading.
    'setPassword' does not follow the convention of a getter. This is more like inputPassword().
    In setPassword() you pass in a parameter named 'pass'. This is immediately overwritten by the first statement in the method. You shouldn't have any parameters see as how passing in 'pass' is pointless.


    Okay so I think I fixed most of the errors in my code. But I still have one problem. I made all the helper methods in the main code private like you said but now I cannot call them in my driver class. How would I go about fixing this?
     
    Carey Brown
    Bartender
    Posts: 3011
    46
    Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    You shouldn't have ANY helpers in your Driver class, only in your Finish class. Remember that length and lengthUser should be the responsibility of Finish and therefore should be stripped from Driver.
     
    Carey Brown
    Bartender
    Posts: 3011
    46
    Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Aside from the constructor, what three methods should your driver contain?
     
    John Sing
    Ranch Hand
    Posts: 58
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Carey Brown wrote:You shouldn't have ANY helpers in your Driver class, only in your Finish class. Remember that length and lengthUser should be the responsibility of Finish and therefore should be stripped from Driver.


    I know I meant I made the methods in the main code private. I think I misunderstood you so if I am correct I need to make them public. Also I changed length and lengthUse so they are initiated in the main code now and not the driver.
     
    Carey Brown
    Bartender
    Posts: 3011
    46
    Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    John Sing wrote:
    Carey Brown wrote:You shouldn't have ANY helpers in your Driver class, only in your Finish class. Remember that length and lengthUser should be the responsibility of Finish and therefore should be stripped from Driver.


    I know I meant I made the methods in the main code private. I think I misunderstood you so if I am correct I need to make them public. Also I changed length and lengthUse so they are initiated in the main code now and not the driver.

    I'm not clear on "main code". You have Driver which has a main() method. When you say "main code" do you mean code that is inside the main() method, code that is inside the Driver class, or code that is inside the Finished class?
     
    John Sing
    Ranch Hand
    Posts: 58
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Carey Brown wrote:
    John Sing wrote:
    Carey Brown wrote:You shouldn't have ANY helpers in your Driver class, only in your Finish class. Remember that length and lengthUser should be the responsibility of Finish and therefore should be stripped from Driver.


    I know I meant I made the methods in the main code private. I think I misunderstood you so if I am correct I need to make them public. Also I changed length and lengthUse so they are initiated in the main code now and not the driver.

    I'm not clear on "main code". You have Driver which has a main() method. When you say "main code" do you mean code that is inside the main() method, code that is inside the Driver class, or code that is inside the Finished class?


    I meant all the code that is not inside the driver class AKA the code that has all the methods in it. So yes anything under the Finished class is what I mean by the main code. Sorry for not being clearer on that.
     
    Winston Gutkowski
    Bartender
    Posts: 10573
    65
    Eclipse IDE Hibernate Ubuntu
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    John Sing wrote:I know I meant I made the methods in the main code private. I think I misunderstood you so if I am correct I need to make them public. Also I changed length and lengthUse so they are initiated in the main code now and not the driver....

    You're way too bogged down in the mechanics of this. So my advice is to StopCoding.

    Forget about Java, and write down in English (or your native language) WHAT your program needs to do. Every single step.
    And, as the link suggests, list WHAT it needs to do, not HOW it's going to do it.

    Right now, you're all all tied up in knots with Java code semantics. You need to free your mind of that and get back to basics.

    One tip that's often suggested here is to imagine that you have to explain what your program does to a smart 10-year old with NO knowledge of Java.

    And don't write one more line of Java code until you can do that.

    HIH

    Winston
     
    • Post Reply Bookmark Topic Watch Topic
    • New Topic
    Boost this thread!