Sunteți pe pagina 1din 6

import java.awt.

FlowLayout;
import javax.swing.*;
import java.awt.event.*;
import java.awt.Container;
import java.util.Random;

/**
* Lab14 Description.
*
* @author Daniel Levengood
* @version mm/yyyy
*/
public class Lab14 implements ActionListener
{
// Application variables

private JFrame myFrame;

private JTextField txtInput;

private JButton cmdRun;


private JButton cmdExit;

private JTextArea txaOutput;

private static Random rand = new Random();

private double trials;

/**
* Schedule a job for the event-dispatching thread
* to create and show this application's GUI.
*
* @param args program arguments
*/
public static void main(String[] args)
{
javax.swing.SwingUtilities.invokeLater(new Runnable()
{
public void run()
{
Lab14 myLab14 = new Lab14();
myLab14.createAndShowGUI();
}
});
}

/**
* Create and show the graphical user interface.
*/
private void createAndShowGUI()
{

Container myPane;

txtInput = new JTextField(10);

cmdRun = new JButton("Run");


cmdRun.addActionListener(this);

cmdExit = new JButton("Quit");


cmdExit.addActionListener(this);

txaOutput = new JTextArea(35, 40);


txaOutput.setEditable(false);

// Set up and show the application frame

myFrame = new JFrame("Lab14");


myFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
myPane = myFrame.getContentPane();
myPane.setLayout(new FlowLayout());
myPane.add(new JLabel("Number of Trials"));
myPane.add(txtInput);
myPane.add(cmdRun);
myPane.add(cmdExit);
myPane.add(txaOutput);
myFrame.pack();
myFrame.setVisible(true);
txtInput.requestFocus();
}

/**
* Enable user interaction.
*
* @param ae the specified action event
*/
public void actionPerformed(ActionEvent ae)
{
String number;
String report;

int dotSum = 0;
int count = 0;
int freq = 0;
int prob = 0;

Object source = ae.getSource();

if (source.equals(cmdRun))
{
try
{
number = txtInput.getText();
trials = Double.parseDouble(number);
if (trials <= 0)
{
throw new Exception(
"The number of trials must be positive.");
}
else
{
while (count < trials)
{
dotSum = dotSum + rand.nextInt(6) + 1;
count++;
}

txaOutput.setText(
getReport(dotSum, freq, prob));
}
}
catch (NumberFormatException nfe)
{
txtInput.selectAll();
txtInput.requestFocus();
JOptionPane.showMessageDialog(
myFrame,
"The number of trials is not a number.",
"Number Format Error",
JOptionPane.ERROR_MESSAGE);
}
catch (Exception e)
{
txtInput.selectAll();
txtInput.requestFocus();
JOptionPane.showMessageDialog(
myFrame,
e.getMessage(),
"Number Format Error",
JOptionPane.ERROR_MESSAGE);
}
}
else
{
System.exit(0);
}
}
/**
* Get the report for the specified dot sum, frequency,
* and probability. The output shows the results from
* rolling the dice a selected number of trials.
*
* @param dotSum the sum of the dots
* @param frequency the frequency of the dice
* @param probability the probability of rolling a specified amount
*
* @return the report
*/
private String getReport(int dotSum,
int freq,
int prob)
{
int count;
int freq0;
int freq1;
int freq2;
int freq3;
int freq4;
int freq5;
int freq6;
int freq7;
int freq8;
int freq9;
int freq10;
double prob0;
double prob1;
double prob2;
double prob3;
double prob4;
double prob5;
double prob6;
double prob7;
double prob8;
double prob9;
double prob10;
String report;

int numDice = 2;

while (trials != 0)
{
Dice myDice = new Dice(numDice);
freq0 = 0;
freq1 = 0;
freq2 = 0;
freq3 = 0;
freq4 = 0;
freq5 = 0;
freq6 = 0;
freq7 = 0;
freq8 = 0;
freq9 = 0;
freq10 = 0;
for (count = 0; count < trials; count++)
{
switch (myDice.getDotSum())
{
case 2: freq0++;
break;
case 3: freq1++;
break;
case 4: freq2++;
break;
case 5: freq3++;
break;
case 6: freq4++;
break;
case 7: freq5++;
break;
case 8: freq6++;
break;
case 9: freq7++;
break;
case 10: freq8++;
break;
case 11: freq9++;
break;
case 12: freq10++;
}
}
prob0 = (double)freq0/(double)trials;
prob1 = (double)freq1/(double)trials;
prob2 = (double)freq2/(double)trials;
prob3 = (double)freq3/(double)trials;
prob4 = (double)freq4/(double)trials;
prob5 = (double)freq5/(double)trials;
prob6 = (double)freq6/(double)trials;
prob7 = (double)freq7/(double)trials;
prob8 = (double)freq8/(double)trials;
prob9 = (double)freq9/(double)trials;
prob10 = (double)freq10/(double)trials;

double freqSum = freq0 + freq1 + freq2 + freq3 + freq4 +


freq5 + freq6 + freq7 + freq8 + freq9 + freq10;

double probSum = prob0 + prob1 + prob2 + prob3 + prob4 +


prob5 + prob6 + prob7 + prob8 + prob9 + prob10;

return
String.format("%7s %9s %11s%n", "Dot Sum", "Frequency",
"Probability")+
String.format("%7s %9s %11s%n", "-------", "---------",
"-----------")+
String.format(" 2 %9d", freq0)+
String.format(" %11.3f%n", prob0)+
String.format(" 3 %9d", freq1)+
String.format(" %11.3f%n", prob1)+
String.format(" 4 %9d", freq2)+
String.format(" %11.3f%n", prob2)+
String.format(" 5 %9d", freq3)+
String.format(" %11.3f%n", prob3)+
String.format(" 6 %9d", freq4)+
String.format(" %11.3f%n", prob4)+
String.format(" 7 %9d", freq5)+
String.format(" %11.3f%n", prob5)+
String.format(" 8 %9d", freq6)+
String.format(" %11.3f%n", prob6)+
String.format(" 9 %9d", freq7)+
String.format(" %11.3f%n", prob7)+
String.format(" 10 %9d", freq8)+
String.format(" %11.3f%n", prob8)+
String.format(" 11 %9d", freq9)+
String.format(" %11.3f%n", prob9)+
String.format(" 12 %9d", freq10)+
String.format(" %11.3f%n", prob10)+
String.format("%7s %9s %11s%n", "-------", "---------",
"-----------")+
String.format("%18d", freqSum)+
String.format("%13.3f%n", probSum);

}}
}
}

S-ar putea să vă placă și