• 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Bear Bibeault
  • Henry Wong
  • Devaka Cooray
Saloon Keepers:
  • salvin francis
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Frits Walraven
Bartenders:
  • Jj Roberts
  • Carey Brown
  • Scott Selikoff

Keeping Track of Method Calls

 
Ranch Hand
Posts: 65
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
#Edited

Hello Friends,

I noticed that my first post was very vague and bad explained so after working on the task for a little bit I came across an issue I need to some help with.
At the moment I am trying to create a custom Iterator for my Task and I need to modify my next method according to some requirements .

To my question, with my current approach I need to find a way to have an overview how often my Method was already called.
The next() method gets called multiple times and when it reaches a certain number of calls/ already gave out a certain value it should change to another form of output.

Quick example:
Iterator should first give out Numbers from 0-9 in the form of : 0000,1111,2222.... until we reach 9999 so about 9 times.

Afterwards I want to change the outcome to increasing rows in the form of: 1,2,3,4 / 2,3,4,5 etc.


My first approach was to initialize a variable at the beginning which keeps track of the calls like



But from my understanding, calls will always be reset to 0 when I call the method again for the next value?
So how can I keep track of the called/ returned values to let my program know what to do next?

Help will be highly appreciated!

 
Michael Grünau
Ranch Hand
Posts: 65
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
EDITED ABOVE
 
Marshal
Posts: 72098
312
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Please explain why you would need to keep such a count of calls.
 
Campbell Ritchie
Marshal
Posts: 72098
312
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think you would have to show us how you are calling next(), please.
 
Michael Grünau
Ranch Hand
Posts: 65
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The overall goal is to create a custom Iterator which Iterates over different combinations of values from 0-9 to find a password.
Length of the password is given at the beginning.

The iterator should be implemented with the following conditions:
All examples are based on password length 4.

1. Start with repeating numbers (1111,2222,3333,...)

2. Continue with increasing rows (1234,2345,3456...)

3. Continue with decreasing rows (9876,8765,7654...)

4. End with all missing Numbers with the correct length.

My idea was that when I keep tracking of how often the next() method was called I can go to the next task.
For example. I know that for the first condition we call the next() method at most 9 times until we need to change to the second condition.

So the first 9 times my next() will return in a loop the repeating values (first call = 1111, second call = 2222, third call = 3333 etc.)

 
Campbell Ritchie
Marshal
Posts: 72098
312
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The easiest way to do it is to put count = 0; in the constructor and count++; in next(). That won't take you back to 0 however. Consider getCount(), resetCount(), and count() (=increment) methods.
 
Michael Grünau
Ranch Hand
Posts: 65
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yeah I had similar approach but it gets messy after a while as the passwordLength can change and so do the amount of calls I have.

Maybe I implement a variable which stands for each step and changes if the step is done and don't use overall calls.

Quick question about the task in general, I think this approach is very basic and I don't loose the feeling that I could change the complete approach to make it way more efficient in the first place.
Any good idea or maybe a suggestion that I can look into?
 
Campbell Ritchie
Marshal
Posts: 72098
312
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Consider this sort of loop:-No need for a count.
 
Campbell Ritchie
Marshal
Posts: 72098
312
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Michael Grünau wrote:. . . passwordLength can change . . .

That does make it more awkward.I think I had better say no more otherwise I am liable to give you too complete a solution.
 
Michael Grünau
Ranch Hand
Posts: 65
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Appreciate the help!!
Your code above is basically a neat way to create the increasing rows, correct?


For the moment I will try to come up with a solution by myself and maybe get back if I am stuck!
Thanks again!
 
Rancher
Posts: 259
13
Eclipse IDE C++ Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
a brute force solver that tries every possibility becomes impractical when there are many possible permutations, such as if a password contains a-z, A-Z, 0-9, and symbols.
if you use multi-threading and can test multiple keys at a time, it will reduce the time it takes to find a solution, but for complex keys you'll need a better algorithm and even then there's no guarantee you'll find the solution in a reasonable amount of time, or ever.
 
Rancher
Posts: 144
9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
So, as by the mentioned rules each number from 1000 to 9999 will get tested - why not use a simple loop:

The overall count wont changes just by reordering the increments.
 
Michael Grünau
Ranch Hand
Posts: 65
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The Main Part of the Task is about working with Iterators so this should be implemented in a next() method.

Of course are there better options to check for the password but for this one the main point is that the next() call iterates over the values as described before.
 
Kristina Hansen
Rancher
Posts: 144
9
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Well, if your focus is on the iterator then your approach doesn't look right - at least not to me. The Iterator interface and it's methods are meant to be used within the collection framework it is a part of. An Iterator is used to iterate over a given list or set of already provided elements, not as to have any logic on it's own. Also an Iterator has to fulfill the contract to keep the same order as its underlying collection. When you modify the Iterator or it's next() to become self-aware and work on it's own without respect to its collection you break this contract basically renders your Iterator invalid/useless.
I guess you try to use the wrong tool for the wrong job as you only described you want to keep track of numbers of times a metho got called. This is easy done by adding a simple member to the class and increment it each time (wich will require synchronization) - but from the history you already struggled with that easy task.
I know that some here again will take this as to offensive - but from the history of this topic it seems you're not have much experience of it. I guess the better approach would be to step back a step or two and rethink about the goal you want to achieve: If you want to learn about collections and Iterator you chose the wrong example as this not how Iterator in java works. If you focus on bruteforce an n-digit phrase an Iterator only makes sense if you have a collection like a Set or a List already populated you want to iterate over. Otherwise your goal and your approach doesn't align and shouldn't be forced as whatever you might think to learn will end up in a wrong conclusion.

TLDR: To help us to help you try to explain the bigger goal overall than just this one problem ...
 
Michael Grünau
Ranch Hand
Posts: 65
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Well let me explain the task to you.

It is about implementing an Iterator which returns digit sequences as a String.
We need to implement an Instanz of Iterator which goes through all the possible digit sequences of the given password length with the following conditions.

1. Digit Sequence of same number 0000
2. Digit Sequence rising 1234
3. Digit Sequence falling 4321
4. All the rest

We don't have a list where we can iterate over...
 
Marshal
Posts: 26388
81
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Michael Grünau wrote:We need to implement an Instanz of Iterator which goes through all the possible digit sequences of the given password length with the following conditions.

1. Digit Sequence of same number 0000
2. Digit Sequence rising 1234
3. Digit Sequence falling 4321
4. All the rest

We don't have a list where we can iterate over...



Well, the list ends up containing all possible sequences of four digits. So my suggestion would be this:

1. Create a list which contains all of the possible sequences.
2. Sort that list using a Comparator which sorts the sequences according to the four rules you posted.
3. Use an Iterator over that list.
 
I've never won anything before. Not even a tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic