/* Author : Robert Magala */ /* Assignment : 6.30 */ /* Due Date : September 2003 */ /* Platform : Java 1.41 */ /* Summary : Coin toss */ import java.awt.* ; import java.awt.event.* ; import javax.swing.* ; public class toss3 extends JApplet implements ActionListener { boolean coin ; int totalHeads = 0 ; int totalTails = 0 ; JLabel flipLabel, totalHeadsLabel, totalTailsLabel ; JTextField flip, headsTotal, tailsTotal ; JButton toss ; public void init() { Container c = getContentPane() ; c.setLayout( new FlowLayout() ) ; flipLabel = new JLabel("Result") ; c.add (flipLabel) ; flip = new JTextField(10) ; flip.setEditable(false); c.add (flip) ; totalHeadsLabel = new JLabel("Heads Total") ; c.add (totalHeadsLabel) ; headsTotal = new JTextField(10) ; headsTotal.setEditable(false); c.add (headsTotal) ; totalTailsLabel = new JLabel("Tails Total") ; c.add (totalTailsLabel) ; tailsTotal = new JTextField(10) ; tailsTotal.setEditable(false); c.add (tailsTotal) ; toss = new JButton("Toss Coin") ; toss.addActionListener(this) ; c.add(toss ) ; } public void actionPerformed( ActionEvent e ) { flipit(); if (coin == true) { flip.setText("True/Heads") ; totalHeads++ ; headsTotal.setText(Integer.toString(totalHeads)); } if (coin == false) { flip.setText("False/Tails") ; totalTails++ ; tailsTotal.setText(Integer.toString(totalTails)) ; } } public boolean flipit() { int toss = 0 ; toss = 1 + (int) (Math.random() * 2) ; if (toss == 1 ) coin = true ; else if (toss == 2) coin = false ; return coin ; } }