public class Election
{
private Voter v1;
private Voter v2;
private Voter v3;
public String winner;
int myTotal = 0;
int hisTotal = 0;
public Election(double pa1, double pv1, double pa2, double pv2, double pa3, double pv3)
{
for(int i=1; i<=3500; i++)
v1=new Voter(pa1, pv1);
for(int i=1; i<=3500; i++)
v2=new Voter(pa2, pv2);
for(int i=1; i<=3000; i++)
v3=new Voter(pa3, pv3);
winner = "Unknown";
}
public void vote()
{
v1.vote();
if(v1.getVote() == 1)
myTotal++;
else if (v1.getVote() == -1)
hisTotal++;
v2.vote();
if(v2.getVote() == 1)
myTotal++;
else if (v2.getVote() == -1)
hisTotal++;
v3.vote();
if(v3.getVote() == 1)
myTotal++;
else if (v3.getVote() == -1)
hisTotal++;
if(myTotal > hisTotal)
winner = "my candidate";
else
winner = "the opponent";
}
public int myTotal()
{
return myTotal;
}
public int hisTotal()
{
return hisTotal;
}
public String toString()
{
String president;
president = "The winner is ";
president += winner;
president += "\n" + myTotal() + "\n" + hisTotal;
return president;
}
}
public class Voter
{
private double pattend;
private double pvote;
public int decision = 0;
public Voter(double pa, double pv)
{
pattend = pa;
pvote = pv;
}
public void vote()
{
if(Math.random() < pattend)
if(Math.random() < pvote)
decision = 1;
else
decision = -1;
}
public int getVote()
{
return decision;
}
}
I am supposed to have 10,000 voters, 3500 of the first kind (v1), 3500 of v2, and 3000 of v3. I created v1 v2 and v3, and when I try to loop them to create more of them, I still somehow get 3 voters. The way I have it now, I get anywhere from 0 to 3 votes, meaning that only 3 voters are voting, even though I thought the for loops would create 10,000 voters total. I tried using a while loop (and I gave it an exit), but the program still seems to continue indefinitely, meaning I get no output, no error message, nothing. Just Eclipse and Pico both telling me that the program is still running.
I'm confused about where I'm going wrong. Anyone help!?!