1. ## Bar Graph

Hey I have a discrete Mathematics project due next week, and we have to do a probability graph. I have it all set up just I can't figure out what the x and y values to plot correctly. If someone could help me that would be much appreciated.
Java Code:
```import java.awt.Color;
import java.awt.Graphics;

import javax.swing.JComponent;

public class BarGraph extends JComponent implements Runnable
{

private Model myModel;
private Coin[] myCoinArray;

public static final double[] nValues = {
5, 5, 5, 5, 5, 5, 5, 15, 30,
50, 100, 150, 300, 500, 1000, 3000,
400, 700, 1500, 40000, 50000};

private int myN;
private boolean isSimulated;
private int myNumberOfRuns;
private boolean isSimulating;
private int[] myNumberOfTriesArray;
private int[] myNumberOfRepeatedTries;
private long mySpeed;

public BarGraph()
{
myN = 0;
this.start();
myModel = new Model();
isSimulated = false;
isSimulating = false;
}

private void start()
{

}

@Override
public void run()
{
while(true)
{
repaint();
if(isSimulating)
{
for(int i = 0; i < myNumberOfRuns; i++)
{
myNumberOfTriesArray[i] = myModel.getNumberOfTries();
myNumberOfRepeatedTries[i]++;
for(int k = 0; k < i - 1; k++)
{
if(myNumberOfTriesArray[i] == myNumberOfTriesArray[k])
{
myNumberOfRepeatedTries[i]++;
}
}
repaint();
this.runSimulation();
try{ Thread.sleep(mySpeed); } catch(Exception e) {}
}
isSimulating = false;
}
try{ Thread.sleep(0); } catch(Exception e) {}
}
}

public void paintComponent(Graphics g)
{
super.paintComponents(g);
g.setColor(Color.white);
g.drawLine(350, 10, 350, 530);
g.drawLine(350, 530, 1090, 530);
g.drawString("0", 310, 570);

for(int i = 1; i <= 10; i++)
{
g.drawLine(360 + (i * ((990 - 350) / 10)) - 10, 530,
360 + (i * ((990 - 350) / 10)) - 10, 550);

g.drawString(((double)(nValues[myN] / 10) * i) + ""
, 360 + (i * ((990 - 350) / 10)) - 20, 570);

g.drawLine(330, (i * ((530 - 10) / 10)) - 20, 350,
(i * ((530 - 10) / 10)) - 20);

g.drawString(((double)(nValues[myN] / 10) * (11 - i)) / nValues[myN] + ""
, 310, (i * ((530 - 10) / 10)) - 25);
}

int count = 0;
for(int k = 0; k <= myN / 5; k++)
{
for(int i = 0; i < 5; i++)
{
if(isSimulated && count < myN)
{
{
g.setColor(Color.green);
g.fillOval(i * 58 + 10, 10 + (k * 58), 48, 48);
g.setColor(Color.BLACK);
g.drawString("H", i * 58 + 30, (k * 58) + 35);
}
else
{
g.setColor(Color.red);
g.fillOval(i * 58 + 10, 10 + (k * 58), 48, 48);
g.setColor(Color.BLACK);
g.drawString("T", i * 58 + 30, (k * 58) + 35);
}
}
count++;
}
}
if(isSimulated)
{
for(int i = 0; i < myNumberOfRuns; i++)
{
g.setColor(Color.white);
g.drawRect(350 + (myNumberOfTriesArray[i] * 10),
520 - (myNumberOfTriesArray[i] * ( 10 * myNumberOfRepeatedTries[i]) / myNumberOfRuns) * 10,
10, 530 - (520 - (myNumberOfTriesArray[i] * (myNumberOfRepeatedTries[i] / 20) / myNumberOfRuns) * 10));
}
}
}

public void setN(int value)
{
myN = value;
myModel.setNumberOfCoins(myN);
}

{
}

public void runSimulation()
{
myModel.runSimulation();
myCoinArray = myModel.getCoinArray();
isSimulated = true;
isSimulating = true;
}

public void setNumberOfRepitions(int value)
{
myNumberOfRuns = value;
myNumberOfTriesArray = new int[value];
myNumberOfRepeatedTries = new int[value];
}

public void setSpeed(long value)
{
mySpeed = value;
}
}```

2. Your code doesn't compile. There are two classes missing: Model and Coin

3. Java Code:
```import java.awt.Color;
import java.awt.Component;
import java.awt.Container;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;

import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JSlider;

public class View extends JFrame implements ActionListener, MouseListener
{
private JSlider nSlider;
private BarGraph myBarGraph;
private JLabel nLabel;
private JButton enterButton;
private JSlider probabiltySlider;
private JLabel probabiltyLabel;
private JLabel nNameLabel;
private JLabel probabiltyNameLabel;
private JLabel numberRepitionsNameLabel;
private JSlider numberRepitionsSlider;
private JLabel numberRepitionsLabel;
private JSlider speedSlider;
private JLabel speedLabel;
private JLabel speedNameLabel;

public View()
{
Container contentPane = getContentPane();

this.setTitle("Odd Man Out");
this.setSize(1050, 700);
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
this.setAlwaysOnTop(true);
this.setLocationRelativeTo(null);
this.setResizable(false);
contentPane.setBackground(Color.black);

numberRepitionsNameLabel = new JLabel("Number of Repitions");
numberRepitionsNameLabel.setForeground(Color.white);
numberRepitionsNameLabel.setBounds(115, 575, 115, 50);

numberRepitionsSlider = new JSlider(JSlider.HORIZONTAL, 0, 1000, 500);
numberRepitionsSlider.setAutoscrolls(true);
numberRepitionsSlider.setMajorTickSpacing(1);
numberRepitionsSlider.setSnapToTicks(true);
numberRepitionsSlider.setBackground(Color.black);
numberRepitionsSlider.setForeground(Color.white);
numberRepitionsSlider.setBounds(250, 590, 200, 20);
numberRepitionsSlider.setToolTipText("NNumber Of Repitions");

numberRepitionsLabel = new JLabel("R = " + numberRepitionsSlider.getValue());
numberRepitionsLabel.setForeground(Color.white);
numberRepitionsLabel.setBounds(460, 575, 50, 50);

speedNameLabel = new JLabel("Speed in MilliSeconds");
speedNameLabel.setForeground(Color.white);
speedNameLabel.setBounds(115, 615, 125, 50);

speedSlider = new JSlider(JSlider.HORIZONTAL, 0, 1000, 500);
speedSlider.setAutoscrolls(true);
speedSlider.setMajorTickSpacing(1);
speedSlider.setSnapToTicks(true);
speedSlider.setBackground(Color.black);
speedSlider.setForeground(Color.white);
speedSlider.setBounds(250, 630, 200, 20);
speedSlider.setToolTipText("Speed");

speedLabel = new JLabel("S = " + speedSlider.getValue());
speedLabel.setForeground(Color.white);
speedLabel.setBounds(460, 615, 50, 50);

nNameLabel = new JLabel("Number of Coins");
nNameLabel.setForeground(Color.white);
nNameLabel.setBounds(515, 575, 95, 50);

nSlider = new JSlider(JSlider.HORIZONTAL, 3, 10, 6);
nSlider.setAutoscrolls(true);
nSlider.setMajorTickSpacing(1);
nSlider.setSnapToTicks(true);
nSlider.setBackground(Color.black);
nSlider.setForeground(Color.white);
nSlider.setBounds(630, 590, 200, 20);
nSlider.setToolTipText("Number Of Coins");

nLabel = new JLabel("N = " + nSlider.getValue());
nLabel.setForeground(Color.white);
nLabel.setBounds(840, 575, 50, 50);

probabiltyNameLabel = new JLabel("Probability of Heads");
probabiltyNameLabel.setForeground(Color.white);
probabiltyNameLabel.setBounds(515, 615, 115, 50);

probabiltySlider = new JSlider(JSlider.HORIZONTAL, 0, 100, 50);
probabiltySlider.setAutoscrolls(true);
probabiltySlider.setMajorTickSpacing(1);
probabiltySlider.setSnapToTicks(true);
probabiltySlider.setBackground(Color.black);
probabiltySlider.setForeground(Color.white);
probabiltySlider.setBounds(630, 630, 200, 20);

probabiltyLabel = new JLabel("p = " + probabiltySlider.getValue());
probabiltyLabel.setForeground(Color.white);
probabiltyLabel.setBounds(840, 615, 50, 50);

enterButton = new JButton("Enter");
enterButton.setBounds(900, 590, 90, 70);

myBarGraph = new BarGraph();

this.setVisible(true);

}
@Override
public void mouseClicked(MouseEvent arg0)
{
// TODO Auto-generated method stub

}

@Override
public void mouseEntered(MouseEvent arg0)
{
// TODO Auto-generated method stub

}

@Override
public void mouseExited(MouseEvent arg0)
{
// TODO Auto-generated method stub

}

@Override
public void mousePressed(MouseEvent arg0)
{
// TODO Auto-generated method stub

}

@Override
public void mouseReleased(MouseEvent e)
{
if(e.getSource() == nSlider)
{
nLabel.setText("n = " + nSlider.getValue());
}
if(e.getSource() == probabiltySlider)
{
probabiltyLabel.setText("p = " + probabiltySlider.getValue());
}
if(e.getSource() == numberRepitionsSlider)
{
numberRepitionsLabel.setText("R = " + numberRepitionsSlider.getValue());
}
if(e.getSource() == speedSlider)
{
speedLabel.setText("S = " + speedSlider.getValue());
}

}
@Override
public void actionPerformed(ActionEvent e)
{
if(e.getSource() == enterButton);
{
myBarGraph.setN(nSlider.getValue());
myBarGraph.setNumberOfRepitions(numberRepitionsSlider.getValue());
myBarGraph.setSpeed((long) speedSlider.getValue());
myBarGraph.runSimulation();
}
}
}```
Java Code:
```import java.util.Random;

public class Model
{
private Coin myCoinArray[];
private int myNumberOfCoins;
private int myNumberOfTails;
private int myNumberOfTries;

private Random rand;

public Model()
{
myNumberOfTries = 1;
rand = new Random();
}

private void initiateArray()
{
for(int i = 0; i < myNumberOfCoins; i++)
{
myCoinArray[i] = new Coin();
}

}

public void runSimulation()
{
this.flipCoins();
myNumberOfTries = 1;
while(myNumberOfHeads != 1 && myNumberOfTails != 1
||(myNumberOfHeads == 0 || myNumberOfTails == 0))
{
myNumberOfTails = 0;
myNumberOfTries++;
this.flipCoins();
}
}

public void flipCoins()
{
int temp;
for(int i = 0; i < myNumberOfCoins; i++)
{
temp = rand.nextInt(100);
{
temp = 1;
}
else
{
temp = 2;
}
switch(temp)
{
case 2: myCoinArray[i].setState(Coin.TAILS); myNumberOfTails++; break;
default : myCoinArray[i].setState(Coin.TAILS); myNumberOfTails++; break;
}
}
}

public String toString()
{
String temp = "";
for(int i = 0; i < myNumberOfCoins; i++)
{
temp += myCoinArray[i].getState() + "\n";
}
temp += myNumberOfTails + "\n";
temp += myNumberOfTries + "\n";
return temp;
}

public Coin[] getCoinArray()
{
return myCoinArray;
}

public int getNumberOfTries()
{
return myNumberOfTries;
}

public void setNumberOfCoins(int n)
{
myNumberOfCoins = n;
myCoinArray = new Coin[myNumberOfCoins];
this.initiateArray();
}

{
}
}```
Java Code:
```public class Demo
{

/**
* @param args
*/
public static void main(String[] args)
{
View myView = new View();
}

}```
Java Code:
```public class Coin
{
public static final int HEADS = 0;
public static final int TAILS = 1;
public static final int NO_STATE = -1;

private int myState;

public Coin()
{
myState = NO_STATE;
}

public int getState()
{
return myState;
}

public void setState(int aState)
{
myState = aState;
}

}```

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•