Java 913 The Location class Design a class named Location fo

Java

9.13 (The Location class) Design a class named Location for locating a maximal value and its location in a two-dimensional array. The class contains public data fields row, column, and maxValue that store the maximal value and its indices in a two-dimensional array with row and column as int types and maxValue as a double type. Write the following method that returns the location of the largest element in a two-dimensional array public static Location locateLargest Cdouble[][] a)

Solution

Location.java

public class Location {
   //Declaring variables
   static int row;
   static int column;
   static double maxValue;
  
   //Parameterized constuctor
   public Location(int row, int column, double maxValue2) {
       super();
       this.row = row;
       this.column = column;
       this.maxValue = maxValue2;
   }
  
   //Getters and setters
   public int getRow() {
       return row;
   }

   public void setRow(int row) {
       this.row = row;
   }

   public int getColumn() {
       return column;
   }

   public void setColumn(int column) {
       this.column = column;
   }

   public double getMaxValue() {
       return maxValue;
   }

   public void setMaxValue(double maxValue) {
       this.maxValue = maxValue;
   }

   //This method finds the maximum value of an array and find the location of the maximum value
   public static Location locateLargest(double a[][])
   {
      
   maxValue=a[0][0];
   for(int i=0;i<a.length;i++)
   {
       for(int j=0;j<a[0].length;j++)
       {
           if(maxValue<a[i][j])
           {
               maxValue=a[i][j];
               row=i;
               column=j;
           }
       }
   }
     
   Location loc=new Location(row, column, maxValue);
   return loc;
     
   }

}

______________________

Test.java

import java.util.Scanner;

public class Test {

   public static void main(String[] args) {
       //Declaring variables
       int row,column;
      
       //Scanner Object is used to get the inputs entered by the user
       Scanner sc=new Scanner(System.in);
      
      
       //Getting the no of rows and columns of an array entered by the user
       System.out.print(\"Enter the number of rows and columns in the array :\");
       row=sc.nextInt();
column=sc.nextInt();
  
//Creating an double type array
double arr[][]=new double[row][column];
  
//Getting the array elements entered by the user and populate them into an array
System.out.println(\"Enter the array :\");
for(int i=0;i<row;i++)
{
   for(int j=0;j<column;j++)
   {
       arr[i][j]=sc.nextDouble();
   }
}
  
//Calling the static method on the Location class object
Location l=Location.locateLargest(arr);
  
//Displaying the maximum element of an array and its location
System.out.println(\"The Location of the largest element is \"+l.getMaxValue()+\" at (\"+l.getRow()+\",\"+l.getColumn()+\")\");
   }

}

_______________________

Output:

Enter the number of rows and columns in the array :3 4
Enter the array :
23.5 35 2 10
4.5 3 45 3.5
35 44 5.5 9.6
The Location of the largest element is 45.0 at (1,2)

____________Thank You

Java 9.13 (The Location class) Design a class named Location for locating a maximal value and its location in a two-dimensional array. The class contains public
Java 9.13 (The Location class) Design a class named Location for locating a maximal value and its location in a two-dimensional array. The class contains public
Java 9.13 (The Location class) Design a class named Location for locating a maximal value and its location in a two-dimensional array. The class contains public

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site