I have a project that I am working on for 5 days, and I am stuck. I have been able to generate some numbers, but no based on the instructions below. Can you please provide some assistance on what I missing and what I need to do to get this thing working. I have attached the code.

1. Generating random numbers.

a. Create a method in line 142 named generate that will take two ints, representing the low and high end of a range of random nubers and return a string containing a generated random number.

b. Drawing numbers for the games. Add code to the generateJButtonActionPerformed metod to call the generate method and display the generated numbers for all four games. Some lotteries allow repitition of numbers. To make this appliation simple, allow repetition of numbers for all the lotters.

itzi bitzi mitzi

Recommended Answers

All 5 Replies

For those who don't want to download the attached .doc file:

import java.awt.*;
import java.awt.event.*;
import java.text.DecimalFormat;
import java.util.Random;
import javax.swing.*;

public class LotteryPicker extends JFrame
{
   // JLabel and JTextField to display three number lottery
   private JLabel threeJLabel;
   private JTextField output3JTextField;

   // JLabel and JTextField to display four number lottery
   private JLabel fourJLabel;
   private JTextField output4JTextField;

   // JLabel and JTextField to display five number lottery
   private JLabel fiveJLabel;
   private JTextField output5JTextField;

   // JLabel and JTextField to display five number + one lottery
   private JLabel fivePlus1JLabel;
   private JTextField output5Plus1JTextField;
   private JTextField outputExtra1JTextField; 

   // JButton to generate new lottery numbers
   private JButton generateJButton;
  
   // create a new Random object
   private Random generator = new Random();
   private int game3one = 0 + generator.nextInt( 10 );
   private int game3two = 0 + generator.nextInt( 10 );
   private int game3three = 0 + generator.nextInt( 10 );
   
   //private int game3 = 0 + generator.nextInt( 10 );
   private int game4 = 0 + generator.nextInt( 10 );	
   private int game5 =  1 + generator.nextInt( 40 );
   private int game5Plus1 = 1 + generator.nextInt( 50 );
   private int game5PlusExtra = 1 + generator.nextInt( 43 );
   
   // no-argument constructor
   public LotteryPicker()
   {
      createUserInterface();
   }
   
   // create and position GUI components; register event handlers
   private void createUserInterface()
   {
      // get content pane for attaching GUI components
      Container contentPane = getContentPane();

      // enable explicit positioning of GUI components
      contentPane.setLayout( null );

      // set up threeJLabel
      threeJLabel = new JLabel();
      threeJLabel.setText( "Three number lottery:" );
      threeJLabel.setBounds( 16, 18, 132, 16 );
      contentPane.add( threeJLabel );

      // set up output3JTextField
      output3JTextField = new JTextField();
      output3JTextField.setBounds( 152, 16, 124, 23 );
      output3JTextField.setHorizontalAlignment( JTextField.CENTER );
      output3JTextField.setEditable( false );
      contentPane.add( output3JTextField );

      // set up fourJLabel
      fourJLabel = new JLabel();
      fourJLabel.setText( "Four number lottery:" );
      fourJLabel.setBounds( 16, 50, 132, 16 );
      contentPane.add( fourJLabel );

      // set up output4JTextField
      output4JTextField = new JTextField();
      output4JTextField.setBounds( 152, 48, 124, 23 );
      output4JTextField.setHorizontalAlignment( JTextField.CENTER );
      output4JTextField.setEditable( false );
      contentPane.add( output4JTextField );

      // set up fiveJLabel
      fiveJLabel = new JLabel();
      fiveJLabel.setText( "Five number lottery:" );
      fiveJLabel.setBounds( 16, 82, 132, 16 );
      contentPane.add( fiveJLabel );

      // set up output5JTextField
      output5JTextField = new JTextField();
      output5JTextField.setBounds( 152, 80, 124, 23 );
      output5JTextField.setHorizontalAlignment( JTextField.CENTER );
      output5JTextField.setEditable( false );
      contentPane.add( output5JTextField );

      // set up fivePlus1JLabel
      fivePlus1JLabel = new JLabel();
      fivePlus1JLabel.setText( "Five number + 1 lottery:" );
      fivePlus1JLabel.setBounds( 16, 114, 132, 16 );
      contentPane.add( fivePlus1JLabel );

      // set up output5Plus1JTextField
      output5Plus1JTextField = new JTextField();
      output5Plus1JTextField.setBounds( 152, 112, 92, 23 );
      output5Plus1JTextField.setHorizontalAlignment(
         JTextField.CENTER );
      output5Plus1JTextField.setEditable( false );
      contentPane.add( output5Plus1JTextField );

      // set up outputExtra1JTextField
      outputExtra1JTextField = new JTextField();
      outputExtra1JTextField.setBounds( 252, 112, 24, 23 );
      outputExtra1JTextField.setHorizontalAlignment(
         JTextField.CENTER );
      outputExtra1JTextField.setEditable( false );
      contentPane.add( outputExtra1JTextField );

      // set up generateJButton
      generateJButton = new JButton();
      generateJButton.setText( "Generate" );
      generateJButton.setBounds( 180, 152, 96, 24 );
      contentPane.add( generateJButton );
      generateJButton.addActionListener( 
      
         new ActionListener() // anonymous inner class
         {
            // event handler called when generateJButton is pressed
            public void actionPerformed( ActionEvent event )
            {
               generateJButtonActionPerformed( event );
            }
            
         } // end anonymous inner class
         
      ); // end call to addActionListener

      // set properties of application's window
      setTitle( "Lottery Picker" ); // set title bar string
      setSize( 300, 216 );          // set window size
      setVisible( true );           // display window
      
   } // end method createUserInterface
   
   // generate random lottery numbers
   private void generateJButtonActionPerformed( ActionEvent event )
   {
		
		output3JTextField.setText( String.valueOf( game3one + "  "
		  + game3two + "  " + game3three));
	//	output3JTextField.setText( String.valueOf( game3 ));
		output4JTextField.setText( String.valueOf( game4 ));
		output5JTextField.setText(String.valueOf( game5));
		output5Plus1JTextField.setText(String.valueOf( game5Plus1 ));
		outputExtra1JTextField.setText(String.valueOf( game5PlusExtra ));
			
			
   } // end method generateJButtonActionPerformed
   
   private void generate()
   {
    int low = 0;
    int high = 10;
    Random generator = new Random();
    String number = String.valueOf( low + generator.nextInt
       ( high ) );
     
   } // end method generate
   // main method
   public static void main( String[] args )
   {
      LotteryPicker application = new LotteryPicker();
      application.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );

   } // end method main
   
} // end class LotteryPicker

a. Create a method in line 142 named generate that will take two ints, representing the low and high end of a range of random nubers and return a string containing a generated random number.

If you want the method to return a String, you should not be declaring the method to return 'void' and you should be taking the low and high values as method arguments.

private void generate(int low, int high) {
    Random generator = new Random();

    return String.valueOf( low + generator.nextInt( high -low ) );
     
}

As a comment on your signature, many military elements, in many countries around the world, have played around with weather control, and China is actually actively attempting to manipulate the weather right now to avoid droughts in some areas of the country (it hasn't been working to well, but they are doing it). ;-)

commented: You here to answer the question, not to make silly comments on signature. If you don't know answer leave it be +0

As a comment on your signature, many military elements, in many countries around the world, have played around with weather control, and China is actually actively attempting to manipulate the weather right now to avoid droughts in some areas of the country (it hasn't been working to well, but they are doing it). ;-)

:lol:

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.