Solved

code be changed to start off with smaller numbers

Posted on 2004-03-27
28
185 Views
Last Modified: 2010-03-31
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
Comment
Question by:Drop_of_Rain
  • 13
  • 13
  • 2
28 Comments
 
LVL 13

Expert Comment

by:Webstorm
ID: 10695124
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
 
LVL 13

Expert Comment

by:Webstorm
ID: 10695128
static void printRows(int INDEX_LIMIT,int BATCH_LENGTH_LIMIT,int BATCH_SIZE,int NUM_BATCH_GROUPS,int batchLength)
{

0
 
LVL 86

Expert Comment

by:CEHJ
ID: 10695136
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
 
LVL 86

Expert Comment

by:CEHJ
ID: 10695143
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
 
LVL 86

Expert Comment

by:CEHJ
ID: 10695160
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
 
LVL 86

Expert Comment

by:CEHJ
ID: 10695170
The comment

>>// Add numbers if more needed

should be changed to

// Add numbers up to the batch size

to avoid confusion


0
 

Author Comment

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



0
 
LVL 86

Accepted Solution

by:
CEHJ earned 350 total points
ID: 10695363
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
 

Author Comment

by:Drop_of_Rain
ID: 10695726
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
 
LVL 86

Expert Comment

by:CEHJ
ID: 10695740
>>printSets();

Should be

printSets(8,4);
0
 

Author Comment

by:Drop_of_Rain
ID: 10695873
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
 

Author Comment

by:Drop_of_Rain
ID: 10695880
It could stop as well after the first gruop end with [8, 4, 3, 1, 7, 6, 2, 5]
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 10695955
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
 

Author Comment

by:Drop_of_Rain
ID: 10695981
I would agree with you. The problem I have is to not ask for to much.
0
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 86

Expert Comment

by:CEHJ
ID: 10696008
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
 

Author Comment

by:Drop_of_Rain
ID: 10696060
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
 
LVL 86

Expert Comment

by:CEHJ
ID: 10696094
>>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
 

Author Comment

by:Drop_of_Rain
ID: 10696145
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
 

Author Comment

by:Drop_of_Rain
ID: 10696175
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
 
LVL 86

Expert Comment

by:CEHJ
ID: 10698306
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
 

Author Comment

by:Drop_of_Rain
ID: 10698540
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
 
LVL 86

Expert Comment

by:CEHJ
ID: 10698654
>>this way there is no repeating patterns

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

Author Comment

by:Drop_of_Rain
ID: 10698743
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
 

Author Comment

by:Drop_of_Rain
ID: 10701077
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
 
LVL 86

Expert Comment

by:CEHJ
ID: 10703433
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
 

Author Comment

by:Drop_of_Rain
ID: 10705396
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
 

Author Comment

by:Drop_of_Rain
ID: 10705529
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
 
LVL 86

Expert Comment

by:CEHJ
ID: 10739353
:-)
0

Featured Post

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.

760 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now