I need to finish this assignment by tonight. I have been working on it for the last five hours and I am completely stuck.

Any help would be greatly appreciated! I have already talked to my teacher, but she is awful. No help at all.

This is what I have to do.
Write a test program that counts votes for two candidates for student body president. The votes are entered from the keyboard. Number 1 is a vote for Candidate 1, and number 2 is a vote for Candidate 2. Number -1 deducts a voter from Candidate 1, and number -2 deducts a vote from Candidate 2. Number o signifies the end of the count. Display the name and votes for each candidate and the student body president.

Here is what I have so far.

import javax.swing.JOptionPane;
public class test {

	public static void main(String[] args) {
  		//int count =0;
		int tracker = 0;
		Vote test = new Vote();
		Candidate names = new Candidate();
		while( tracker < 5) {
		String enterVote = JOptionPane.showInputDialog(" Enter 1 to vote for candidate One or Two to vote for candidate Two \n press 0 to quit." + tracker++);
		
		int stringValue = Integer.parseInt (enterVote);
		
	//	test.increment();
	//	System.out.println(test.getCount());
		//Vote updateCount = new Vote();
	//	System.out.println(stringValue);
		if (stringValue == 1){
		
			 test.increment();
			 System.out.println(test.getCount()); 
			}
			else{ test.decrement();
			{
			  
			}
	System.out.println(test.getCount());

			}
			}


 class Vote {
	
	int count ;
	Vote() 
	{
	  count = 0;
	}
	

	 int getCount(){
	
		return count;
	}
	
	void setCount (int newCount){
		count = newCount;
	}

	void clear (){
	 	count = 0;
	}
	
	void increment(){
	count++;
	}
	
	void decrement (){
	count--;	
	}
	
//end class vote	
	
class Candidate {
	int numberOfCandidates = 0;
	String name = "";
	Vote vote = new Vote(); 
	
	Candidate(){
	numberOfCandidates++;
	}
	
	Candidate(String newname, Vote newvote){
	name = newname;
	vote = newvote;
	}
	
	String getname() {
		return name;
	
	}
	
	int getVote(){
   	return vote.getCount();
	
	}
	
	int getNumberofCandidate(){
		return numberOfCandidates;
		}
		

		
		}
	}
	}
	}

Recommended Answers

All 2 Replies

One, if you aren't required to do it using Swing, do a console program, in my opinion. You're already outputting to console anyway. Two, use better names for your classes and variables. It's really hard to follow. test ? tracker ? names is of type Candidate ?

I'd get rid of the Vote class. It has a field called count. I'd put count in Candidate and have it have two fields:

String name;
int count;

This shouldn't be in a class called Candidate:

int numberOfCandidates;

If you wanted to have an Election class and put that as a field, fine, but like the Vote class implies a single vote, Candidate implies a single candidate.

I'd say you need to go completely back to the drawing board on this design.

Three, use code tags and formatting. It's impossible to read the way it is:

[code=JAVA] // paste code here

[/code]

Four, you don't have an increment function in the test class, so this isn't going to work:

test.increment();

[edit]
Just noticed that you have a variable called test of type Vote. Really bad idea. Not only is it completely non-descriptive, but it's confusing since that's the name of one of your classes too.

Ok, thanks for the prompt reply.
I'm going to try this one more time, then just submit it and hope for at least half of the points.

Thanks again

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.