1answer.
Ask question
Login Signup
Ask question
All categories
  • English
  • Mathematics
  • Social Studies
  • Business
  • History
  • Health
  • Geography
  • Biology
  • Physics
  • Chemistry
  • Computers and Technology
  • Arts
  • World Languages
  • Spanish
  • French
  • German
  • Advanced Placement (AP)
  • SAT
  • Medicine
  • Law
  • Engineering
GuDViN [60]
4 years ago
10

(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 type and maxValue as double type.Write the following method that returns the location of the largest element in a two-dimensional array.public static int[] locateLargest(double[][] a)The return value is a one-dimensional array that contains two elements. These two elements indicate the row and column indices of the largest element in the two dimensional array. 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 of the array: 3 4Enter the array:23.5 35 2 104.5 3 45 3.535 44 5.5 9.6The location of the largest element is at (1, 2)

Computers and Technology
1 answer:
-Dominant- [34]4 years ago
3 0

Answer:

Here is the Location class:

public class Location {   //class name

   public int row;  // public data field to hold the value of no. of rows

   public int column;  // public data field to hold the value of no. of columns

   public double maxValue;   //public data field to hold the maximum value

   public Location(int row, int column, double maxValue) { //parameterized constructor of Location class

//this keyword is used to refer to a current instance variable and used to avoid naming conflicts between attributes and parameters with the same name

       this.row = row;  

       this.column = column;  

       this.maxValue = maxValue;     }  

   public static Location locateLargest(double[][] a) {   //method that takes a 2 dimensional array a as argument and returns the location of the largest element

       int row = 0;  //initializes row to 0

       int column = 0;  //initializes column to 0

       double maxValue = a[row][column];   //stores largest element

       for (int i = 0; i < a.length; i++) {  //iterates through rows of a[] array

           for (int j = 0; j < a[i].length; j++) {  //iterates through columns of array

               if (maxValue < a[i][j]) {  //if the maxValue is less than the element of 2 dimensional array at ith row and jth column

                   maxValue = a[i][j];  //then set that element to maxValue

                   row = i;  // i is set to traverse through rows

                   column = j;   }             }         }  // i is set to move through columns

       return new Location(row,column,maxValue);     }  } //calls constructor of class by passing row, column and maxValue as argument

Explanation:

Here is the Main class with a main() function to  test program  

import java.util.Scanner;   //to accept input from user

public class Main {   //class name

   public static void main(String[] args) {   //start of main function

       Scanner scan = new Scanner(System.in);  //creates Scanner class object to scan the input from user

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

       int row = scan.nextInt();  // reads value of row from user

       int column = scan.nextInt();  //reads input value of column

       double[][] array = new double[row][column];   //declares a 2 dimensional array named array

       System.out.println("Enter the array:");  //prompts to enter array elements

       for (int i = 0; i < array.length; i++) {  //iterates through the rows of array until the size of array is reaced

           for (int j = 0; j < array[i].length; j++) {  //iterates through the columns of array

               array[i][j] = scan.nextDouble();    }    }   //reads each element at i-th row and j-th column

       Location location = Location.locateLargest(array);  //calls locateLargest method by passing array to it in order to locate the largest element in the array

System.out.println("The largest element in a two-dimensional array is: " + location.maxValue);  //displays the largest element of array

       System.out.println("The location of the largest element is at (" + location.row + ", " + location.column + ")");       }    } //displays the location of the largest element in the two dimensional array

Suppose the user enters 2 rows and 2 columns. The elements are:

1         5.5

4.5      3

The program works as follows:

for (int i = 0; i < array.length; i++) this outer loop iterates through rows

i = 0

inner loop for (int j = 0; j < array[i].length; j++) iterates through columns

array[i][j] = scan.nextDouble(); reads the element at position i-th row and j-th column. This becomes:

array[0][0] = scan.nextDouble();

so element at 0th row and 0th column is 1

Location location = Location.locateLargest(array); now this calls the method which works as follows:

double maxValue = a[row][column]; this becomes:

double maxValue = a[0][0];

so maxValue = 1

for (int i = 0; i < a.length; i++) this loop in method iterates through rows and  for (int j = 0; j < a[i].length; j++) this iterates through columns of array

if (maxValue < a[i][j]) this becomes:

if (maxValue < a[0][0])

As we know that maxValue = 1 so this if condition is true.

                   maxValue = a[i][j];  this becomes:

             maxValue = a[0][0];  

maxValue = 1

Now set row = 0 and column = 0

Now the inner loop value of j is incremented to 1. So j = 1

At next iteration array[0][1] is checked. The element at this position is 5.5

if (maxValue < a[i][j]) is true because 1<5.5 so now value of maxValue becomes:

maxValue = 5.5

and

i = 0  j = 1

This way at each iteration of inner loop the columns are traversed and at each iteration of outer loop rows are traversed.

At next iteration element at array[1][0] is checked which is 4.5. This is not greater than maxValue so maxValue remains 5.5

At next iteration element at array[1][1] is checked which is 3. This is not greater than maxValue so maxValue remains 5.5 .

After both the loops end the statement:

return new Location(row,column,maxValue);

returns row , column and maxValue

where row = 0  column = 1 and maxValue = 5.5

So the output is:

The largest element in a two-dimensional array is: 5.5

The location of the largest element is at (0,1)

You might be interested in
Do you think EA sports should add more tape jobs to the game? (Game is nhl22)
wariber [46]

Answer:

yes

Explanation:

7 0
2 years ago
Joe is a part of a team where the members come from various cultures and have different perspectives and viewpoints. What does J
Mamont248 [21]

Answer:

various cultures of the world.

Explanation:

You need to understand the cultures of the world, to work in a multinational company. Undoubtedly, you need to understand that the employees working in multinational companies are from various cultures, and they think differently as well. You need to understand them, and only then you can make them your friend and finally work together with full cooperation to ensure the best work. And for your success as well as the success of the company this is important.

6 0
3 years ago
Which of the following documents should beprepared before the commencement of a softwareproject?
ludmilkaskok [199]

Answer:

Software requirement specification

Explanation:

requirement specification is the first step before proceeding with any project.

we should validate our output at all phases with software requirement specification

7 0
3 years ago
What was the #1 song in 1987 and who was the artist
Veronika [31]
Billboard magazine said it was "Walk Like an Egyptian" by The Bangles.
8 0
3 years ago
Read 2 more answers
For an activity with more than one immediate predecessor activity, which of the following is used to compute its earliest finish
laila [671]

Answer:

The correct option is A

Explanation:

In project management, earliest finish time for activity A refers to the earliest start time for succeeding activities such as B and C to start.

Assume that activities A and B comes before C, the earliest finish time for C can be arrived at by computing the earliest start-finish (critical path) of the activity with the largest EF.

That is, if two activities (A and B) come before activity C, one can estimate how long it's going to take to complete activity C if ones knows how long activity B will take (being the activity with the largest earliest finish time).

Cheers!

7 0
3 years ago
Other questions:
  • What is the amount of a good or service that business have available to sell?
    13·1 answer
  • which of these paste options is commonly available from the paste options button? A&gt; use the destination them. B. keep the em
    14·2 answers
  • The objective of an Enterprise Resource Planning (ERP) system is to create a customized software program that integrates the inf
    14·1 answer
  • 4. How can you select non-adjacent cells (i.e. cells that are not all together in one block)?
    5·2 answers
  • Exactly how thin is the air in outer space?
    10·1 answer
  • Write a MATLAB program to accomplish the following: Create two vectors, a and b, where vector a contains all positive integers l
    10·1 answer
  • Which of the following extends a network over a large geographic area by connecting local area networks together?
    14·2 answers
  • The way a student prepares for a test or reviews academic material is referred to as
    8·2 answers
  • Area Triangolo Rettangolo in c++
    6·1 answer
  • [2]<br> (c) Describe how the microprocessor can determine when to sound the clock alarm.
    6·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!