Question

In: Computer Science

Problem 1 Write a Java program that implements a two-dimensional array of grades for students in...

Problem 1
        Write a Java program that implements a two-dimensional array of grades
        for students in a class. The grades should be as follows:
        Row 1: 87, 45
        Row 2: 69, 88, 90, 94
        Row 3: 79, 87, 94
        
        Compute and print out the maximum number of columns in the array.
        Hint: use a for loop.
        
   (5 Points.)
*/

/*      Problem 2
*       Suppose there are 4 candidates in 6 districts running for an election.
    Let's assume the candidates' names are: Jane, Amani, Doe, and Macbeth.
        Your task is to print out: 1) the total votes per candidate, and 2) the
        total votes per district.
        
        Here are some hints:
        1) Create a two-dimensional array of type int and name it ballots.
         int [][] ballots = { }. Fill it with votes. Remember, you are going to have
         four columns and six rows.
         
        2) Create the candidates as follows: String [] candidates = { } and
        assign them names.
        
        3) Create an object reference to hold the tally for the votes.
        
        4) Use a for loop to print the totals for candidates. 
        
        5) Use a for loop to print the totals for districts.
        
        Your output should look like:
        Total votes per candidate
        Jane
        x
        
        Amani
        x
        
        Doe
        x
        
        Macbeth
        x
        
        Total votes per district
        1
        x
        
        2
        x
        
        3
        x
        
        4
        x
        
        5
        x
        
        6
        x
*       (10 Points.)

Solutions

Expert Solution

solutions for Problem 1

import java.util.*;
class Problem1
{
   public static void main(String args[])
   {
   Scanner sc=new Scanner(System.in);
   int row=2;
   System.out.println("Enter maximum number of columns in the array : ");
   int col=sc.nextInt();
   // array declaration
   int a[][]=new int[row][col];       
   System.out.print("Enter " + row*col + " Grades to Store in Array :\n");
for (int i = 0; i < row; i++)
   {
   for(int j = 0; j < col; j++)
   {
    a[i][j] = sc.nextInt();
   }
   }   
System.out.print("Grades in Array are :\n");
for (int i = 0; i < row; i++)
   {
           System.out.print("Row "+(i+1)+":");

   for(int j = 0; j < col; j++)
   {
   System.out.print(a[i][j]+", ");
    }
       System.out.println();
   }
   }
}

Screen shot of the code

Problem 2 solution:

public class Problem2
{
public static void main(String[] args) {
int sumRow, sumCol;
//Initialize candidates
       String [] candidates = {"Jane", "Amani", "Doe", "Macbeth"};
       // initialize matrix[6][4] with votes
       int [][]ballots ={
                           {10000,10003,100004,2343},
                           {42343,535,4535,5656},
                           {676,877,323,3435},
                           {6667,2334,5565,2324},
                           {456,790,907,433},
                           {223,545,565,767}};
for(int i = 0; i < 6; i++){
sumRow = 0;
for(int j = 0; j < 4; j++){
sumRow = sumRow + ballots[i][j];
}
System.out.println("Total votes per district " + (i+1) +":" + sumRow);
}
       System.out.println("*********************************");
  
for(int i = 0; i < 4; i++){
sumCol = 0;
for(int j = 0; j < 6; j++){
sumCol = sumCol + ballots[j][i];
}
System.out.println("Total votes per candidate " + candidates[i] +": " + sumCol);
}
}
}


Related Solutions

C++ ASSIGNMENT: Two-dimensional array Problem Write a program that create a two-dimensional array initialized with test...
C++ ASSIGNMENT: Two-dimensional array Problem Write a program that create a two-dimensional array initialized with test data. The program should have the following functions: getTotal - This function should accept two-dimensional array as its argument and return the total of all the values in the array. getAverage - This function should accept a two-dimensional array as its argument and return the average of values in the array. getRowTotal - This function should accept a two-dimensional array as its first argument...
Write a Java program that will use a two-dimensional array to solve the following tasks: 1....
Write a Java program that will use a two-dimensional array to solve the following tasks: 1. Create a method to generate a 2-dimensional array (random numbers, range 0 - 500). The array has ROW rows and COL columns, where ROW and COL are class constants. 2. Create a method to print the array. 3. Create a method to find the largest element in the array 4. Create a method to find the smallest element in the array 5. Create a...
Write a Java program that will use a two-dimensional array and modularity to solve the following...
Write a Java program that will use a two-dimensional array and modularity to solve the following tasks: Create a method to fill the 2-dimensional array with (random numbers, range 0 - 30). The array has rows (ROW) and columns (COL), where ROW and COL are class constants. Create a method to print the array. Create a method to find the largest element in the array Create a method to find the smallest element in the array Create a method to...
Write a Java program that will use a two-dimensional array and modularity to solve the following...
Write a Java program that will use a two-dimensional array and modularity to solve the following tasks: 1. Create a method to generate a 2-dimensional array (random numbers, range 0 - 500). The array has ROW rows and COL columns, where ROW and COL are class constants. 2. Create a method to print the array. 3. Create a method to find the largest element in the array 4. Create a method to find the smallest element in the array 5....
Write a Java program that will use a two-dimensional array and modularity to solve the following...
Write a Java program that will use a two-dimensional array and modularity to solve the following tasks: Create a method to generate a 2-dimensional array (random numbers, range 0 - 500). The array has ROW rows and COL columns, where ROW and COL are class constants. Create a method to print the array. Create a method to find the largest element in the array Create a method to find the smallest element in the array Create a method to find...
IN JAVA Write a program that uses a two-dimensional array to store the highest and lowest...
IN JAVA Write a program that uses a two-dimensional array to store the highest and lowest temperatures for each month of the year. Prompt the user for 12 months of highest and lowest.   Write two methods : one to calculate and return the average high and one to calculate and return the average low of the year. Your program should output all the values in the array and then output the average high and the average low. im trying to...
(JAVA) Write a program that maintains student test scores in a two-dimesnional array, with the students...
(JAVA) Write a program that maintains student test scores in a two-dimesnional array, with the students identified by rows and test scores identified by columns. Ask the user for the number of students and for the number of tests (which will be the size of the two-dimensional array). Populate the array with user input (with numbers in {0, 100} for test scores). Assume that a score >= 60 is a pass for the below. Then, write methods for: Computing the...
Write a program in Java to do the following: -Create a one-dimensional array of 7 integers...
Write a program in Java to do the following: -Create a one-dimensional array of 7 integers as follows: Assign {35,20,-43,-10,6,7,13} -Create a one dimensional array of 7 Boolean values as follows: Assign {true,false,false,true,false,true,false} -Create a one dimensional array of 7 floating-point values as follows: Assign {12.0f,1.5f,-3.5f,-2.54f,3.4f,45.34f,22.13f} -Declare sum as integer and set it to 0. -Declare sumf as float and set it to 0.0f. -Use a for loop to go through each element of the Boolean array, and if an...
Write a Java program that creates a three-dimensional array. Populate each element with a string that...
Write a Java program that creates a three-dimensional array. Populate each element with a string that states each coordinate position in the array.
1.Write the java code to create a two dimensional String array of sizes 12 by 8;...
1.Write the java code to create a two dimensional String array of sizes 12 by 8; Given the java array:       double[] test = new double[3]; 2.  Write the java code to put the numbers 1.0, 2.0, and 3.0 in to the array so that the 1.0 goes in the first cell, the 2.0 goes in the second cell and the 3.0 goes in the third cell. 3. Can you have different types of data is a three dimensional array? 4....
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT