In: Computer Science
Write a class Electionresults. Write the input inData (int a, int b) that creates (for example by random method) polls and returns two-dimensional arrays containing polls, where as the first dimension is the number of a political party and the second dimension is the constituency code.
The inData entry method includes integers, number of categories and number of constituencies. Write the program countingCategory that includes a two-dimensional state with data on the number of votes, and returns the total number of votes of each political party nationally in a one-dimensional state.
Write a main function that is a test tool that tries inData and countingCategory separately. a and b should be just some numbers doesn't matter, but numbers for political party (a) and numbers for constituency code(b). The beginning of the program should look something like this:
public class Electionresults {
public static int [] [] inn Data (int a, int b) {
}
public static int [] count Category (int [] [] tolur) {
}
public static invalid primary (String [] argument) {
}
}
The question is not clear so i assumed that the third function is not specified and for the rest two functions I've implemented the code. For inData() function I've generated some random values and stored into them and used the result for countCategory() for counting [for my ease use different array to calculate the count if needed].
import java.util.*;
import java.io.*;
public class Electionresults
{
public static void print2D(int mat[][])
{
// Loop through all rows
for (int[] row : mat)
// converting each row as string
// and then printing in a separate line
System.out.println(Arrays.toString(row));
}
public static int[][] inData(int a, int b) {
int[][] polls=new int[a][b];
for(int i=0;i<a;i++){
for(int j=0;j<b;j++){
polls[i][j]=(int)(Math.random() * 3223232);
}
}
return polls;
}
public static int[] countCategory(int[][] votes) {
int sum=0;
int[] count=new int[votes.length];
for(int i=0;i<votes.length;i++){
for(int j=0;j<votes[i].length;j++){
sum=sum+votes[i][j];
}
count[i]=sum;
sum=0;
}
return count;
}
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
System.out.println("enter a and b");
int a=sc.nextInt();
int b=sc.nextInt();
int[][] polls=inData(a,b);
//print2D(polls); uncomment this to visualize the polls matrix
int[] count=countCategory(polls);
//System.out.println(Arrays.toString(count)); uncomment this to visualize the sum
}
}