Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 16 of 16

Thread: Java program for election voting machine

  1. #1
    Junior Member
    Join Date
    Apr 2009
    Posts
    25
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Java program for election voting machine

    Hi, so this is my final assignment for the semester so I thought I would setup a thread for it now because I will probably need a fair bit of ongoing help. (Thank you in advance to anyone that does give me a hand).
    Website of project: Java Programming (CITS1200)

    Basically I need to make a program that takes in the candidates name from an external text file. And then takes in the votes from a separate text file.
    I need 3 classes:
    * Vote - will represent a single vote and its preferences.
    * Candidate - will represent a single candidate and their votes.
    * Election - will represent an election process.

    I've been supplied this code (Which is apparently for reading text files):
    /*
     * Reads a file into an array of strings
     * 
     * Simple create a FileIO object with a valid file name,
     * and it will read the file into lines, and set noOfLines
     * 
     * e.g. reading the file f.txt containing "abc\nde\n\ngh\n" will return an object with
     * file = "f.txt" 
     * noOfLines = 4 
     * lines = {"abc", "de", "", "gh"}
     * 
     * Lyndon While, 2009
     */
     
    import java.io.*;
     
    class FileIO 
    {
       public String file;
       public String[] lines;
       public int noOfLines; // lines and nooflines will be consistent
     
       public FileIO (String f) 
       {file = f; 
        try {// Open the file
             FileInputStream fstream = new FileInputStream(file);
             // Convert fstream to a DataInputStream
             BufferedReader in = new BufferedReader(new InputStreamReader(fstream));
    	 // This will usually be way too big... 
             String[] toomanylines = new String[10000]; 
             // Read lines while they keep coming
             while (in.ready()) 
                   {toomanylines[noOfLines] = in.readLine(); noOfLines++;}
             // Close the data stream
             in.close();
             // set up lines with the right size
             lines = new String[noOfLines];
             for (int i = 0; i < noOfLines; i++) lines[i] = toomanylines[i];
            } 
        catch (Exception e) {System.err.println("File input error");}
       }
    }

    The project says I need to use SimpleCanvas which is just a graphical interface for BlueJ. But I'm planning on just focusing on getting the coding down pact. And then I can sort that stuff out later, because it's not really an important aspect in the scheme of things.

    If anyone has tips on where to start off it'd be much appreciated. According to my lecturer, Vote should be and easy class, Candidate a little bit harder, and Election the hardest.
    Not too good when I'm not entirely sure what I should be doing for vote? Haha.
    I'm sure people know how it is, a push and a shove and I'll keep on rolling Just no way of getting started on the hill. (Quite proud of that analogy)

    Thanks very much,
    Regard, Tom.


  2. #2
    mmm.. coffee JavaPF's Avatar
    Join Date
    May 2008
    Location
    United Kingdom
    Posts
    3,336
    My Mood
    Mellow
    Thanks
    258
    Thanked 287 Times in 225 Posts
    Blog Entries
    4

    Default Re: Election Program - Ongoing

    Hello bruint,

    Is the FileIO class an example or does this have to be included into the project?
    Please use [highlight=Java] code [/highlight] tags when posting your code.
    Forum Tip: Add to peoples reputation by clicking the button on their useful posts.

    Looking for a Java job? Visit - Java Programming Careers

  3. #3
    Junior Member
    Join Date
    Apr 2009
    Posts
    25
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Re: Election Program - Ongoing

    Included in the project

  4. #4
    Junior Member
    Join Date
    Apr 2009
    Posts
    25
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Re: Election Program - Ongoing

    Apologies for the double post, but I needed to bump this.
    I still don't know where to start. Any help, is very much appreciated.

    Just focussing on the vote class at the moment.

  5. #5
    mmm.. coffee JavaPF's Avatar
    Join Date
    May 2008
    Location
    United Kingdom
    Posts
    3,336
    My Mood
    Mellow
    Thanks
    258
    Thanked 287 Times in 225 Posts
    Blog Entries
    4

    Default Re: Election Program - Ongoing

    Hello bruint,

    This looks complicated!! My brain is in no fit state this morning to be working this out lol

    I've read through it but i'm still unsure as to how it's ment to work

    I'm looking at the Vote class. As you see here:

    Java Programming (CITS1200)

    You need to first setup the constructor and methods.

    This is the starting blocks:

    public class Vote {
     
        public Vote(String marks){
     
        }
     
        public String getPrefs(String prefs){
            return prefs;
        }
     
        public boolean isFormal(String candidates){
            return true;
        }
     
        public void delete(char loser){
     
        }
     
        public char preferred(String losers){
            return 'E';
        }
     
     
        public static void main(String[] args) {
     
     
        }
     
    }
    Please use [highlight=Java] code [/highlight] tags when posting your code.
    Forum Tip: Add to peoples reputation by clicking the button on their useful posts.

    Looking for a Java job? Visit - Java Programming Careers

  6. #6
    Junior Member
    Join Date
    Apr 2009
    Posts
    25
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Re: Election Program - Ongoing

    Hi JavaPF, thanks for getting back to me. But whilst I understand the structure of Java programs, I just don't know where to start in regards to what I need to do to go about taking what I get from FileIO and analysing it...


  7. #7
    mmm.. coffee JavaPF's Avatar
    Join Date
    May 2008
    Location
    United Kingdom
    Posts
    3,336
    My Mood
    Mellow
    Thanks
    258
    Thanked 287 Times in 225 Posts
    Blog Entries
    4

    Default Re: Election Program - Ongoing

    Quote Originally Posted by bruint View Post
    Hi JavaPF, thanks for getting back to me. But whilst I understand the structure of Java programs, I just don't know where to start in regards to what I need to do to go about taking what I get from FileIO and analysing it...

    I'm still trying to figure out what needs to go into FileIO in the first place.

    So lets start with the Vote class. If you understand this, then what is the Vote class ment to do?
    Please use [highlight=Java] code [/highlight] tags when posting your code.
    Forum Tip: Add to peoples reputation by clicking the button on their useful posts.

    Looking for a Java job? Visit - Java Programming Careers

  8. #8
    Junior Member
    Join Date
    Apr 2009
    Posts
    25
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Re: Election Program - Ongoing

    the Vote class takes from a text file, one line of letters say:
    ACBDE
    This represents one individuals vote.
    A is one candidate, B is another, C is another, etc.

    The vote class checks that it is a formal vote first, record if it isn't. To be a formal vote it must have the number of Candidates as explained by a candidates text file which will only have one line of letters ABCDE.
    If a voter has AcBDE, their vote is informal, if a voter has ABCD their vote is informal.

    The vote goes like this
    1. A
    2. B
    3. C
    4. D
    5. E

    It reads through all the votes in the text file, if D gets the least number of 1's then they aren't going to win, so for this voter, their D vote would be given to E. And this would be done for a number of people. Eg.
    1. D
    2. B
    3. C
    4. E
    5. A
    This persons D vote would be given to B.

    1. B
    2. C
    3. D
    4. E
    5. A
    This persons D vote would be given to E.

  9. #9
    mmm.. coffee JavaPF's Avatar
    Join Date
    May 2008
    Location
    United Kingdom
    Posts
    3,336
    My Mood
    Mellow
    Thanks
    258
    Thanked 287 Times in 225 Posts
    Blog Entries
    4

    Default Re: Election Program - Ongoing

    OK well to start we should use the Small example election text files.

    So we need to read in the candidates file first? CS.txt

    To do this, we need to give the file name to the FileIO constructor. If the Vote class is our main starting point:

        public static void main(String[] args) {
     
            FileIO fileio = new FileIO("SC.txt");        
     
        }

    The next step would be to get that information out of the array in the FileIO class?!
    Please use [highlight=Java] code [/highlight] tags when posting your code.
    Forum Tip: Add to peoples reputation by clicking the button on their useful posts.

    Looking for a Java job? Visit - Java Programming Careers

  10. #10
    Junior Member
    Join Date
    Apr 2009
    Posts
    25
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Re: Election Program - Ongoing

    right and that would be done by say...

    FileIO.lines[] = lines[]

    Or something? Which would just copy the array over?

  11. #11
    mmm.. coffee JavaPF's Avatar
    Join Date
    May 2008
    Location
    United Kingdom
    Posts
    3,336
    My Mood
    Mellow
    Thanks
    258
    Thanked 287 Times in 225 Posts
    Blog Entries
    4

    Default Re: Election Program - Ongoing

    You can print what we have sent over to FileIO like this: (Vote class)

        public static void main(String[] args) {
     
            FileIO fileio = new FileIO("SC.txt");
     
            for(int a = 0; a < fileio.lines.length; a++){
            System.out.println(fileio.lines[a]);
            }
     
        }

    What would be the next step?
    Please use [highlight=Java] code [/highlight] tags when posting your code.
    Forum Tip: Add to peoples reputation by clicking the button on their useful posts.

    Looking for a Java job? Visit - Java Programming Careers

  12. #12
    Junior Member
    Join Date
    Apr 2009
    Posts
    25
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Re: Election Program - Ongoing

    probably use the charAt [0], charAt[1], etc. to find which each is equal to and store each as a Char variable and then compare them to each new vote in the list seperately by using a loop that ends when it is equal to the noOfLines from fileIO?

    Can I just say THANKS SO MUCH for giving your time up for me

    Does what I explained above sound correct?

  13. #13
    mmm.. coffee JavaPF's Avatar
    Join Date
    May 2008
    Location
    United Kingdom
    Posts
    3,336
    My Mood
    Mellow
    Thanks
    258
    Thanked 287 Times in 225 Posts
    Blog Entries
    4

    Default Re: Election Program - Ongoing

    Quote Originally Posted by bruint View Post
    probably use the charAt [0], charAt[1], etc. to find which each is equal to and store each as a Char variable and then compare them to each new vote in the list seperately by using a loop that ends when it is equal to the noOfLines from fileIO?

    Can I just say THANKS SO MUCH for giving your time up for me

    Does what I explained above sound correct?
    Yes that sounds good.

    You seem to know exactly what is expected here which is a lot more than I can say for me!

    See how far you can get with it and I will help if I can when you get stuck..
    Please use [highlight=Java] code [/highlight] tags when posting your code.
    Forum Tip: Add to peoples reputation by clicking the button on their useful posts.

    Looking for a Java job? Visit - Java Programming Careers

  14. The Following User Says Thank You to JavaPF For This Useful Post:

    bruint (May 11th, 2009)

  15. #14
    Junior Member
    Join Date
    Apr 2009
    Posts
    25
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Re: Election Program - Ongoing

    haha, no, you've been a ton of help *clicks thanks*

    Thanks very much. Be back here soon enough

  16. #15
    Junior Member
    Join Date
    Apr 2009
    Posts
    25
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Re: Election Program - Ongoing

    ok. So I've worked on it a bit more with my friend who's done the course before (But a completely different project)
    Vote:
    /**
     * Represents a single vote and it's preferences
     * 
     * Thomas Bruining
     * v1.0
     */
    import java.lang.String;
     
    public class Vote
    {
     
    String vote;
    //Constructor
        public Vote(String marks){
          this.vote = marks;
     
        }
     //Methods
        public String getPrefs(){
            return vote;
        }
     
        //Returns true if the vote is formal< based on the fact that it is a candidate(non duplicate)    
        public boolean isFormal(String candidates){
            if(vote.length() == candidates.length()){
     
                for(int i = 0; i<= vote.length(); i++){
                    for(int j = 1; j < vote.length(); j++){
                        char[] voteA = vote.toCharArray();
     
                        if(voteA[i] == voteA[j]){
                            return false;
                        }else{ ;
     
                            for(int t = 0; t < vote.length(); t++){
                                for(int s = 0; s < vote.length(); s++){
                                     char[] candA = candidates.toCharArray();
     
                                     if(candA[t] != voteA[s]){
                                        return false;
                                     }else{
                                        return true;
                                     }
                                }
                            }
                        }
                    }
                }
            }
                return false;  
        }
     
        //Deletes loser from sequence of preferences 
        public void delete(char loser){
            char[] voteA = vote.toCharArray();
            for(int i = 0; i < vote.length(); i++){
                if(voteA[i] == loser){
                    voteA[i] = voteA[i+1];
                    voteA[vote.length() - 1] = ' ';
                    vote = new String(voteA);
                }
            }
        }
     
        //returns the preffered surviving candidate
        public char preferred(String losers){
            char[] losersA = losers.toCharArray();
            for(int i = 0; i < losers.length(); i++){
                if(vote.charAt(0) == losersA[i]){
                    char[] voteA = vote.toCharArray();
                    for(int n  = 0; n < vote.length(); n++){
                        voteA[n] = voteA[n+1];
                    }
                    voteA[vote.length() - 1] = ' ';
                    vote = new String(voteA);
                }
            }
            return vote.charAt(0);
        }
    }

    Vote should be completed. However, it may be worth reading through. Basically it is just a huge number of for loops to check various things.
    Next comes the Candidate Class:
    /**
     * Represents a single candidate and their votes
     * 
     * Thomas Bruining
     * v1.0
     */
    public class Candidate{
     
        char c;
        int v;
     
        public Candidate(char name, int n){
           this.c = name;
           this.v = n;
        }
     
        public char getName(){
            return Candidate.c;
        }
     
        public int getCount(){
            return Candidate.v;
        }
     
        public Vote[] getVotes(){
     
        }
     
        public boolean isWinner(int n){
            if(n > (Election.getNoOfVotes())/2){return true;}
        }
     
        public void getVotes(Vote[] papers, int n, String losers){
        }
    }

    Candidate is almost complete. However, I am somewhat stumped by the methods that I have yet to complete.
    "public Vote[] getVotes()" and "public void getVotes(Vote[] papers, int n, String losers)"

    And finally:
    /**
     * Represents the election process
     * 
     * Thomas Bruining
     * v1.0
     */
    public class Election{
     
     
     
        public Election(String candidatesFile, String votesFile){
            FileIO tempcand = new FileIO("SC.txt");
            candidatesFile = (String)tempcand;
            FileIO tempvotes = new FileIO("SV.txt");
            votesFile = (String)tempvotes;
        }
     
        public int getNoOfCandidates(){
     
        }
    }

    Which has barely been started.

    If you refer to my Original Post there is a ton of information in there.

    If someone could read through the Vote class and check for problems that may occur, and also help me with those 2 methods I mentioned above.

    Thanks very much to any help you can offer This is due on friday, so I'm beginning to get a bit..worried about it all as I'm a bit stumped. If it is of any help, election is one of the main classes, the other two are however important as they supply a number of helper methods.

  17. #16
    Junior Member
    Join Date
    Apr 2009
    Posts
    25
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Re: Election Program - Ongoing

    Update again:

    Vote (FINISHED):
    /**
     * Represents a single vote and it's preferences
     * 
     * Thomas Bruining
     * v1.0
     */
    import java.lang.String;
     
    public class Vote
    {
     
    String vote;
    //Constructor
        public Vote(String marks){
          this.vote = marks;
     
        }
     //Methods
        public String getPrefs(){
            return vote;
        }
     
        //Retuns true if the vote is formal< based on the fact that it is a candidate(non duplicate)    
        public boolean isFormal(String candidates){
            if(vote.length() == candidates.length()){
               if(vote == candidates){
                return true;
                }else{ 
                    for(int i = 0; i<= vote.length(); i++){
                        for(int j = 1; j < vote.length(); j++){
                            char[] voteA = vote.toCharArray();
     
                            if(voteA[i] == voteA[j]){
                                return false;
                            }else{ 
                                char[] candA = candidates.toCharArray();
                                for(int t = 0; t < vote.length(); t++){
     
                                         if(candA[t] == voteA[t]){
                                             return false;
                                          }else{ 
                                             return true;
                                          }
     
                                }
                            }  
                        }
                    }
                }
            }else return false;  
        return true;
        }
     
        //Deletes loser from sequence of preferences 
        public void delete(char loser){
            char[] voteA = vote.toCharArray();
            for(int i = 0; i < vote.length(); i++){
                if(voteA[i] == loser){
                    for(int j = i; j < vote.length(); j++){
                        if(j +1 != vote.length()){
                            voteA[j] = voteA[j+1];
                        }
                    }
                }
            }
            voteA[vote.length() - 1] = ' ';
            vote = new String(voteA);
        }
     
        //returns the preffered surviving candidate
        public char preferred(String losers){
            char[] losersA = losers.toCharArray();
            for(int i = 0; i < losers.length(); i++){
                if(vote.charAt(0) == losersA[i]){
                    char[] voteA = vote.toCharArray();
                    for(int n  = 0; n < vote.length(); n++){
                        for(int j = n; j < vote.length(); j++){
                            if(j +1 != vote.length()){
                                voteA[j] = voteA[j+1];
                            }
                        }
                    }
                    voteA[vote.length() - 1] = ' ';
                    vote = new String(voteA);
                }
            }
            return vote.charAt(0);
        }
    }

    Candidate: (FINISHED APART FROM FINAL METHOD -- <<<REALLY NEED HELP)
    /**
     * Represents a single candidate and their votes
     * 
     * Thomas Bruining
     * v1.0
     */
    public class Candidate{
     
        char c;
        int v = 0;
        Vote[] votes;
     
        public Candidate(char name, int n){
           c = name;
           votes = new Vote[n];
           v = n;
        }
     
        public char getName(){
            return c;
        }
     
        public int getCount(){
            return v;
        }
     
        public Vote[] getVotes(){
            return votes;
        }
     
        public boolean isWinner(int n){
            if(v >= n/2 + 1){return true;}
            else return false;}
     
     
        public void getVotes(Vote[] papers, int n, String losers){
           for(int i = 0; i <= n; i++){
     
            if(papers[i].preferred(losers) == c){
                    v++;
                }
     
            }
     
        }
    }

    public void getVotes(Vote[] papers, int n, String losers){
    I'm lost as to how to do this. Any help is greatly appreciated.
    Link:
    Java Programming (CITS1200)

    Election: (Unfinished and relatively unstarted <<NEED HELP)
    /**
     * Represents the election process
     * 
     * Thomas Bruining
     * v1.0
     */
     
     
    public class Election{
        int NumVotes;
        int NumCand;
        int formal;
        String canfile;
        String votefile;
        FileIO votes;
        FileIO cands;
     
        public Election(String candidatesFile, String votesFile){
     
            FileIO cand = new FileIO(candidatesFile);
            FileIO tempvotes = new FileIO(votesFile);
            canfile = candidatesFile;
            votefile = votesFile;
            NumCand = cand.lines[0].length();
            NumVotes = tempvotes.noOfLines;
            votes = tempvotes;
            cands = cand;
        }
     
       public int getNoOfCandidates(){
            return NumCand;
        }
     
       public int getNoOfVotes(){
     
     
        }
     
       public String getLosers(){
           return null;
        }
     
        public void eliminateInformalVotes(String names, Vote[] votes){
     
        }
     
        public char winner(){
        }
     
        public Candidate findAndDeleteLoser(){
        }
     
        public void nextRound(){
           FileIO tempcand = new FileIO(canfile);
            for (int i = 0; i < tempcand.lines.length; i++){
                Candidate temp = new Candidate(tempcand.lines[i], getNoOfVotes);
                if (temp.isWinner == true){System.out.println("The winner is" + tempcand.lines[i]);}
     
        }
     
        //public void displayCount(){
        //}
     
    }}

    FileIO: (Supplied for project - Needs to be incorporated into the Election class)
    /*
     * Reads a file into an array of strings
     * 
     * Simple create a FileIO object with a valid file name,
     * and it will read the file into lines, and set noOfLines
     * 
     * e.g. reading the file f.txt containing "abc\nde\n\ngh\n" will return an object with
     * file = "f.txt" 
     * noOfLines = 4 
     * lines = {"abc", "de", "", "gh"}
     * 
     * Lyndon While, 2009
     */
     
    import java.io.*;
     
    class FileIO 
    {
       public String file;
       public String[] lines;
       public int noOfLines; // lines and nooflines will be consistent
     
       public FileIO (String f) 
       {file = f; 
     
        try {// Open the file
             FileInputStream fstream = new FileInputStream(file);
             // Convert fstream to a DataInputStream
             BufferedReader in = new BufferedReader(new InputStreamReader(fstream));
    	 // This will usually be way too big... 
             String[] toomanylines = new String[10000]; 
             // Read lines while they keep coming
             while (in.ready()) 
                   {toomanylines[noOfLines] = in.readLine(); noOfLines++;}
             // Close the data stream
             in.close();
             // set up lines with the right size
             lines = new String[noOfLines];
             for (int i = 0; i < noOfLines; i++) lines[i] = toomanylines[i];
            } 
        catch (Exception e) {System.err.println("File input error");}
       }
     
     
    }

    Thanks for any help! I need it ASAP. This is worth a large percentage of my course, and I'd rather get a semi-acceptable mark for it to get me out of the depths for my exam.

    Thankyou in advance!!! Please help me get Candidate finished first.