944,103 Members | Top Members by Rank

Ad:
  • Java Discussion Thread
  • Unsolved
  • Views: 16482
  • Java RSS
Feb 26th, 2005
0

Help with Java programming for lottery

Expand Post »
Hey guys,
I need your help. I need to write a Java program for a simulation for a lottery. I got so far as I got numbers and everything. But how can i make sure that there is no number going to be there twice?
Thanks for your help
Julie
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Juliane123 is offline Offline
1 posts
since Feb 2005
Feb 26th, 2005
0

Re: Help with Java programming for lottery

Could you use an Else If type thing.
Reputation Points: 16
Solved Threads: 9
Master Poster
OurNation is offline Offline
780 posts
since Aug 2004
Feb 26th, 2005
0

Re: Help with Java programming for lottery

put all lottos numbers in an array and search through it....
use a do-while loop

make sure to initialize your array to something that would be invalidlike 0 to avoid a null pointer exception
Reputation Points: 13
Solved Threads: 4
Posting Whiz
paradox814 is offline Offline
351 posts
since Oct 2004
Feb 27th, 2005
0

Re: Help with Java programming for lottery

it might help if we could see your code...
Reputation Points: 12
Solved Threads: 2
Posting Whiz
Ghost is offline Offline
352 posts
since Aug 2004
Mar 13th, 2005
0

Re: Help with Java programming for lottery

This is from the Deitel Book. I also post the requirements they need. And to answer your question, look at number 6.

1. need to add for a "for" statement that will execute the application 4 times.
2. Using a 2 dimensional boolean array (uniqueNumber) . THe value of that variable in the array should be set to false. Add a nested "for" statement to set the 40 values to 'false'. I think it requires a for statement to index the array.
3. Init a String to hold selections.
4. Iterating over 5 numbers selected. Each time this statement executes, a single unique lottery value will be generated.
5. a do...while statment. Using a method "generate" pass it to args 0-39. To return a random number.
6. Add code to set the value in uniqueNumber represented by the selected number to true. It won't be selected again.
7. then it's supposed to display.
Java Syntax (Toggle Plain Text)
  1. // This application picks randomly generated numbers for a lottery.
  2. import java.awt.*;
  3. import java.awt.event.*;
  4. import java.text.DecimalFormat;
  5. import java.util.Random;
  6. import javax.swing.*;
  7.  
  8. public class LotteryPicker extends JFrame
  9. {
  10. // JLabel and JTextField for first lottery
  11. private JLabel oneJLabel;
  12. private JTextField oneJTextField;
  13.  
  14. // JLabel and JTextField for second lottery
  15. private JLabel twoJLabel;
  16. private JTextField twoJTextField;
  17.  
  18. // JLabel and JTextField for third lottery
  19. private JLabel threeJLabel;
  20. private JTextField threeJTextField;
  21.  
  22. // JLabel and JTextField for fourth lottery
  23. private JLabel fourJLabel;
  24. private JTextField fourJTextField;
  25.  
  26. // JButton to generate lottery numbers
  27. private JButton generateJButton;
  28.  
  29. // Random object to create random integers
  30. private Random generator = new Random();
  31.  
  32. // two-dimensional array to maintain unique random numbers
  33. private boolean[][] uniqueNumber = new boolean[ 4 ][ 40 ];
  34.  
  35. // one-dimensional array to store strings for output
  36. private String[] output = new String[ 4 ];
  37.  
  38. // instance variable to hold the selected lottery number
  39. private int selection;
  40.  
  41. // no-argument constructor
  42. public LotteryPicker()
  43. {
  44. createUserInterface();
  45. }
  46.  
  47. // create and position GUI components; register event handlers
  48. private void createUserInterface()
  49. {
  50. // get content pane for attaching GUI components
  51. Container contentPane = getContentPane();
  52.  
  53. // enable explicit positioning of GUI components
  54. contentPane.setLayout( null );
  55.  
  56. // set up oneJLabel
  57. oneJLabel = new JLabel();
  58. oneJLabel.setBounds( 16, 18, 100, 16 );
  59. oneJLabel.setText( "First lottery:" );
  60. contentPane.add( oneJLabel );
  61.  
  62. // set up oneJTextField
  63. oneJTextField = new JTextField();
  64. oneJTextField.setBounds( 120, 16, 124, 23 );
  65. oneJTextField.setHorizontalAlignment( JTextField.CENTER );
  66. oneJTextField.setEditable( false );
  67. contentPane.add( oneJTextField );
  68.  
  69. // set up twoJLabel
  70. twoJLabel = new JLabel();
  71. twoJLabel.setBounds( 16, 50, 100, 16 );
  72. twoJLabel.setText( "Second lottery:" );
  73. contentPane.add( twoJLabel );
  74.  
  75. // set up twoJTextField
  76. twoJTextField = new JTextField();
  77. twoJTextField.setBounds( 120, 48, 124, 23 );
  78. twoJTextField.setHorizontalAlignment( JTextField.CENTER );
  79. twoJTextField.setEditable( false );
  80. contentPane.add( twoJTextField );
  81.  
  82. // set up threeJLabel
  83. threeJLabel = new JLabel();
  84. threeJLabel.setBounds( 16, 82, 100, 16 );
  85. threeJLabel.setText( "Third lottery:" );
  86. contentPane.add( threeJLabel );
  87.  
  88. // set up threeJTextField
  89. threeJTextField = new JTextField();
  90. threeJTextField.setBounds( 120, 80, 124, 23 );
  91. threeJTextField.setHorizontalAlignment( JTextField.CENTER );
  92. threeJTextField.setEditable( false );
  93. contentPane.add( threeJTextField );
  94.  
  95. // set up fourJLabel
  96. fourJLabel = new JLabel();
  97. fourJLabel.setBounds( 16, 114, 100, 16 );
  98. fourJLabel.setText( "Fourth lottery:" );
  99. contentPane.add( fourJLabel );
  100.  
  101. // set up fourJTextField
  102. fourJTextField = new JTextField();
  103. fourJTextField.setBounds( 120, 112, 124, 23 );
  104. fourJTextField.setHorizontalAlignment( JTextField.CENTER );
  105. fourJTextField.setEditable( false );
  106. contentPane.add( fourJTextField );
  107.  
  108. // set up generateJButton
  109. generateJButton = new JButton();
  110. generateJButton.setBounds( 148, 150, 96, 24 );
  111. generateJButton.setText( "Generate" );
  112. contentPane.add( generateJButton );
  113. generateJButton.addActionListener(
  114.  
  115. new ActionListener() // anonymous inner class
  116. {
  117. // event handler called when generateJButton is clicked
  118. public void actionPerformed( ActionEvent event )
  119. {
  120. generateJButtonActionPerformed( event );
  121. }
  122.  
  123. } // end anonymous inner class
  124.  
  125. ); // end call to addActionListener
  126.  
  127. // set properties of application's window
  128. setTitle( "Lottery Picker" ); // set title bar string
  129. setSize( 264, 212 ); // set window size
  130. setVisible( true ); // display window
  131.  
  132. } // end method createUserInterface
  133.  
  134. // generate four random five-number lottery combinations
  135. private void generateJButtonActionPerformed( ActionEvent event )
  136. {
  137. (HELP HERE)
  138.  
  139. } // end method generateJButtonActionPerformed
  140.  
  141. // generate random number in a given range
  142. private int generate( int low, int high )
  143. {
  144. // generate random number in range from low to high
  145. return low + generator.nextInt( high - low + 1 );
  146.  
  147. } // end method generate
  148.  
  149. // main method
  150. public static void main( String[] args )
  151. {
  152. LotteryPicker application = new LotteryPicker();
  153. application.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
  154.  
  155. } // end method main
  156.  
  157. } // end class LotteryPicker
  158.  
  159. /**************************************************************************
  160.  * (C) Copyright 1992-2004 by Deitel & Associates, Inc. and *
  161.  * Pearson Education, Inc. All Rights Reserved. *
  162.  * *
  163.  * DISCLAIMER: The authors and publisher of this book have used their *
  164.  * best efforts in preparing the book. These efforts include the *
  165.  * development, research, and testing of the theories and programs *
  166.  * to determine their effectiveness. The authors and publisher make *
  167.  * no warranty of any kind, expressed or implied, with regard to these *
  168.  * programs or to the documentation contained in these books. The authors *
  169.  * and publisher shall not be liable in any event for incidental or *
  170.  * consequential damages in connection with, or arising out of, the *
  171.  * furnishing, performance, or use of these programs. *
  172.  **************************************************************************/
Code tags added. -Narue
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Mogster is offline Offline
1 posts
since Mar 2005
Mar 13th, 2005
0

Re: Help with Java programming for lottery

There are basically two ways to ensure that no number is repeated. The first way is to randomly permute a list of numbers with no duplicates. Then you can just ask for n numbers from that list and they should be fairly random:
Java Syntax (Toggle Plain Text)
  1. public class scratch {
  2. public static void main(String[] args)
  3. {
  4. new lottery ( 20 );
  5. }
  6. }
  7.  
  8. class lottery {
  9. private int[] numbers;
  10.  
  11. public lottery ( int n )
  12. {
  13. numbers = new int[n];
  14.  
  15. // Initialize lottery numbers
  16. for ( int i = 0; i < n; i++ ) {
  17. numbers[i] = i;
  18. }
  19.  
  20. show_all ( numbers );
  21. random_shuffle ( numbers );
  22. show_all ( numbers );
  23. }
  24.  
  25. private void random_shuffle ( int[] list )
  26. {
  27. for ( int i = 0; i < list.length - 1; i++ ) {
  28. int r = (int)( Math.random() * ( list.length - i ) );
  29. int save = list[i];
  30. list[i] = list[i + r];
  31. list[i + r] = save;
  32. }
  33. }
  34.  
  35. private void show_all ( int[] list )
  36. {
  37. for ( int i = 0; i < list.length; i++ ) {
  38. System.out.print ( list[i] + " " );
  39. }
  40. System.out.println();
  41. }
  42. }
The second way is to build a list straight from the random number generator and check which numbers you've already inserted to avoid inserting it again. There's a large number of ways you can go about this, the simplest being a search in the existing array for each new number:
Java Syntax (Toggle Plain Text)
  1. public class scratch {
  2. public static void main(String[] args)
  3. {
  4. new lottery ( 20 );
  5. }
  6. }
  7.  
  8. class lottery {
  9. private int[] numbers;
  10.  
  11. public lottery ( int n )
  12. {
  13. numbers = new int[n];
  14.  
  15. // Initialize lottery numbers
  16. int i = 0;
  17.  
  18. while ( i < n ) {
  19. int r = (int)( Math.random() * n );
  20.  
  21. if ( add ( numbers, i, r ) ) {
  22. ++i;
  23. }
  24. }
  25.  
  26. show_all ( numbers );
  27. }
  28.  
  29. private boolean add ( int[] list, int size, int val )
  30. {
  31. for ( int i = 0; i < size; i++ ) {
  32. if ( list[i] == val ) {
  33. return false;
  34. }
  35. }
  36.  
  37. list[size] = val;
  38.  
  39. return true;
  40. }
  41.  
  42. private void show_all ( int[] list )
  43. {
  44. for ( int i = 0; i < list.length; i++ ) {
  45. System.out.print ( list[i] + " " );
  46. }
  47. System.out.println();
  48. }
  49. }
Administrator
Reputation Points: 6442
Solved Threads: 1393
Bad Cop
Narue is offline Offline
11,807 posts
since Sep 2004

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Java Forum Timeline: Keeping a Java App. Open but Closing One of the Windows
Next Thread in Java Forum Timeline: Images





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC