Question

In: Computer Science

2-Dimensional Array Operations. Use a Java class with a main method. In this class (after the...

2-Dimensional Array Operations.

Use a Java class with a main method.

In this class (after the main method), define and implement the following methods:

  • public static void printArray. This method accepts a two-dimensional double array as its argument and prints all the values of the array, separated by a comma, each row in a separate line.
  • public static double getAverage. This method accepts a two-dimensional double array as its argument and returns the average of all the values in the array.
  • public static double getRowTotal. This method accepts a two-dimensional double array as its first argument and an integer value as its second argument. The second argument is the subscript of a row in the array. The method returns the total of the values in the specified row.
  • public static double getLowestInColumn. This method accepts a two-dimensional double array as its first argument and an integer value as its second argument. The second argument is the subscript of a column in the array. The method should return the lowest value in the specified column of the array.

In the main method create and initialize a 2-dimensional array of double numbers and test all the methods described above.

Solutions

Expert Solution

public class Main
{
   public static void main(String[] args) {
       double p[][] = {{9,7,4},{1,6,3},{4,2,8}}; //Declaring 2d array
       printArray(p); //calling printArray function
       double ans;
       ans = getAverage(p); //calling getAverage function and storing result in ans
       System.out.println("Average of all elements is: "+ans); //printing the ans
       double sum;
       sum = getRowTotal(p,2); //calling getRowTotal function and storing result in sum
       System.out.println("Sum of row is: "+sum); //printing the sum of row with index 2
       double low;
       low = getLowestInColumn(p,2); //calling getLowestInColumn function and storing result in low
       System.out.println("Lowest in column is: "+low); //printing the low of column with index 2
   }
   public static void printArray(double p[][]) //printArray function
   {
   for(int i=0;i<3;i++)
   {
   for(int j=0;j<3;j++)
   {
   System.out.print(p[i][j]); //printing the elements
   System.out.printf(",");
   }
   System.out.printf("\n");
   }
   }
   public static double getAverage(double p[][]) //getAverage function
   {
   double sum=0,ans=0;
   for(int i=0;i<3;i++)
   {
   for(int j=0;j<3;j++)
   {
   sum = sum + p[i][j]; //adding the elements
   }
   }
   ans = sum / 9; //calculating the ans
   return ans; //returning the ans
   }
   public static double getRowTotal(double p[][],int q) //getRowTotal function
   {
   double ans=0;
   ans = p[q][0] + p[q][1] + p[q][2]; //adding the elements of row
   return ans; //returning the ans
   }
   public static double getLowestInColumn(double p[][],int q) //getLowestInColumn function
   {
   if(p[0][q]<p[1][q] && p[0][q]<p[2][q]) //cheking if first element in column is low
   return p[0][q];
   else if(p[1][q]<p[2][q]) //cheking if second element in column is low
   return p[1][q];
   else
   return p[2][q];
   }
}

Output:-


Related Solutions

Write a java method that creates a two dimensional char array after asking the user to...
Write a java method that creates a two dimensional char array after asking the user to input a String text (for example, "Sara" which is entered by the user)  and String key consisting of integers (for example, 2314) only, such that int rows=(int)Math.ceil(text.length()/key.length())+1; int columns= key.length(); The method fills the 2d array with letters a String entered by the use (column by column). The method then shifts the columns of the array based on key. For example, if the user enter...
Write a java method that creates a two dimensional char array after asking the user to...
Write a java method that creates a two dimensional char array after asking the user to input a String text and String key consisting of integers only, such that int rows=(int)Math.ceil(text.length()/key.length()); // or int rows=(int)Math.ceil(text.length()/key.length()); ? int columns= key.length(); int remainder= text.length() % key.length(); // such that the last row avoids taking an index beyond the string text by making columns - remainder The method fills the 2d array with letters a String entered by the use (row by row)....
Java programming Create a application with a method void (after main() ) that creates an array...
Java programming Create a application with a method void (after main() ) that creates an array and asks for the user fill it with float numbers. This array have infinite elements until the user decide that it is enough and input a command to stop. Display this array's elements data in another method void. Thanks for your help!
Create a “Main” method that contains two 2-Dimensional arrays of characters. The first array, which we...
Create a “Main” method that contains two 2-Dimensional arrays of characters. The first array, which we will call our visible field, needs to be 5 by 5 and should initially hold the underscore character “_”.  This will be used in our game of minesweeper to represent the possible locations the player can check. The second array, which we will call our hidden field, should also be 5 by 5 but filled with the capital letter "S” which means safety. However, we...
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 C++ for netbeans. Use a 1 dimensional array object to create a 2 dimensional table...
In C++ for netbeans. Use a 1 dimensional array object to create a 2 dimensional table object. Then modify to create a triangular table. Objective -> create an array of dynamic objects of RowAray inside Table i.e. an Aggregate. See Specs RowAray.h, Table.h Then create a triangular table, i.e. Triangle. Fill each cell with random 2 digit integers. The example Table has 8 columns of RowAray objects each filled with 6 rows of random 2 digit numbers. Then create a...
Required components: A controller class with Java main() method and a PaymentCalculator class to hold the...
Required components: A controller class with Java main() method and a PaymentCalculator class to hold the data and methods for the application. Scenario/Information: If you carry a balance on a credit card, it can be nice to see how long it would take to payoff the card. For this scenario, you should design a Java application the prints a credit card payment schedule. Inputs for your program should include: Customer’s name (first and last), the account number (as an integer),...
Java Class Create a class with a main method. Write code including a loop that will...
Java Class Create a class with a main method. Write code including a loop that will display the first n positive odd integers and compute and display their sum. Read the value for n from the user and display the result to the screen.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT