aspose file tools*
The moose likes Beginning Java and the fly likes Method creation trouble Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Method creation trouble" Watch "Method creation trouble" New topic
Author

Method creation trouble

John Vorona
Ranch Hand

Joined: Sep 21, 2013
Posts: 48

This code basically tests the Lock.java class.
What it does is it creates a Lock with the combination C, A, K.



I am unsure of how to create the method. I understand that the method will start like this:


What to put in the body of the method I am unsure of, how can i set a variable equal to the character A, without overriding the previous dial variable which is C.

Here is my Lock.java code, i greyed out the body of the set method since i am sure it's wrong.



Any help appreciated.


There are 10 types of people: those who understand binary, and those who do not understand it.
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11499
    
  16

stop coding.
start thinking.

What is the set method supposed to DO? What exactly is it setting?

it looks like this is supposed to be passing in the individual combination parts. This doesn't seem right to me. On a real lock, you don't enter one number, see if it opens, enter the next number, see if it opens, enter the third, and see if it opens.

You enter a full combination, and then see if it opens.

I would make a method that does something like

tryCombination(char first, char second, char third)

that then validates all three simultaneously. Only if all three are right would it return true. If any of the three are wrong, it would return false.


There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 40052
    
  28
Do you know how to use arrays? You might use a char[] instead of the three individual characters.
If you have an array, you can pass an array of the three characters new char[]{'C', 'A', 'K'}, or a String "CAK", or you would have to have an array for attempts to undo the Lock.
You might have the array in the form {'C', 0, 0} where 0 means the “null character”. In which case you would need that array as a field and an index, telling you how many letters have been passed to attempt to unlock it. First use of the method sets the first letter, second use, etc.
You will not get it to work with a single letter field.

I would suggest you change the signature of the set and unlock methods to take a String or three letters or an array. Then you can get rid of most of the fields you have, and use local variables instead.
John Vorona
Ranch Hand

Joined: Sep 21, 2013
Posts: 48

Alright the tryCombination makes it a lot clearer although I was given the LockTester code and I am unable to modify what is already coded, I can only add. What I am unsure of is that in the LockTester code, the combination is CAK, and each char (C, A and K) are set individually. How would I set up my set method so that, say the first time the method is called, it stores whatever char was provided in the parameter is the first letter of the combination, and the second time the set method is called, it stores the char that is provided in the parameter as the second letter of the combination and so on.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 40052
    
  28
Nazar Buko wrote: . . .

. . .
That code can be shortened and still retain readability. Look at this style guide, and you can see it should be shortened toThe line breaks and () are not absolutely necessary, but may make it easier to read.
Remember: if you have one letter field, you will never only that method to produce true if your password is “XXX” or similar.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 40052
    
  28
Nazar Buko wrote: . . . How would I set up my set method so that, say the first time the method is called, it stores whatever char was provided in the parameter is the first letter of the combination, and the second time the . . .
I have already hinted at that.

The correct answer is, you should't.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 40052
    
  28
fred rosenberger wrote: . . . On a real lock, you don't enter one number, see if it opens, enter the next number, see if it opens, enter the third, and see if it opens. . . .
When I was little, at school, we did exactly that. We could buy bicycle combination locks which were so poorly‑made, we could feel when one number of the combination was correct. At the age of ten, I could undo such a lock in about 30″
John Vorona
Ranch Hand

Joined: Sep 21, 2013
Posts: 48

Well let me give you some background info, basically this is for an introductory course to computer science and the language we are first learning is java, we have just finished covering if, else, else if statements and have barely begun loops. I am still clueless on arrays but I can learn them on my own which isn't an issue. Although I would imagine that the assignment could be complete by the concepts we have learned which in this case do not include arrays. I'll keep you updated on how it goes and whether I eventually figure i out :P.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 40052
    
  28
Probably best to change the signature of the method to take three chars, if you don't yet know how to use arrays.
John Vorona
Ranch Hand

Joined: Sep 21, 2013
Posts: 48

Okay, say I change it to

In the LockTester code, it uses the set method to only set 1 char. This I can not change as it was given to me prewritten. That's what i'm lost in, if I don't figure it out here I will talk to the prof.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 40052
    
  28
I think you will have to query that. You would have to change it to myLock.set('C', 'A', 'K'); or similar.
Tyson Lindner
Ranch Hand

Joined: May 16, 2012
Posts: 179
You need three new variables to represent each one of the tries. The combination of the lock is final and should not be changed, so there will be a total of six variables. When you call the pull() method, you have to check if the try variables match up with the lock's combo variables exactly. The set(...) method only changes the try variables. You also need an int value to be able to tell which try you're on. So set should increment that int value when its called, and then change the corresponding try variable. Also, you don't have to test if the try variable equals the parameter, you can just set it.
John Vorona
Ranch Hand

Joined: Sep 21, 2013
Posts: 48

Campbell Ritchie wrote:I think you will have to query that. You would have to change it to myLock.set('C', 'A', 'K'); or similar.


If that was the case and i could change myLock.Set() to take three arguments, I would be done a long time ago. Thing is people in my class are done this assignment and they didn't change any of the prewritten code. I will have to clarify with the prof. Thanks though!
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Method creation trouble