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)....
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...
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),...
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...
Java Language -Create a project and a class with a main method, TestCollectors. -Add new class,...
Java Language -Create a project and a class with a main method, TestCollectors. -Add new class, Collector, which has an int instance variable collected, to keep track of how many of something they collected, another available, for how many of that thing exist, and a boolean completist, which is true if we want to collect every item available, or false if we don't care about having the complete set. -Add a method addToCollection. In this method, add one to collected...
in java code In the class Hw2, write a method removeDuplicates that given a sorted array,...
in java code In the class Hw2, write a method removeDuplicates that given a sorted array, (1) removes the duplicates so that each distinct element appears exactly once in the sorted order at beginning of the original array, and (2) returns the number of distinct elements in the array. The following is the header of the method: public static int removeDuplicates(int[ ] A) For example, on input A=0, 0, 1, 1, 1, 2, 2, 3, 3, 4, your method should:...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT