Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

In approval voting each voter specifies which candidates are acceptable. The win

ID: 3936890 • Letter: I

Question

In approval voting each voter specifies which candidates are acceptable. The winning candidate is the one who is voted acceptable on the most ballots. In java.

For example, suppose seven votes submit these ballots:

Moe Curly

Curly Larry

Larry

Gerry Larry Moe

Larry Moe Gerry Curly

Gerry Moe

Curly Moe

Here are the vote totals for the candidates, in alphabetical order.

Curly                      4

Gerry                    3

Larry                      4

Moe                      5

Here are the vote totals for the candidates, in decreasing order of vote totals.

Moe                      5

Curly                      4

Larry                      4

Gerry                    3

Implement a program to process approval voting ballots. The program should

1. prompt for the name of a text file that contains the ballots, one per line,

2. read the ballots,

3. display the vote totals with two tables as in the example above.

Be sure that your software is documented and thoroughly tested. Submit you work as a jar file.

Explanation / Answer

Please follow the code and comments for description :

CODE :

import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Map;
import java.util.Scanner;
import java.util.StringTokenizer;
import java.util.TreeMap;

public class ApproveVotingBallots {

    public static void main(String[] args) throws FileNotFoundException, IOException {
        Scanner sc = new Scanner(System.in);
        ArrayList<String> al = new ArrayList();      
        TreeMap<String, Integer> tmap = new TreeMap();
        System.out.println("Please Enter the File Name : ");
        String filename = sc.nextLine();
        File file = new File(filename + ".txt");
        BufferedReader br = new BufferedReader(new FileReader(file));
        String line = br.readLine();
        int namecount = 1;
        while (line != null) {
            StringTokenizer st = new StringTokenizer(line, " ");
            while (st.hasMoreTokens()) {
                al.add(st.nextToken());
            }
            line = br.readLine();
        }
        for (int i = 0; i < al.size(); i++) {
            if (tmap == null) {
                tmap.put(al.get(i), namecount);
            } else {
                if (tmap.containsKey(al.get(i))) {
                    tmap.put(al.get(i), tmap.get(al.get(i)) + namecount);
                } else {
                    tmap.put(al.get(i), namecount);
                }
            }
        }
        System.out.println("The Result in the Alphabetical Order is : ");
        for (Map.Entry m : tmap.entrySet()) {
            System.out.println(m.getKey() + " " + m.getValue());
        }
    }
}


OUTPUT :

Please Enter the File Name :
ballots
The Result in the Alphabetical Order is :
Curly 4
Gerry 3
Larry 4
Moe 5


Hope this is helpful.

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote