Question

I need assistance writing the following Java class. Design a class named Location for locating a...

I need assistance writing the following Java 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 TestLocation class with a main method that prompts the user to enter a two-dimensional array and displays the location of the largest element in the array using the Location class's locateLargest method. 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).

0 0
Add a comment Improve this question Transcribed image text
Answer #1

import java.util.Scanner;
class Location {
    static int row, col;
    static double maxValue = 0;

    Location(int row, int col, double maxValue){
        this.row = row;
        this.col = col;
        this.maxValue = maxValue;
    }
  
    public static Location locateLargest(double[][] a) {
        int maxRow = 0, maxCol = 0, cl;
      
        for (int rw = 0; rw < a[0].length - 1; rw++) {
            double currMax = a[rw][0];
            int currMaxRow = rw;
            int currMaxCol = 0;
            for (cl = 0; cl < a[rw].length; cl++) {
                if (currMax < a[rw][cl]) {
                    currMax = a[rw][cl];  
                    currMaxRow = rw;
                    currMaxCol = cl;
                }
            }
            if (maxValue < currMax) {
                maxRow = currMaxRow;
                maxCol = currMaxCol;
                maxValue = currMax;
            }
        }
        return new Location(maxRow, maxCol, maxValue);     
    }  
}

public class Exercise09_13 {
    public static void main(String[] args) {
        double maxValue = 0;
        Location location;
        Scanner input = new Scanner(System.in);
      
      
        System.out.println("enter the number of rows and columns in the array: ");
        int row = input.nextInt();
        int col = input.nextInt();
        double[][] list = new double[row][col];
        int cl, rw;
      
        System.out.println("enter the array");
      
        for (rw = 0; rw < row; rw++)
            for (cl = 0; cl < col; cl++)
                list[rw][cl] = input.nextDouble();
      
                
        new Location(row, col, list[row - 1][col - 1]);
        Location.locateLargest(list);
      
        System.out.println("\n");
      
        System.out.printf("the location of the largest element is %.2f at "
                + "(%d, %d)", Location.maxValue, Location.row, Location.col);
    }
}

Add a comment
Know the answer?
Add Answer to:
I need assistance writing the following Java class. Design a class named Location for locating a...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • this language is JAVA 2. Design a class named Location for locating a maximum value and...

    this language is JAVA 2. Design a class named Location for locating a maximum 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) Create...

  • Given the following construct a UML diagram for Java: Design a class named location for locating...

    Given the following construct a UML diagram for Java: 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 maximum value and its indices in a two-dimensional array with row and column as int types and maxValue as double type. Write the following method that returns the location of the largest element in two-dimensional array: Public static Location locateLargest(double[][] a)...

  • SOLVE IN PYTHON: Exercise #2: Develop a program (name it LocateLargestElement) that prints out the location...

    SOLVE IN PYTHON: Exercise #2: Develop a program (name it LocateLargestElement) that prints out the location of the first largest value in a two-dimensional array. The largest values may appear more than once in the array. The program defines method locateLargest() that takes a two-dimensional array of integers and returns the location (row index and column index) of the first largest value as a single-dimensional array. The program main method prompts the user to enter a 3-by-4 matrix, prints out...

  • IN C#.Develop a program that prints out the location of the largest value in a two-dimensional...

    IN C#.Develop a program that prints out the location of the largest value in a two-dimensional array. The largest values may appear more than once in the array, so you must only print out the first instance. The program defines method locateLargest() that takes a two-dimensional array of integers and returns the location (row index and column index) of the first largest value as a single-dimensional array. The program main method prompts the user to enter a 3-by-4 matrix, prints...

  • This needs to be in python, however, I am having trouble with the code. Is there...

    This needs to be in python, however, I am having trouble with the code. Is there any way to use the code that I already have under the main method? If so, what would be the rest of the code to finish it? #Create main method def main(): #Create empty list list=[] #Display to screen print("Please enter a 3 x 4 array:") #Create loop for rows and have each entered number added to list for i in range(3): list.append([int(x) for...

  • Java Programming Assignment Write a class named 2DArrayOperations with the following static methods: getTotal . This...

    Java Programming Assignment Write a class named 2DArrayOperations with the following static methods: getTotal . This method should accept a two-dimensional array as its argument and return the total of all the values in the array. Write overloaded versions of this method that work with int , double , and long arrays. (A) getAverage . This method should accept a two-dimensional array as its argument and return the average of all the values in the array. Write overloaded versions of...

  • Java Programming (use jGrasp if not thats fine) Write a Two classes one class that creates...

    Java Programming (use jGrasp if not thats fine) Write a Two classes one class that creates a two-dimensional 2 rows by 3 columns double array. This class will have a separate method for the user to populate the array with data values. The other class that has the following methods:   getTotal. This method should accept a two-dimensional array as its argument and return the total of all the values in the array.   getAverage. This method should accept a two-dimensional array...

  • You must create a Java class named TenArrayMethods in a file named TenArrayMethods.java. This class must...

    You must create a Java class named TenArrayMethods in a file named TenArrayMethods.java. This class must include all the following described methods. Each of these methods should be public and static.Write a method named getFirst, that takes an Array of int as an argument and returns the value of the first element of the array. NO array lists. Write a method named getLast, that takes an Array of int as an argument and returns the value of the last element...

  • Java 1. Write a getCount method in the IntArrayWorker class that returns the count of the...

    Java 1. Write a getCount method in the IntArrayWorker class that returns the count of the number of times a passed integer value is found in the matrix. There is already a method to test this in IntArrayWorkerTester. Just uncomment the method testGetCount() and the call to it in the main method of IntArrayWorkerTester. 2. Write a getLargest method in the IntArrayWorker class that returns the largest value in the matrix. There is already a method to test this in...

  • This is for Java. Create ONE method/function that will return an array containing the row and...

    This is for Java. Create ONE method/function that will return an array containing the row and column of the largest integer i the 2D array. If the largest number is located on row 2, column 1, the method needs to return row 2 and column one. Do not use more than one method. Use the code below as the main. Please comment any changes. in java Given the main, create a method that RETURNS the largest number found in the...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT