We help IT Professionals succeed at work.

Check out our new AWS podcast with Certified Expert, Phil Phillips! Listen to "How to Execute a Seamless AWS Migration" on EE or on your favorite podcast platform. Listen Now

x

code be changed to start off with smaller numbers

Drop_of_Rain
Drop_of_Rain asked
on
Medium Priority
213 Views
Last Modified: 2010-03-31
Hello everyone out there I need some help again. CEHJ created this code for me from this question:
https://www.experts-exchange.com/Programming/Programming_Languages/Java/Q_20933110.html


How could this code be changed to start off with smaller numbers then when the numbers are increased larger numbers can be introduced as well? Can comments be added as well please?

import java.awt.*;
import java.awt.event.*;
import java.awt.image.*;
import java.util.*;
import java.lang.*;



public class TestIt {
    public static void main(String[] args) {
        printRows();
   }

// paste the other cod in here

static void printRows() {
    final int INDEX_LIMIT = 8;
          final int BATCH_LENGTH_LIMIT = 8;
    final int BATCH_SIZE = 4;
    final int NUM_BATCH_GROUPS = 4;
    int batchLength = 4;
          // Disallow duplicates by using a Set
    Set numbers = new HashSet();
    for (int batchGroup = 0; batchGroup < NUM_BATCH_GROUPS; batchGroup++) {
               // Add numbers if more needed
      while (numbers.size() < batchLength && batchLength < BATCH_LENGTH_LIMIT) {
        numbers.add(new Integer((int)(Math.random()  * INDEX_LIMIT) + 1));
      }
      for (int batchSize = 0; batchSize < BATCH_SIZE; batchSize++) {
        java.util.List list = new ArrayList(numbers);
                    // Shuffle and print
        Collections.shuffle(list);
        System.out.println(list);
      }
      batchLength++;
      System.out.println();
    }
  }  
}
Comment
Watch Question

Top Expert 2004

Commented:
Hi Drop_of_Rain,

replace the following code:

static void printRows() {
    final int INDEX_LIMIT = 8;
          final int BATCH_LENGTH_LIMIT = 8;
    final int BATCH_SIZE = 4;
    final int NUM_BATCH_GROUPS = 4;
    int batchLength = 4;

by:

static void printRows(int INDEX_LIMIT,int BATCH_LENGTH_LIMIT,int BATCH_SIZE,int NUM_BATCH_GROUPS,batchLength = 4)
{



Top Expert 2004

Commented:
static void printRows(int INDEX_LIMIT,int BATCH_LENGTH_LIMIT,int BATCH_SIZE,int NUM_BATCH_GROUPS,int batchLength)
{

CERTIFIED EXPERT
Top Expert 2016

Commented:
Voila!

      static void printRows() {
            final int INDEX_LIMIT = 8;
            final int BATCH_LENGTH_LIMIT = 8;
            final int BATCH_SIZE = 4;
            final int NUM_BATCH_GROUPS = 4;
            int batchLength = 4;
            // Disallow duplicates by using a Set
            Set numbers = new HashSet();
            for (int batchGroup = 0; batchGroup < NUM_BATCH_GROUPS; batchGroup++) {
                  // Add numbers if more needed
                  for (int batchSize = 0; batchSize < BATCH_SIZE; batchSize++) {
                        numbers.add(new Integer(batchSize + 1));
                        java.util.List list = new ArrayList(numbers);
                        // Shuffle and print
                        Collections.shuffle(list);
                        System.out.println(list);
                  }
                  batchLength++;
                  System.out.println();
            }
      }
CERTIFIED EXPERT
Top Expert 2016

Commented:
Here it is back in its context:

import java.util.*;

public class TestIt {

      public static void main(String[] args) {
            printRows();
      }


      static void printRows() {
            final int INDEX_LIMIT = 8;
            final int BATCH_LENGTH_LIMIT = 8;
            final int BATCH_SIZE = 4;
            final int NUM_BATCH_GROUPS = 4;
            int batchLength = 4;
            // Disallow duplicates by using a Set
            Set numbers = new HashSet();
            for (int batchGroup = 0; batchGroup < NUM_BATCH_GROUPS; batchGroup++) {
                  // Add numbers if more needed
                  for (int batchSize = 0; batchSize < BATCH_SIZE; batchSize++) {
                        numbers.add(new Integer(batchSize + 1));
                        java.util.List list = new ArrayList(numbers);
                        // Shuffle and print
                        Collections.shuffle(list);
                        System.out.println(list);
                  }
                  batchLength++;
                  System.out.println();
            }
      }
}
CERTIFIED EXPERT
Top Expert 2016

Commented:
Parameterizing those values to the function as Webstorm suggested is generally a good thing (i only wrote it as a demo), although merely doing so alone won't have the effect you want of course ;-)
CERTIFIED EXPERT
Top Expert 2016

Commented:
The comment

>>// Add numbers if more needed

should be changed to

// Add numbers up to the batch size

to avoid confusion


Author

Commented:
I lost some thig that the last code had. It no longer goes up to 8. How can that be changed.



CERTIFIED EXPERT
Top Expert 2016
Commented:
Unlock this solution and get a sample of our free trial.
(No credit card required)
UNLOCK SOLUTION

Author

Commented:
Having trouble compiling this it says "prinySets (int,int) in TestIt cannot be applied to ()


import java.util.*;

public class TestIt {

     public static void main(String[] args) {
          printSets();
     }

     static void printSets(int numBatches, int batchSize) {
          Set numbers = new HashSet();
          for (int batchNum = 0; batchNum < numBatches; batchNum++) {
               numbers.add(new Integer(batchNum + 1));
               for (int batch = 0; batch < batchSize; batch++) {
                    java.util.List list = new ArrayList(numbers);
                    // Shuffle and print
                    Collections.shuffle(list);
                    System.out.println(list);
               }
               System.out.println();
          }
     }
}


CERTIFIED EXPERT
Top Expert 2016

Commented:
>>printSets();

Should be

printSets(8,4);

Author

Commented:
I am starting to see that this part of the program is going to be diffcult. Why did it start off with a group of 8 then go to one. I changed the arg from (8,4) to (8,1) to get this. There is to much repeating of 1 and 2 at the beginning is there something that can be done about that.  I have ask another question associated with this one.
https://www.experts-exchange.com/Programming/Programming_Languages/Java/Q_20933943.html

This was the out come from the code:


[2, 8, 1, 3, 4, 7, 6, 5]

[1]

[1]

[1, 2]

[2, 1]

[1, 3, 2]

[3, 1, 2]

[3, 2, 4, 1]

[1, 2, 3, 4]

[5, 4, 1, 2, 3]

[2, 1, 4, 5, 3]

[2, 3, 1, 6, 4, 5]

[6, 5, 3, 4, 2, 1]

[1, 7, 4, 5, 2, 6, 3]

[7, 6, 4, 3, 1, 5, 2]

[1, 7, 4, 2, 6, 8, 3, 5]

[8, 4, 3, 1, 7, 6, 2, 5]

[1]

[1, 2]

[3, 1, 2]

[4, 1, 3, 2]

[2, 5, 1, 4, 3]

[2, 6, 3, 4, 5, 1]

[5, 2, 1, 3, 7, 4, 6]

[3, 5, 4, 1, 6, 8, 7, 2]



Author

Commented:
It could stop as well after the first gruop end with [8, 4, 3, 1, 7, 6, 2, 5]
CERTIFIED EXPERT
Top Expert 2016

Commented:
The trouble here is that was occurring is that i'm guessing what you want and you're saying it's not quite right. Obviously this is not an ideal way of producing software ;-)

What would be easier is if you specified exactly what you want

Author

Commented:
I would agree with you. The problem I have is to not ask for to much.
CERTIFIED EXPERT
Top Expert 2016

Commented:
It's just a question of producing the right pattern. Maybe if we approached it in steps:

1. How many sets of numbers should there be? We define set here as [x, y, z] etc.
2. How many numbers in each set?
3. Do you want the sets grouped, i.e.


[x, y]
[y, x]

[x, y, z]
[z, y, x]


[x, y, z, a]
[a, y, z, x]


4. Is that pattern good for you (swapping outer numbers)?


etc

Author

Commented:
Let me explain what I want to create. A workout routine. The reason for numbers repeating is for warm ups and beginners. The problem with switching the end numbers is it follows the same number, that would be ok sometimes.
Maybe the starting numbers could be swaped after the groupin get to three.

I won't know what is right until I see what the code creates. I hope you understand.
CERTIFIED EXPERT
Top Expert 2016

Commented:
>>Let me explain what I want to create. A workout routine.

Just go into that a bit more would you? Do we take it that the numbers are the numbers of particular exercises and you want to create a random order for the purposes of varying the rountines?

Author

Commented:
Both. The routines are for the trainee to learn their material. The numbers are a specific part of the routine. This order needs to recorded in the same order. These numbers need to be linked to audio files. Then these audio files need to be joined together into a single file so it can be burned on a CD for them to use. I want to be able to make up a wide range of workout using again specfic parts of the routine.

Author

Commented:
CEHJ

Heres the code you gave me for linking the audio files and checkboxs.


public class CheckBoxesToAudioFiles {
     Map checkToAudio;

     public CheckBoxesToAudioFiles {
          checkToAudio = new HashMap();
     }
     
     public void mapCheckboxToAudioFile(JCheckbox cb, AudioFile af) {
          checkToAudio.put(cb, af);
     }
     
     public AudioFile getAudioFileForCheckbox(JCheckbox cb) {
          (AudioFile)return checkToAudio.get(cb);
     }
     
}
CERTIFIED EXPERT
Top Expert 2016

Commented:
You could get it to print out all the permutations like this:

1 2 3
1 3 2
2 1 3
2 3 1
3 1 2
3 2 1

1 2 3 4
1 2 4 3
1 3 2 4
1 3 4 2
1 4 2 3
1 4 3 2
2 1 3 4
2 1 4 3
2 3 1 4
2 3 4 1
2 4 1 3
2 4 3 1
3 1 2 4
3 1 4 2
3 2 1 4
3 2 4 1
3 4 1 2
3 4 2 1
4 1 2 3
4 1 3 2
4 2 1 3
4 2 3 1
4 3 1 2
4 3 2 1

Author

Commented:
Would there be a way to take the first set of the group example ( 1 2 3, 2 1 3, 3 1 2) and but into an arraylist. That seems like a good mix. Because I need to be able to use these numbers as audio files. Could it then take the second of the set and store them in the arraylist but this time going backwards up the groups example (4 3 2 1, 3 4 2 1,
2 4 3 1, 1 4 3 2) this way there is no repeating patterns.



 
CERTIFIED EXPERT
Top Expert 2016

Commented:
>>this way there is no repeating patterns

Well there's not a single repeating pattern in the list i just posted of course ...

Author

Commented:
What I ment was to not have so many fo the same number groups. The way I mean repeating is each group starts with the same number.

1 2 3 4
1 2 4 3
1 3 2 4
1 3 4 2
1 4 2 3
1 4 3 2

Author

Commented:
Boy was i having a bad day with that statement. What I ment was to not have so many of the same numbers in groups. The way each group starts with the same number (example below.)
1 2 3 4
1 2 4 3
1 3 2 4
1 3 4 2
1 4 2 3
1 4 3 2
CERTIFIED EXPERT
Top Expert 2016

Commented:
OK - take a careful look at these unique patterns - as it's my last attempt ;-)


1 2 3
2 1 3
3 1 2

1 2 3 4
2 1 3 4
3 1 2 4
4 1 2 3

1 2 3 4 5
2 1 3 4 5
3 1 2 4 5
4 1 2 3 5
5 1 2 3 4

1 2 3 4 5 6
2 1 3 4 5 6
3 1 2 4 5 6
4 1 2 3 5 6
5 1 2 3 4 6
6 1 2 3 4 5

Author

Commented:
That would be ok as well  but it has repeating second numbers. The one below would work the best, because I can do other things with those numbers in other ways. I think the one below would work the best to do that with because they are all goups of 4.  I'm sorry I wasn't clear with my communication, I learned a lot from this about asking question and repeating them before the person goes to the trouble to write more code.

Would there be a way to take the first set of the group example ( 1 2 3, 2 1 3, 3 1 2) and put into an arraylist. That seems like a good mix. Because I need to be able to use these numbers as audio files. Could it then take the second of the set and store them in the arraylist but this time going backwards up the groups example (4 3 2 1, 3 4 2 1,
2 4 3 1, 1 4 3 2) this way there is no repeating patterns.

Thanks for all of your devoted work and contributions. If you could post that code it would be great. I hope you saved it, or remember how you wrote it.


1 2 3 4
1 2 4 3
1 3 2 4
1 3 4 2
1 4 2 3
1 4 3 2
2 1 3 4
2 1 4 3
2 3 1 4
2 3 4 1
2 4 1 3
2 4 3 1
3 1 2 4
3 1 4 2
3 2 1 4
3 2 4 1
3 4 1 2
3 4 2 1
4 1 2 3
4 1 3 2
4 2 1 3
4 2 3 1
4 3 1 2
4 3 2 1

Author

Commented:
Sorry again. But now that I am on doing other things with the numbers. Look at the numbers below generated by this code.

import java.util.*;

public class TestIt {

     public static void main(String[] args) {
          printSets(8,6);
     }

     static void printSets(int numBatches, int batchSize) {
          Set numbers = new HashSet();
          for (int batchNum = 0; batchNum < numBatches; batchNum++) {
               numbers.add(new Integer(batchNum + 1));
               for (int batch = 0; batch < batchSize; batch++) {
                    java.util.List list = new ArrayList(numbers);
                    // Shuffle and print
                    Collections.shuffle(list);
                    System.out.println(list);
               }
               System.out.println();
          }
     }
}



[1, 2]



[2, 3, 1]

[2, 1, 3]

[1, 3, 2]

[1, 2, 3]

[1, 2, 3]

[2, 3, 1]



[4, 1, 3, 2]

[4, 3, 1, 2]

[4, 1, 3, 2]

[2, 3, 4, 1]

[4, 1, 2, 3]

[1, 3, 4, 2]



[2, 4, 3, 1, 5]

[2, 3, 4, 5, 1]

[1, 3, 2, 4, 5]

[2, 5, 4, 3, 1]

[2, 3, 1, 4, 5]

[2, 4, 1, 3, 5]



[1, 4, 2, 6, 3, 5]

[3, 6, 2, 1, 5, 4]

[6, 5, 4, 1, 3, 2]

[6, 4, 2, 3, 1, 5]

[3, 2, 1, 6, 4, 5]

[5, 6, 3, 1, 4, 2]



[3, 1, 7, 5, 6, 4, 2]

[7, 4, 6, 5, 1, 2, 3]

[4, 6, 3, 5, 7, 2, 1]

[6, 3, 1, 2, 4, 7, 5]

[3, 4, 1, 2, 5, 6, 7]

[6, 2, 3, 7, 5, 1, 4]



[6, 8, 7, 2, 4, 5, 1, 3]

[4, 2, 3, 6, 8, 1, 5, 7]

[3, 7, 5, 8, 6, 1, 4, 2]

[1, 2, 8, 4, 3, 6, 7, 5]

[6, 3, 7, 2, 5, 4, 8, 1]

[5, 7, 4, 1, 8, 3, 6, 2]

I would be able to just take the groups with 8 numbers accross and store them into an arraylist or array. Because those numbers are great. The code was working I just didn't know how to look at it right. Part of the learning process. I sorry it was at your expense.

CERTIFIED EXPERT
Top Expert 2016

Commented:
:-)
Unlock the solution to this question.
Thanks for using Experts Exchange.

Please provide your email to receive a sample view!

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.