• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Method creation trouble

 
Ranch Hand
Posts: 48
Mac OS X Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
lowercase baba
Posts: 13089
67
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 48
Mac OS X Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 48
Mac OS X Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 48
Mac OS X Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think you will have to query that. You would have to change it to myLock.set('C', 'A', 'K'); or similar.
 
Ranch Hand
Posts: 211
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 48
Mac OS X Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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!
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic