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!?!

Recommended Answers

All 6 Replies

I'm not sure I understand how the program works anyway. What would you put in a main function in order to do something with this? What do the parameters mean for Election? Like I would put this in main: new Election(??,??,??,??,??,??);

But what is supposed to go in the parameters?

I do have an election class with Election(value, value.....) etc I just didn't include it since that's about all it does. The first value is the probability that the first voter actually votes, the second value is the probability that he votes for my guy. The third value is the probability that the second voter votes and the fourth value is the probability that he votes for my guy, etc.

The program is an election simulation. We are supposed to have 10,000 voters - 3500 with one set of probabilities, 3500 with another set of probabilities, and 3000 with a final set of probabilities. I created 3 voters - v1, v2 and v3, so with the for loop I am trying to replicate them into 3500 and 3000 voters respectively. However, I don't understand why, it is not working correctly, and I still only get 3 votes at the end.

From what I get from the first glance at your program is that, your for loops are working correctly and you are creating 10,000 Voter objects but in the first for loop you are assigning all the 3500 objects to the same reference v1 in the next you are assigning all the 3500 objects to v2 and same for the third when instead you need to have an array of those many objects.

and how is that done exactly? i figured it must be something like that, but i dont know how to fix it

From what I get from the first glance at your program is that, your for loops are working correctly and you are creating 10,000 Voter objects but in the first for loop you are assigning all the 3500 objects to the same reference v1 in the next you are assigning all the 3500 objects to v2 and same for the third when instead you need to have an array of those many objects.

Good call man.... Yea, you are just assigning a new voter object to the same variable every time.... you want something like:

Voter[] v1List = new Voter[];

for (int i = 0; i < 3500; i++)
{
v1List = new Voter();
}

Voter[] v1List = new Voter[];
for (int i = 0; i < 3500; i++)
{
v1List[i] = new Voter();
}

This will throw a compiler error, "array dimension missing", which is caused by

Voter[] v1List = new Voter[];

The JVM would know what type of memory to allocate, but not how many such types there wouuld be, so the compiler won't allow this. Change this to:

Voter[] v1List = new Voter[3500]; // or however objects you need to create
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.