| | |
Need help!
Please support our Java advertiser: Programming Forums - DaniWeb Sister Site
![]() |
•
•
Join Date: Sep 2008
Posts: 2
Reputation:
Solved Threads: 0
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.
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.
java Syntax (Toggle Plain Text)
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; } } } } }
Last edited by cscgal; Sep 26th, 2008 at 7:55 pm. Reason: Added code tags
•
•
Join Date: Jan 2008
Posts: 3,819
Reputation:
Solved Threads: 501
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.
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:
This shouldn't be in a class called Candidate:
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:
[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.
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:
Java Syntax (Toggle Plain Text)
String name; int count;
This shouldn't be in a class called Candidate:
Java Syntax (Toggle Plain Text)
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:
Java Syntax (Toggle Plain Text)
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.
Last edited by VernonDozier; Sep 25th, 2008 at 10:04 pm.
![]() |
Other Threads in the Java Forum
- Previous Thread: help!
- Next Thread: Unicode - character printing probelm
| Thread Tools | Search this Thread |
addball addressbook android api append applet application apps array arrays automation binary bluetooth businessintelligence button card chat class classes client code collision component crashcourse css csv database eclipse ee error exception fractal free game gis givemetehcodez graphics gui html ide image input integer integration j2me java javaarraylist javadoc javafx javaprojects jni jpanel julia jvm linux list loan loop machine map method methods migrate mobile netbeans newbie output phone physics print problem program programming project radio recursion reporting robot scanner screen se server service set size sms socket software sort sql string swing textfield threads transfer tree trolltech ubuntu utility windows






