• Post Reply Bookmark Topic Watch Topic
  • New Topic

Breaking a lock code  RSS feed

 
Josh Galeigh
Ranch Hand
Posts: 49
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm having trouble with this problem. I'm suppose to read the following code and devise a program that can break the lock.



So, I believe that I can try various password lengths until I get a return from open that !=-1. But I'm not sure how to implement this. This is what I have so far to get the length:



I get the error:Cannot make a static reference to the non-static method open(char[]) from the type InsecurePasswordLock

I'm just not sure how else to figure this out. How can I keep querying the open method to get my length??

 
Mike. J. Thompson
Bartender
Posts: 689
17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
As the error suggests, you're attempting to call the open(char[]) method as if it was a static method. It is an instance method though, not a static method. You cannot call it using the classname, you need to create an instance of InsecurePasswordLock and use that.

 
Josh Galeigh
Ranch Hand
Posts: 49
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks, I still don't understand static/instance/etc very well. I like running into problems like these because it helps me learnthat much more.
 
Mike. J. Thompson
Bartender
Posts: 689
17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Josh Galeigh wrote:Thanks, I still don't understand static/instance/etc very well. I like running into problems like these because it helps me learnthat much more.


Well learning about the static and non-static contexts is pretty important. You'll struggle to understand object oriented design without it. If there are any specific questions you have let us know, or take a look at the Oracle Java tutorial
 
fred rosenberger
lowercase baba
Bartender
Posts: 12563
49
Chrome Java Linux
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Josh Galeigh wrote:Thanks, I still don't understand static/instance/etc very well.

Think about dogs. Real life, flesh and bone dogs. You're probably familiar with them. I can ask you questions about dogs, regardless of whether you actually HAVE a dog or not: Do they have fur? How many legs do they have? Is it safe to feed them grapes?

But some questions don't make sense unless you own a dog, and we are talking about a specific dog: What is your dog's name? How old is your dog? What breed is your dog?

That is the difference between a static method and non-static. A static method can be called even if NO SUCH OBJECT exists. A non-static method can only be called on an actual, created object.

now, let's think about a Java class that represents Dogs. (Let's not think too hard, this is a somewhat contrived example.)

If i had code for a Dog class, methods like hasFur(), getNumLegs(), isSafeToEat(String foodName) would all be static, because it doesn't matter if we ever created a Dog object (i.e. an instance of a Dog). However, methods like getName(), getAge(), and getBreed() only make sense when I have a Dog Object, and I would do it via the reference variable i have:

 
Josh Galeigh
Ranch Hand
Posts: 49
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That helps, thank you. Objects have certainly been my weakest topic so far. I had a C++ class before but objects were completely ignored for whatever reason.

So I got the code to run through and find the length of the password fine. And I think I'm able change each character in the array until it works. But my code still isn't passing. I'm still not passing the test. I see in the debugger that I'm iterating through as I planned and changing each character. Here is my code:



Any thoughts?

 
Campbell Ritchie
Marshal
Posts: 56546
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Josh Galeigh wrote: . . . Objects have certainly been my weakest topic so far. I had a C++ class before but objects were completely ignored for whatever reason. . . .
That seems to be a common problem; people who have learnt a non‑object language have no end of difficulty learning objects.
 
Mike. J. Thompson
Bartender
Posts: 689
17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Josh Galeigh wrote:That helps, thank you. Objects have certainly been my weakest topic so far. I had a C++ class before but objects were completely ignored for whatever reason.


If they taught you C++ without teaching you about objects then they may as well have just taught you C! Well objects are central to understanding Java so it's definitely worth taking some time to understand them.

The reason you're not printing out that you unlocked the lock is because you have two instances of InseucrePasswordLock created in your code. You create one in breackLock() (line 4), and one in main() (line 37).

When you call breaklock() it unlocks the instance it created on line 4. When you print out the result of isUnlocked() you obtain the value from the instance created on line 37. You didn't unlock the instance on line 37 though so it will never return true. You unlocked the instance on line 4. So this is why you need to get a firm understanding of what a class is, and what an instance of a class (an object) is. Your two lock objects are both instances of InsecurePasswordLock. They both have the same structure, the same methods and the same types of internal variables, but they are both separate from each other. When you do something to one of the instances it has no effect on the other instance because they are separate. So whenever you use the new keyword you are creating a new instance.
 
Mike. J. Thompson
Bartender
Posts: 689
17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:
Josh Galeigh wrote: . . . Objects have certainly been my weakest topic so far. I had a C++ class before but objects were completely ignored for whatever reason. . . .
That seems to be a common problem; people who have learnt a non‑object language have no end of difficulty learning objects.


I remember when I first started work I was put on a C++ project and I only knew Java. It took me quite some time to realise what it meant to have an object on the stack rather than the heap since Java only lets you put objects on the heap. I couldn't understand how you could create something without using the new keyword!
 
Josh Galeigh
Ranch Hand
Posts: 49
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Okay, I know it has been a while but I got busy with work and school.

I totally missed the 1st new object in Main. Things would have been a lot easier for me if I had bothered to read it. So, I'm using that now. I feel like my code should work but I have a few problems still. First, my 'While' loop is not exiting when I set checkCount to false. I've had to insert a 'break' in order to get out (it exits once the 100 count is over, but I want it to exit as soon as the length is found). Second, I added another break in the character search section because I'm concerned I'll have the same issue.

Finally, I'm getting:

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 31
at InsecurePasswordLockBreaker.breakLock(InsecurePasswordLockBreaker.java:32)
at InsecurePasswordLockBreaker.main(InsecurePasswordLockBreaker.java:47)

When I run it now. Line 32 is the "key[wrong]+=1;" line and 47 is "char[] key = breakLock(lock);"

I don't understand this. I'm not changing the length of key when I add 1 to the character in the array. The only thing I can think is maybe the loop isn't catching the correct value and iterating passed all the ascii values. But when I debug it seems to find everything fine.

This is what I have now:

 
Mike. J. Thompson
Bartender
Posts: 689
17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Josh Galeigh wrote:

Finally, I'm getting:

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 31
at InsecurePasswordLockBreaker.breakLock(InsecurePasswordLockBreaker.java:32)
at InsecurePasswordLockBreaker.main(InsecurePasswordLockBreaker.java:47)

When I run it now. Line 32 is the "key[wrong]+=1;" line and 47 is "char[] key = breakLock(lock);"

I don't understand this. I'm not changing the length of key when I add 1 to the character in the array. The only thing I can think is maybe the loop isn't catching the correct value and iterating passed all the ascii values. But when I debug it seems to find everything fine.



To solve your problem you need to answer the following 3 questions:

1) What value does your open(...) method return when you give it the correct key?

2) What is the maximum valid index for any array?

3) What does your code in breakLock(...) do immediately after supplying the valid key?
 
Josh Galeigh
Ranch Hand
Posts: 49
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Okay, I got it to work. Though, I'd like it to look a little better. Instead of checking if it is unlocked and breaking I checked to see if it returned the length of the array (question 1). The length of the array is the maximum index right? Now, once it gets the length as an output it breaks and then returns key (3).

I really don't like using break all the much. For my first while loop how come when I set checkCount=false it doesn't break the loop?
 
Mike. J. Thompson
Bartender
Posts: 689
17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Josh Galeigh wrote:Okay, I got it to work. Though, I'd like it to look a little better. Instead of checking if it is unlocked and breaking I checked to see if it returned the length of the array (question 1). The length of the array is the maximum index right? Now, once it gets the length as an output it breaks and then returns key (3).

I really don't like using break all the much. For my first while loop how come when I set checkCount=false it doesn't break the loop?


Josh Galeigh wrote:The length of the array is the maximum index right?


The maximum length of an array is 'length - 1' because arrays are zero-based.

Josh Galeigh wrote:For my first while loop how come when I set checkCount=false it doesn't break the loop?


The boolean expression in a while loop is evaluated immediately before each iteration of the loop, and the iteration of the loop involves running all code in the while-loop body.

Your while loops contains a for-loop, so a single iteration of the while loop involves running the for-loop to completion. Therefore when you set the boolean value inside the for loop it will not be evaluated until the for-loop completes.

You can remove the while-loop completely and use just the for-loop to run up-to 100 iterations but stop looping immediately when you get the right length. The while-loop is just adding an extra layer of complexity.

 
Josh Galeigh
Ranch Hand
Posts: 49
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The thing that gets me with the array is just that it doesn't even matter, I can take it out. I don't think it really saves me any time for small keys. It always seems to use 31 character arrays for the key so I can even take out the first for loop and just set key length to 31. I'm leaving it in just in case the autograder chooses another code length.

But, this line gives me a out of bounds exception: key[wrong]+=1; if I toy around with my code a bit. For instance:



I thought it might be just going beyond the ascii characters but I just made a little for loop testing this and I got no errors. So, I just don't understand why this causes and exception.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!