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

Java Program 6 : The Location class Problem Description: **9.13 (The Location cl

ID: 3731512 • Letter: J

Question

Java Program 6 : The Location class

Problem Description:
**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(double[][] a)
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)   

Coding:
Name your program
TestLocation.java
Location.java

Explanation / Answer

PROGRAM

import java.io.*;

import java.util.Scanner;

class Location // Create Class Location

{

// Declare static variable

public static int row,column;

public static double maxValue;

public static int p1,p2;

public static Location locateLargest(double[][] a) // declare and implement static method LocateLargest()

{

Scanner scr=new Scanner(System.in); // Create Scanner Object scr for reading elements

Location l=new Location(); // Create Object of Location l

for(int i=0;i<row;i++){ // Create for loop until row elements

for(int j=0;j<column;j++){ //Create for loop until column elements

if(a[i][j]>maxValue){ // check condition for maximum value

l.maxValue=a[i][j]; // whether condition is true then store maxValue until last element in the 2D Array

// store the position elements in p1 and p2 of maxValue

p1=i;

p2=j;   

}   

}

}

return l; // return Location Object of Maximum value

}

}

class TestLocation // Create Main class TestLocation

{

public static void main(String args[])

{

Scanner scr=new Scanner(System.in); // Create Scanner Object scr for reading elements

// Create Objects l and l1 of Location class

Location l=new Location();

Location l1=new Location();

System.out.print("Enter the number of rows and columns in the array: ");

// read row and column size of 2D Array

l.row=scr.nextInt();

l.column=scr.nextInt();

double [][] a=new double[l.row][l.column]; // Declare 2D Array with row and column size

System.out.println("Enter the array: ");

// Read 2D Matrix of l.row X l.column size

for(int i=0;i<l.row;i++)

for(int j=0;j<l.column;j++)

a[i][j]=scr.nextDouble();

l1=l.locateLargest(a); // call locateLargest() method and return maximum value

System.out.print("The location of the largest element is "+l.maxValue+" at ("+l.p1+", "+l.p2+")"); // print maxValue and position of 2D Array

}

}

OUTPUT-1

F:>javac TestLocation.java

F:>java TestLocation
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)

OUTPUT-2

F:>java TestLocation
Enter the number of rows and columns in the array: 4 4
Enter the array:

34.5 6 78.5 2
4 55.4 2.3 8.7
43.4 65.5 99.6 44
12 33 5.4 6.5
The location of the largest element is 99.6 at (2, 2)

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote