Need help!

Please support our Java advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: Sep 2008
Posts: 2
Reputation: Darklighter201 is an unknown quantity at this point 
Solved Threads: 0
Darklighter201 Darklighter201 is offline Offline
Newbie Poster

Need help!

 
0
  #1
Sep 25th, 2008
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.

  1. import javax.swing.JOptionPane;
  2. public class test {
  3.  
  4. public static void main(String[] args) {
  5. //int count =0;
  6. int tracker = 0;
  7. Vote test = new Vote();
  8. Candidate names = new Candidate();
  9. while( tracker < 5) {
  10. String enterVote = JOptionPane.showInputDialog(" Enter 1 to vote for candidate One or Two to vote for candidate Two \n press 0 to quit." + tracker++);
  11.  
  12. int stringValue = Integer.parseInt (enterVote);
  13.  
  14. // test.increment();
  15. // System.out.println(test.getCount());
  16. //Vote updateCount = new Vote();
  17. // System.out.println(stringValue);
  18. if (stringValue == 1){
  19.  
  20. test.increment();
  21. System.out.println(test.getCount());
  22. }
  23. else{ test.decrement();
  24. {
  25.  
  26. }
  27. System.out.println(test.getCount());
  28.  
  29. }
  30. }
  31.  
  32.  
  33. class Vote {
  34.  
  35. int count ;
  36. Vote()
  37. {
  38. count = 0;
  39. }
  40.  
  41.  
  42. int getCount(){
  43.  
  44. return count;
  45. }
  46.  
  47. void setCount (int newCount){
  48. count = newCount;
  49. }
  50.  
  51. void clear (){
  52. count = 0;
  53. }
  54.  
  55. void increment(){
  56. count++;
  57. }
  58.  
  59. void decrement (){
  60. count--;
  61. }
  62.  
  63. //end class vote
  64.  
  65. class Candidate {
  66. int numberOfCandidates = 0;
  67. String name = "";
  68. Vote vote = new Vote();
  69.  
  70. Candidate(){
  71. numberOfCandidates++;
  72. }
  73.  
  74. Candidate(String newname, Vote newvote){
  75. name = newname;
  76. vote = newvote;
  77. }
  78.  
  79. String getname() {
  80. return name;
  81.  
  82. }
  83.  
  84. int getVote(){
  85. return vote.getCount();
  86.  
  87. }
  88.  
  89. int getNumberofCandidate(){
  90. return numberOfCandidates;
  91. }
  92.  
  93.  
  94.  
  95. }
  96. }
  97. }
  98. }
Last edited by cscgal; Sep 26th, 2008 at 7:55 pm. Reason: Added code tags
Reply With Quote Quick reply to this message  
Join Date: Jan 2008
Posts: 3,819
Reputation: VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute 
Solved Threads: 501
Featured Poster
VernonDozier VernonDozier is offline Offline
Senior Poster

Re: Need help!

 
0
  #2
Sep 25th, 2008
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:

  1. String name;
  2. int count;

This shouldn't be in a class called Candidate:
  1. 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:

  1. 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.
Reply With Quote Quick reply to this message  
Join Date: Sep 2008
Posts: 2
Reputation: Darklighter201 is an unknown quantity at this point 
Solved Threads: 0
Darklighter201 Darklighter201 is offline Offline
Newbie Poster

Re: Need help!

 
0
  #3
Sep 25th, 2008
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
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Other Threads in the Java Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC