The programming language is Java. Following the instructions in the problem stat
ID: 3856844 • Letter: T
Question
The programming language is Java.
Following the instructions in the problem statement, design and implement a Java program for programming exercise 9.13, page 363 (name it Location.java). Next, develop a test program in a separate file (call it TestLocation.java) to create Location object to test all methods. Use proper labels in your outputs. Document your code, and organize and space the outputs properly. Use escape characters and formatting objects when applicable. (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: The return value is an instance of Location. Write a test program that prompts the user to enter a two-dimensional array and displays the location of the largest element in the array. Here is a sample run: 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 at (1, 2)Explanation / Answer
import java.util.*;
class Location
{
public static double maxValue;
public static int row,column;
public Location()
{
row = -1;
column = -1;
maxValue = 0;
}
public static Location largest(double arr[][],int m,int n)
{
Location l = new Location();
int i,j;
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
if(maxValue < arr[i][j])
{
l.maxValue = arr[i][j];
l.row = i+1;
l.column = j+1;
}
}
}
return l;
}
}
class TestLocation
{
public static void main (String[] args)
{
double[][] arr = new double[20][20];
int m,n,i,j;
Scanner scan = new Scanner(System.in);
System.out.println("Enter the number of rows and columns in the array : ");
m = scan.nextInt();
n= scan.nextInt();
System.out.println("Enter the array : ");
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
arr[i][j] = scan.nextDouble();
}
}
Location l = new Location();
l = l.largest(arr,m,n);
System.out.println("The location of the largest element is : "+l.maxValue +" at ("+l.row +","+l.column+")");
}
}
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 (2,3)
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.