Question

In: Computer Science

Write a class Electionresults. Write the input inData (int a, int b) that creates (for example...

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) {

}

}

Solutions

Expert Solution

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
        }
}

Related Solutions

C Write a function int sort(int** arr, int n) that takes as input an array of...
C Write a function int sort(int** arr, int n) that takes as input an array of int pointers, multiplies the ints the pointers point to by -1, and then sorts the array such that the values pointed to by the pointers are in decreasing order. For example, if the array is size 10 the pointer at index 0 will point to the largest value after multiplying by -1 and the pointer at index 9 will point to the smallest value...
class A { public: //constructors // other members private: int a; int b; }; Give declatations...
class A { public: //constructors // other members private: int a; int b; }; Give declatations of operator functions for each of the following ways to overload operator + You must state where the declatation goes, whether within the class in the public or private section or outside the class. The operator + may be overloaded. a) as friend function b) as member function c) as non-friend, non-member function
in C++ Requirements: Write a program that creates a new class called Customer. The Customer class...
in C++ Requirements: Write a program that creates a new class called Customer. The Customer class should include the following private data: name - the customer's name, a string. phone - the customer's phone number, a string. email - the customer's email address, a string. In addition, the class should include appropriate accessor and mutator functions to set and get each of these values. Have your program create one Customer objects and assign a name, phone number, and email address...
Write a method with the following header: public static void showGradeDistribution(int a, int b, int c,...
Write a method with the following header: public static void showGradeDistribution(int a, int b, int c, int d, int f) It should print a graph (using asterisks) for each of the letters entered in the reverse order of the parameter list and with a label. In addition, if A and B grades sum is equal or exceeds that of grades C and D and F, the message “Strong class!” should be displayed. For example a method call of: showGradeDistribution(5,7,4,4,3); Would...
JAVA JAVA JAVA . I need to convert a string input to int array, for example...
JAVA JAVA JAVA . I need to convert a string input to int array, for example if user enters 12 / 27 / 2020 , I want to store each value in a separate array and add them afterwards.
Task: Write a program that creates a class Apple and a tester to make sure the...
Task: Write a program that creates a class Apple and a tester to make sure the Apple class is crisp and delicious. Instructions: First create a class called Apple  The class Apple DOES NOT HAVE a main method  Some of the attributes of Apple are o Type: A string that describes the apple. It may only be of the following types:  Red Delicious  Golden Delicious  Gala  Granny Smith o Weight: A decimal value representing...
C Write a function that takes as input an array of int pointers, multiplies the ints...
C Write a function that takes as input an array of int pointers, multiplies the ints the pointers point to by -1, and then sorts the array such that the values pointed to by the pointers are in decreasing order. For example, if the array is size 10 the pointer at index 0 will point to the largest value after multiplying by -1 and the pointer at index 9 will point to the smallest value after multiplying by -1. If...
Write a C function, for example void sumOfPrime(int n), that takes a positive int n as...
Write a C function, for example void sumOfPrime(int n), that takes a positive int n as a parameter, put all prime numbers less than n into an array, and print out the array and the sum of the numbers in that array. You can use the isPrime function above to save time.
Write a program that creates a Singleton class to connect to two databases. The program must...
Write a program that creates a Singleton class to connect to two databases. The program must provide two instances of this class. One instance for connecting to MySQL database and the other for connecting to Oracle database.
Using Java preferably with Eclipse Task: Write a program that creates a class Apple and a...
Using Java preferably with Eclipse Task: Write a program that creates a class Apple and a tester to make sure the Apple class is crisp and delicious. Instructions: First create a class called Apple The class Apple DOES NOT HAVE a main method Some of the attributes of Apple are Type: A string that describes the apple.  It may only be of the following types:  Red Delicious  Golden Delicious  Gala  Granny Smith Weight: A decimal value representing...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT