Java Reference
In-Depth Information
The algorithm for the method
main
is:
1. Declare and initialize the variables and the objects.
2. Open the input file
candData.txt
.
3. Read the data from the file
candData.txt
into the array
candidatesName
.
4. Sort the array
candidatesName
.
5. Open the input file
voteData.txt
.
6. Process
the voting data
and store the results
in the array
votesByRegion
.
7. Calculate the total votes received by each candidate and store the
results in the array
totalVotes
.
8. Print the heading.
9. Print the results.
PROGRAM LISTING
//**************************************************************
// Author: D.S. Malik
//
// Program: Election Results
// Given candidates' voting data, this program determines the
// winner of the election. The program outputs the votes
// received by each candidate and the winner.
//**************************************************************
import
java.io.*;
import
java.util.*;
public class
ElectionResults
{
final static int
NO_OF_CANDIDATES = 6;
final static int
NO_OF_REGIONS = 4;
public static void
main (String[] args)
throws
FileNotFoundException
{
//Step 1
String[] candidatesName =
new
String[NO_OF_CANDIDATES];
int
[][] votesByRegion =
new int
[NO_OF_CANDIDATES][NO_OF_REGIONS];
int
[] totalVotes =
new int
[NO_OF_CANDIDATES];
Scanner inFile =
new
Scanner(
new
FileReader("candData.txt"));
//Step 2
Search WWH ::
Custom Search