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

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
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...
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...
Create separate class with these members a, b, c, x, y, z int a b c...
Create separate class with these members a, b, c, x, y, z int a b c float x y z Demonstrate 3) A two arg float, int constructor, and a three arg int, float, float constructor to instantiate objects, initialize variables read from the keyboard, display the sum Note:- Please type and execute this above java program and also give the output for both problems. (Type a java program)
(java) Write a class called CoinFlip. The class should have two instance variables: an int named...
(java) Write a class called CoinFlip. The class should have two instance variables: an int named coin and an object of the class Random called r. Coin will have a value of 0 or 1 (corresponding to heads or tails respectively). The constructor should take a single parameter, an int that indicates whether the coin is currently heads (0) or tails (1). There is no need to error check the input. The constructor should initialize the coin instance variable to...
Write a Haskell function combine :: Int -> Int -> Int -> Int with the following...
Write a Haskell function combine :: Int -> Int -> Int -> Int with the following behavior: • When x, y, and z all correspond to digit values (i.e., integers between 0 and 9, inclusive), combine x y z returns the integer given by the sequence of digits x y z. (That is, x is treated as the digit in the hundreds place, y is treated as the digit in the tens place, and z is treated as the digit...
CODE IN JAVA: Write a ProductTester class that creates one Product object named "Juicer" with a...
CODE IN JAVA: Write a ProductTester class that creates one Product object named "Juicer" with a price of $50.99, prints the name and price of the product, reduces its price by 4.0, and then prints its price again. public class Product { private String name; private double price; public Product(String productName, double productPrice) { name = productName; price = productPrice; } public String getName() { return name; } public double getPrice() { return price; } public void reducePrice(double amount) {...
Purpose: To write an Object-Oriented application that creates a Java class with several instance variables, a...
Purpose: To write an Object-Oriented application that creates a Java class with several instance variables, a constructor to initialize the instance variables, several methods to access and update the instance variables’ values, along with other methods to perform calculations. Also, write a test class that instantiates the first class and tests the class’s constructor and methods. Details: Create a class called Rectangle containing the following: Two instance variables, An instance variable of type double used to hold the rectangle’s width....
Write a C++ program that creates a base class called Vehicle that includes two pieces of...
Write a C++ program that creates a base class called Vehicle that includes two pieces of information as data members, namely: wheels (type int) weight (type float) Program requirements (Vehicle class): Provide set and a get member functions for each data member. Your class should have a constructor with two parameters (one for each data member) and it must use the set member functions to initialize the two data members. Provide a pure virtual member function by the name displayData()...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT