The Artima Developer Community
Sponsored Link

Java Answers Forum
Action Performed lotto generator

1 reply on 1 page. Most recent reply: Feb 29, 2004 3:10 PM by twc

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 1 reply on 1 page
Isa

Posts: 6
Nickname: isa
Registered: Sep, 2003

Action Performed lotto generator Posted: Feb 29, 2004 1:22 PM
Reply to this message Reply
Advertisement
I am doing homework trying to write a lotto program Writting a JAVA applet simulating a really dedicated lotto player. The applet will have 6 text fields for the player to enter 6 different integers in the range 1 to 53. There will be a button captioned Play. When the player clicks the button, a years worth of playing the lotto with those numbers is simulated. The program generates 104 sets (twice a week for 52 weeks) of 6 unique integers in the range 1 to 53. The player wins some money if a set of 6 numbers matches 3, 4, 5, or all 6 of his numbers. Each play (2 a week) costs $1. The payoffs are: match 3 , $5. match 4: $65, match 5 $1500, match all 6, $7,000,000! Your applet will have a non-editable text field in which the total winnings for the 104 plays will be displayed, and another non-editable text field that will show the cost of the plays. After the 104 plays have been tallied, the player will have the choice of continuing for another "year" with the same numbers (totals accumulating), by clicking the Play button again, or clicking another button captioned Reset, which clears all the text fields and totals and moves the focus to the first text field for the user to enter new numbers. When the play button is clicked after numbers have been entered, the program should validate the users entries and display an error message on a messageDialog if there are any numbers not in the range 1 to 53, or if there are any duplicate numbers. I am having trouble with my action command. I am trying to use get.Source to activate the generation of the numbers when i hit play. Is there an easy way of doing this. Thanx..



import java.awt.*;

import java.awt.event.*;


import javax.swing.*;



public class Assmt4 extends JApplet {



int select;
int lottoNum[] = new int [6];




public void init()

{

Container container = getContentPane();
container.setLayout(new FlowLayout());




JLabel numberLabel = new JLabel("Enter 6 numbers between >1 and 53<");
container.add(numberLabel);



JTextField[] numberField = new JTextField[6];


for(int i=1; i<numberField.length; i++){



numberField[i] = new JTextField(3);
container.add(numberField[i]);

}
container.setLayout(new FlowLayout());

JButton playButton = new JButton("Play");
playButton.addActionListener(this);
container.add( playButton);


JButton resetButton = new JButton("Reset");
resetButton.addActionListener(this);
container.add(resetButton);



container.setLayout(new FlowLayout());

JLabel winLabel = new JLabel("Winning ");
container.add(winLabel);

JTextField winField = new JTextField(5);
winField.setEditable(false);
container.add(winField);

JLabel playLabel = new JLabel("PlayCost");
container.add(playLabel);

JTextField playField = new JTextField(5);
playField.setEditable(false);
container.add(playField);

}



public void actionPerformed (ActionEvent event){

int number1,number2;

number1 = Integer.parseInt(winField.getText() );
number2 = Integer.parseInt(playField.getText() );
}
// numberField[select]=Integer.parseInt(playbutton.getSource())


// for( int i = 1; i <= 6; i++ )
// int number = 1 + (int)(Math.random() * 53 );
}

}


twc

Posts: 129
Nickname: twc
Registered: Feb, 2004

Re: Action Performed lotto generator Posted: Feb 29, 2004 3:10 PM
Reply to this message Reply
If you get two replies, it is because the first time I tried to post, nothing happened.

I am guessing that you are trying to use getSource() to figure out which button was pushed. There is a more OOP way to do this. Use inner classes.

Try the following.

//import statements
public class Assmt4 extends JApplet {
//skipping a lot of code to save typing
 
//change playButton.addActionListener(this); to
   playButton.addActionListener(new PlayButtonListener());
 
//change resetButton.addActionListener(this); to
   playButton.addActionListener(new ResetButtonListener());
 
//add these two methods
  private void doWhenPlayButtonPushed(){
     //put code to run when the play button is pushed in here
  }
 
  private void doWhenResetButtonPushed(){
     //put code to run when the reset button is pushed in here
  }
 
//create the following inner classes
  class PlayButtonListener implement ActionListener{
       public void actionPerformed (ActionEvent event){
          doWhenPlayButtonPushed();
       }
  }
 
  class ResetButtonListener implement ActionListener{
       public void actionPerformed (ActionEvent event){
          doWhenResetButtonPushed();
       }
  }
}//end of Assmt4 class


This will create a custom object for each button that only activates when that button is pushed and calls the appropriate method. If you need more buttons, you just add more inner classes. No long complicated if/else trees to mess with.

Flat View: This topic has 1 reply on 1 page
Topic: PLease Help with counter Previous Topic   Next Topic Topic: Python?

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use