Results 1 to 5 of 5
  1. #1
    besart is offline Member
    Join Date
    Dec 2012
    Posts
    2
    Rep Power
    0

    Default Creating a program that counts Votes??

    Hi, I am currently attending a CS1 class and I need help with a problem I have for homework. I have trouble understanding programs with multiple classes and methods. Below is the problem and below that is what I have so far...

    Create a public class called VoteCounter (i.e. create a VoteCounter class, in a file called VoteCounter.java). Each object of the VoteCounter class counts the votes for a particular candidate for the office of mayor, which is being contested. To do this, it supplies a public method, tallyVote(), which takes no parameters and returns no value, but which adds 1 to the total count that it is keeping.
    Separately, create a class called Voter, which has in it a main() method and represents the program that will be run.
    When Voter is run, it first asks the user to name the three (note: no more and no less than three) candidates running for mayor. For each candidate, it remembers the candidate’s name and creates a separate VoteCounter object to tally the votes for that candidate.
    The Voter program then asks the user to enter five names. Voter examines each name to see if it is the same name as one of the candidates (upper/lower case is ignored in this comparison). If it is the same, then the tallyVote() method of the lucky candidate is called; if it is not the same as any of the candidates, the entry is ignored.
    Finally, after all five names have been input, Voter displays the name and total vote count of each of the three candidates.

    This is what I have so far...

    public class VoteCounter {

    public VoteCounter(int c) {
    count = c;
    }

    public int getVoteCount() {
    return count;
    }

    public void tallyVote() {
    count++;
    }

    private int count = 0;
    }

    import java.util.Scanner;
    public class Voter {
    public static void main(String[] args) {
    Scanner k = new Scanner(System.in);
    System.out.println("Please enter 1st candidate name: ");
    String can1 = k.next();
    System.out.println("Please enter 2nd candidate name: ");
    String can2 = k.next();
    System.out.println("Please enter 3rd candidate name: ");
    String can3 = k.next();
    }
    }

    I don't know where to go from here. I understand what the program is supposed to do and I could do it in one class but the two classes are messing me up. Please help...

    Thank you,

    Besart Bytyqi

  2. #2
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,926
    Rep Power
    8

    Default Re: Creating a program that counts Votes??

    How would you do it in a single class?
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    besart is offline Member
    Join Date
    Dec 2012
    Posts
    2
    Rep Power
    0

    Default Re: Creating a program that counts Votes??

    I don't really know. I haven't tried to do it in one class. I might not be able to do it, I just know I don't really get how to make two classes work. I've had a few problems where two classes were required but I did them in one. The professor didn't accept my program so I don't want to try this one.

  4. #4
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,926
    Rep Power
    8

    Default Re: Creating a program that counts Votes??

    What about two classes is confusing you?

    Note that you're already using quite a few other classes. Scanner is another class, as is String. Using your own class is exactly like using those.
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  5. #5
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,931
    Rep Power
    19

    Default Re: Creating a program that counts Votes??

    Break down the requirements.
    You have your VoteCounter class.
    "Each object of the VoteCounter class counts the votes for a particular candidate for the office of mayor, which is being contested."
    So you want one for each candidate.
    "When Voter is run, it first asks the user to name the three (note: no more and no less than three) candidates running for mayor."
    In fact you want exactly 3, since there are 3 candidates.
    Since you need to store the names as well it looks to me like two arrays, one of String[] for the names, and another of VoteCounter[] for the votes.

    That should give you enough to be going on with.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

Similar Threads

  1. Replies: 3
    Last Post: 10-16-2012, 10:47 PM
  2. Replies: 2
    Last Post: 10-06-2012, 04:29 AM
  3. Replies: 5
    Last Post: 03-30-2012, 08:53 PM
  4. Replies: 13
    Last Post: 10-31-2010, 08:42 PM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •