Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 196
  • Last Modified:

code be changed to start off with smaller numbers

Hello everyone out there I need some help again. CEHJ created this code for me from this question:
http://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();
    }
  }  
}
0
Drop_of_Rain
Asked:
Drop_of_Rain
  • 13
  • 13
  • 2
1 Solution
 
WebstormCommented:
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)
{



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

0
 
CEHJCommented:
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();
            }
      }
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
CEHJCommented:
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();
            }
      }
}
0
 
CEHJCommented:
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 ;-)
0
 
CEHJCommented:
The comment

>>// Add numbers if more needed

should be changed to

// Add numbers up to the batch size

to avoid confusion


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



0
 
CEHJCommented:
I think this should be more like it:

      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();
            }
      }
0
 
Drop_of_RainAuthor 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();
          }
     }
}


0
 
CEHJCommented:
>>printSets();

Should be

printSets(8,4);
0
 
Drop_of_RainAuthor 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.
http://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]



0
 
Drop_of_RainAuthor Commented:
It could stop as well after the first gruop end with [8, 4, 3, 1, 7, 6, 2, 5]
0
 
CEHJCommented:
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
0
 
Drop_of_RainAuthor Commented:
I would agree with you. The problem I have is to not ask for to much.
0
 
CEHJCommented:
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
0
 
Drop_of_RainAuthor 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.
0
 
CEHJCommented:
>>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?
0
 
Drop_of_RainAuthor 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.
0
 
Drop_of_RainAuthor 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);
     }
     
}
0
 
CEHJCommented:
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
0
 
Drop_of_RainAuthor 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.



 
0
 
CEHJCommented:
>>this way there is no repeating patterns

Well there's not a single repeating pattern in the list i just posted of course ...
0
 
Drop_of_RainAuthor 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
0
 
Drop_of_RainAuthor 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
0
 
CEHJCommented:
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
0
 
Drop_of_RainAuthor 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
0
 
Drop_of_RainAuthor 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.

0
 
CEHJCommented:
:-)
0

Featured Post

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

  • 13
  • 13
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now