Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

1- a- The method findMax() has the following header: public static int findMax(i

ID: 3581185 • Letter: 1

Question

1- a-

The method findMax() has the following header:

public static int findMax(int [] numbers)

It computes and returns the max value of the array numbers[].

Write a program that does the following:

Declare an integer array numArray[] with 10 elements, initialized to 0.

Prompt the user to enter 10 numbers that will be stored in numArray

Call findMax() to calculate the max of numArray

Print the mean.

You don’t have to write the findMax() method for this question.

b- The method findMin() has the following header:

public static int findMax(int [][] numbers, int index)

It computes and returns the max value of the specified row in a two dimensional array numbers.

For example, if [][] num is:

1 5 9 60 7

5 14 6 8 12

10 15 90 6 30

In the main method, the following command is used:

findMin (num, 1);

One would expect the method to return 14.

*method name changed from findMin to findMax in this version to avoid confusion

Explanation / Answer

1)a)

OneDimArray.java

import java.util.Scanner;

public class OneDimArray {

   public static void main(String[] args) {
      
       //Declaring variables
       int size,max;
       double sum=0,mean=0.0;
      
       /* Scanner class object is used to read
       * the inputs entered by the user
       */
   Scanner sc=new Scanner(System.in);
  
   //Getting the inputs entered by the user
   System.out.print("How many numbers you want to enter :");
   size=sc.nextInt();
  
  
   //Creating an integer array
   int numbers[]=new int[size];
  
  
   /* Getting the inputs entered by the
   * user and populate them into an array
   */
   for(int i=0;i<size;i++)
   {
       //Getting the input entered by the user
       System.out.print("Enter number "+(i+1)+":");
       numbers[i]=sc.nextInt();
      
       //Calculating the sum
       sum+=numbers[i];
   }
  
   //Calculating the average
   mean=sum/size;
  
   //Calling the method
   max=findMax(numbers);
  
   //Displaying the maximum element of an array
   System.out.println("The Maximum Element in the array is :"+max);
  
   //Displaying the mean of array elements
   System.out.println("The Mean of all the elements in the array is :"+mean);
   }

   //This method will find the maximum element and returned it
   private static int findMax(int[] numbers) {
       int max=0;
      
       for(int i=0;i<numbers.length;i++)
       {
           if(numbers[i]>max)
               max=numbers[i];
       }
       return max;
   }

}

________________________

output:

How many numbers you want to enter :10
Enter number 1:23
Enter number 2:45
Enter number 3:56
Enter number 4:67
Enter number 5:87
Enter number 6:43
Enter number 7:32
Enter number 8:11
Enter number 9:12
Enter number 10:54
The Maximum Element in the array is :87
The Mean of all the elements in the array is :43.0

_______________________

1)b)

package org.students;

import java.util.Scanner;

public class TwoDimArray {

   public static void main(String[] args) {
       //Declaring variables
               int rows,cols,count=0,index=0,max=0;
              
               /* Scanner class object is used to read
               * the inputs entered by the user
               */
           Scanner sc=new Scanner(System.in);
          
           //Getting the inputs entered by the user
           System.out.print("Enter No of Rows :");
           rows=sc.nextInt();
          
           System.out.print("Enter No of Columns :");
           cols=sc.nextInt();
          
           //Creating an integer array
           int numbers[][]=new int[rows][cols];
          
          
           /* Getting the inputs entered by the
           * user and populate them into an array
           */
           for(int i=0;i<rows;i++)
           {
               for(int j=0;j<cols;j++)
               {
                   //Getting the input entered by the user
                   System.out.print("Enter number "+(++count)+":");
                   numbers[i][j]=sc.nextInt();                  
               }
           }
          
          
           System.out.println(" Dispalying the Array :");
          
           //This nested for loop will display the array
           for(int i=0;i<rows;i++)
           {
               System.out.print("Row#"+(i+1)+": ");
               for(int j=0;j<cols;j++)
               {
                   System.out.print(numbers[i][j]+" ");
               }
               System.out.println(" ");
           }
          
           //Getting the index value entered by the user
           System.out.print(" Enter the row value :");
           int num=sc.nextInt();
           index=num-1;
          
           //Calling the method
           max=findMax(numbers,index);
          
           //Displaying the maximum element of an array
           System.out.println(" The Maximum Element in the row "+ num +" of an array is :"+max);
          
   }

   //This method will find out the maximum element in the array
   private static int findMax(int[][] numbers, int index) {
       int max=numbers[index][0];

       for(int j=0;j<numbers[index].length;j++)
       {
       if(numbers[index][j]>max)
       {
           max=numbers[index][j];
       }
      
       }
       return max;
   }

}

__________________________

output:

Enter No of Rows :4
Enter No of Columns :4
Enter number 1:12
Enter number 2:23
Enter number 3:34
Enter number 4:43
Enter number 5:56
Enter number 6:67
Enter number 7:76
Enter number 8:88
Enter number 9:77
Enter number 10:66
Enter number 11:55
Enter number 12:99
Enter number 13:10
Enter number 14:19
Enter number 15:29
Enter number 16:39

Dispalying the Array :
Row#1: 12 23 34 43

Row#2: 56 67 76 88

Row#3: 77 66 55 99

Row#4: 10 19 29 39


Enter the row value :3

The Maximum Element in the row 3 of an array is :99

_________________Thank You